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 @test_response = ::StringIO.new(''+ ''+ ''+ 'KasabianKasabianGB'+ '1999'+ 'http://allmusic.com/artist/p678134'+ 'http://en.wikipedia.org/wiki/Kasabian'+ 'http://twitter.com/kasabianhq'+ ''+ 'http://www.bbc.co.uk/music/artists/69b39eab-6577-46a4-a9f5-817839092033'+ 'http://www.discogs.com/artist/Kasabian'+ 'http://www.facebook.com/kasabian'+ 'http://www.imdb.com/name/nm1868442/'+ 'http://www.kasabian.co.uk/'+ 'http://www.myspace.com/kasabian'+ 'http://www.youtube.com/kasabianvevo'+ 'http://www.youtube.com/user/KasabianTour'+ '') 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 MusicBrainz::Tools::Proxy.stub(:get_contents).and_return(@test_response) MusicBrainz::Tools::Proxy.should_receive(:get_contents).once mbid = "69b39eab-6577-46a4-a9f5-817839092033" 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 MusicBrainz::Tools::Proxy.stub(:get_contents).and_return(@test_response) MusicBrainz::Tools::Proxy.should_receive(:get_contents).twice mbid = "69b39eab-6577-46a4-a9f5-817839092033" 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