2012-07-08 22:37:46 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
2012-07-04 13:10:47 +00:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe MusicBrainz::Tools::Cache do
|
|
|
|
before(:all) do
|
|
|
|
@old_cache_path = MusicBrainz::Tools::Cache.cache_path
|
|
|
|
@tmp_cache_path = File.join(File.dirname(__FILE__), '../../tmp/cache/tools')
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) do
|
|
|
|
MusicBrainz::Tools::Cache.cache_path = @old_cache_path
|
|
|
|
end
|
|
|
|
|
|
|
|
before(:each) do
|
2012-07-08 22:22:23 +00:00
|
|
|
file_path = File.join(File.dirname(__FILE__), "../fixtures/kasabian.xml")
|
|
|
|
@test_response = ::StringIO.new(File.open(file_path).gets)
|
2012-07-04 13:10:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with cache enabled" do
|
|
|
|
it "calls get contents only once when requesting the resource twice" do
|
|
|
|
MusicBrainz::Tools::Cache.cache_path = @tmp_cache_path
|
2012-07-08 22:22:23 +00:00
|
|
|
mbid = "69b39eab-6577-46a4-a9f5-817839092033"
|
|
|
|
|
2012-07-04 13:10:47 +00:00
|
|
|
MusicBrainz::Tools::Proxy.stub(:get_contents).and_return(@test_response)
|
|
|
|
MusicBrainz::Tools::Proxy.should_receive(:get_contents).once
|
|
|
|
|
|
|
|
File.exist?("#{@tmp_cache_path}/artist/#{mbid}?inc=url-rels").should be_false
|
|
|
|
artist = MusicBrainz::Artist.find(mbid)
|
|
|
|
artist.should be_a_kind_of(MusicBrainz::Artist)
|
|
|
|
|
|
|
|
File.exist?("#{@tmp_cache_path}/artist/#{mbid}?inc=url-rels").should be_true
|
|
|
|
artist = MusicBrainz::Artist.find(mbid)
|
|
|
|
artist.should be_a_kind_of(MusicBrainz::Artist)
|
|
|
|
|
|
|
|
MusicBrainz::Tools::Cache.clear_cache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with cache disabled" do
|
|
|
|
it "calls get contents twice when requesting the resource twice" do
|
|
|
|
MusicBrainz::Tools::Cache.cache_path = nil
|
2012-07-08 22:22:23 +00:00
|
|
|
mbid = "69b39eab-6577-46a4-a9f5-817839092033"
|
|
|
|
|
2012-07-04 13:10:47 +00:00
|
|
|
MusicBrainz::Tools::Proxy.stub(:get_contents).and_return(@test_response)
|
|
|
|
MusicBrainz::Tools::Proxy.should_receive(:get_contents).twice
|
|
|
|
|
|
|
|
File.exist?("#{@tmp_cache_path}/artist/#{mbid}?inc=url-rels").should be_false
|
|
|
|
artist = MusicBrainz::Artist.find(mbid)
|
|
|
|
artist.should be_a_kind_of(MusicBrainz::Artist)
|
|
|
|
|
|
|
|
File.exist?("#{@tmp_cache_path}/artist/#{mbid}?inc=url-rels").should be_false
|
|
|
|
@test_response.rewind
|
|
|
|
MusicBrainz.stub(:get_contents).and_return(@test_response)
|
|
|
|
artist = MusicBrainz::Artist.find(mbid)
|
|
|
|
artist.should be_a_kind_of(MusicBrainz::Artist)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|