2013-01-20 12:36:52 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require "ostruct"
|
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe MusicBrainz::ClientModules::CachingProxy do
|
|
|
|
let(:test_mbid){ "69b39eab-6577-46a4-a9f5-817839092033" }
|
2015-03-13 19:39:11 +00:00
|
|
|
let(:tmp_cache_path){ File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'cache_module_spec_cache') }
|
2013-01-20 12:36:52 +00:00
|
|
|
let(:test_cache_file){ "#{tmp_cache_path}/03/48/ec/6c2bee685d9a96f95ed46378f624714e7a4650b0d44c1a8eee5bac2480.xml" }
|
|
|
|
let(:test_response_file){ File.join(File.dirname(__FILE__), "../fixtures/kasabian.xml") }
|
|
|
|
let(:test_response){ File.open(test_response_file).read }
|
|
|
|
|
|
|
|
before(:all) do
|
2013-05-06 18:22:41 +00:00
|
|
|
MusicBrainz.config.cache_path = File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'cache_module_spec_cache')
|
2013-01-20 12:36:52 +00:00
|
|
|
end
|
|
|
|
|
2015-03-13 19:39:11 +00:00
|
|
|
before do
|
|
|
|
File.delete(test_cache_file) if File.exist?(test_cache_file)
|
|
|
|
end
|
|
|
|
|
2013-01-20 12:36:52 +00:00
|
|
|
after(:all) do
|
2015-03-13 19:39:11 +00:00
|
|
|
MusicBrainz.config.cache_path = File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'spec_cache')
|
2013-01-20 12:36:52 +00:00
|
|
|
MusicBrainz.config.perform_caching = true
|
|
|
|
MusicBrainz.config.query_interval = 1.5
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with cache enabled" do
|
|
|
|
it "calls http only once when requesting the resource twice" do
|
|
|
|
MusicBrainz.config.perform_caching = true
|
2015-03-13 19:39:11 +00:00
|
|
|
expect(File).to_not exist(test_cache_file)
|
2013-01-20 12:36:52 +00:00
|
|
|
|
|
|
|
# Stubbing
|
2015-03-13 19:39:11 +00:00
|
|
|
allow(MusicBrainz.client.http).to receive(:get).and_return(OpenStruct.new(status: 200, body: test_response))
|
|
|
|
expect(MusicBrainz.client.http).to receive(:get).once
|
2013-01-20 12:36:52 +00:00
|
|
|
|
|
|
|
2.times do
|
|
|
|
artist = MusicBrainz::Artist.find(test_mbid)
|
2015-03-13 19:39:11 +00:00
|
|
|
expect(artist).to be_kind_of(MusicBrainz::Artist)
|
|
|
|
expect(File).to exist(test_cache_file)
|
2013-01-20 12:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
MusicBrainz.client.clear_cache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with cache disabled" do
|
|
|
|
it "calls http twice when requesting the resource twice" do
|
|
|
|
MusicBrainz.config.perform_caching = false
|
2015-03-13 19:39:11 +00:00
|
|
|
expect(File).to_not exist(test_cache_file)
|
2013-01-20 12:36:52 +00:00
|
|
|
|
|
|
|
# Hacking for test performance purposes
|
|
|
|
MusicBrainz.config.query_interval = 0.0
|
|
|
|
|
|
|
|
# Stubbing
|
2015-03-13 19:39:11 +00:00
|
|
|
allow(MusicBrainz.client.http).to receive(:get).and_return(OpenStruct.new(status: 200, body: test_response))
|
|
|
|
expect(MusicBrainz.client.http).to receive(:get).twice
|
2013-01-20 12:36:52 +00:00
|
|
|
|
|
|
|
2.times do
|
|
|
|
artist = MusicBrainz::Artist.find(test_mbid)
|
2015-03-13 19:39:11 +00:00
|
|
|
expect(artist).to be_kind_of(MusicBrainz::Artist)
|
|
|
|
expect(File).to_not exist(test_cache_file)
|
2013-01-20 12:36:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|