Mass rewrite

This commit is contained in:
Gregory Eremin
2012-10-12 16:45:51 +04:00
parent 3bf68d152e
commit 7c42a90a6c
46 changed files with 656 additions and 696 deletions
+24 -12
View File
@@ -4,35 +4,47 @@ require "spec_helper"
describe MusicBrainz do
before(:all) {
@old_cache_path = MusicBrainz::Tools::Cache.cache_path
@old_cache_path = MusicBrainz.config.cache_path
@old_query_interval = MusicBrainz.config.query_interval
}
before(:each) {
$stdout.stub!(:puts)
MusicBrainz::Tools::Cache.cache_path = nil
MusicBrainz.config.cache_path = nil
MusicBrainz.config.query_interval = nil
}
after(:all) {
MusicBrainz::Tools::Cache.cache_path = @old_cache_path
MusicBrainz.config.cache_path = @old_cache_path
MusicBrainz.config.query_interval = @old_query_interval
}
it "allows deprecated use of cache_path" do
MusicBrainz::Tools::Cache.cache_path = "some/path"
MusicBrainz::cache_path.should == "some/path"
MusicBrainz.config.cache_path = "test1"
MusicBrainz::Tools::Cache.cache_path.should == "test1"
MusicBrainz.cache_path.should == "test1"
end
it "allows deprecated use of cache_path=" do
MusicBrainz.cache_path = "some/path"
MusicBrainz::Tools::Cache.cache_path.should == "some/path"
MusicBrainz::Tools::Cache.cache_path = "test2"
MusicBrainz.config.cache_path.should == "test2"
MusicBrainz.cache_path = "test3"
MusicBrainz.config.cache_path.should == "test3"
end
it "allows deprecated use of query_interval" do
MusicBrainz::Tools::Proxy.query_interval = 2
MusicBrainz::query_interval.should == 2
MusicBrainz.config.query_interval = 2
MusicBrainz::Tools::Proxy.query_interval.should == 2
MusicBrainz.query_interval.should == 2
end
it "allows deprecated use of query_interval=" do
MusicBrainz.query_interval = 2
MusicBrainz::Tools::Proxy.query_interval.should == 2
MusicBrainz::Tools::Proxy.query_interval = 3
MusicBrainz.config.query_interval.should == 3
MusicBrainz.query_interval = 4
MusicBrainz.config.query_interval.should == 4
end
end
+2 -8
View File
@@ -24,15 +24,9 @@ describe MusicBrainz::Artist do
matches = MusicBrainz::Artist.search('Chris Martin')
matches[0][:score].should == 100
matches[0][:mbid].should == "98d1ec5a-dd97-4c0b-9c83-7928aac89bca"
matches[0][:id].should == "98d1ec5a-dd97-4c0b-9c83-7928aac89bca"
matches[1][:score].should == 100
matches[1][:mbid].should == "af2ab893-3212-4226-9e73-73a1660b6952"
matches[2][:score].should == 95
matches[2][:mbid].should == "444d1b63-534b-4ea6-89f0-0af6ab2e20c3"
matches[3][:score].should == 95
matches[3][:mbid].should == "b732a912-af95-472c-be52-b14610734c64"
matches[4][:score].should == 95
matches[4][:mbid].should == "90fff570-a4ef-4cd4-ba21-e00c7261b05a"
matches[1][:id].should == "af2ab893-3212-4226-9e73-73a1660b6952"
end
it "finds name first than alias" do
+9 -5
View File
@@ -1,12 +1,16 @@
# -*- encoding: utf-8 -*-
require "rubygems"
require "bundler/setup"
require "ap"
require "musicbrainz"
MusicBrainz::Tools::Cache.cache_path = "tmp/cache"
MusicBrainz.configure do |c|
test_email = %x{ git config --global --get user.email }.gsub(/\n/, "")
test_email = "magnolia_fan@me.com" if test_email.empty?
c.app_name = "MusicBrainzGemTestSuite"
c.app_version = MusicBrainz::VERSION
c.contact = test_email
c.perform_caching = true
end
RSpec.configure do |config|
# Configuration is not currently necessary
+34 -29
View File
@@ -1,59 +1,64 @@
# -*- encoding: utf-8 -*-
require "ostruct"
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')
@tmp_cache_path = File.join(File.dirname(__FILE__), "../../tmp/cache/test")
@test_mbid = "69b39eab-6577-46a4-a9f5-817839092033"
@test_cache_file = "#{@tmp_cache_path}/03/48/ec6c2bee685d9a96f95ed46378f624714e7a4650b0d44c1a8eee5bac2480.xml"
end
after(:all) do
MusicBrainz::Tools::Cache.cache_path = @old_cache_path
MusicBrainz.config.cache_path = @old_cache_path
end
before(:each) do
file_path = File.join(File.dirname(__FILE__), "../fixtures/kasabian.xml")
@test_response = ::StringIO.new(File.open(file_path).gets)
@test_response = File.open(file_path).read
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
mbid = "69b39eab-6577-46a4-a9f5-817839092033"
it "calls http only once when requesting the resource twice" do
MusicBrainz.config.cache_path = @tmp_cache_path
File.exist?(@test_cache_file).should be_false
MusicBrainz::Tools::Proxy.stub(:get_contents).and_return(@test_response)
MusicBrainz::Tools::Proxy.should_receive(:get_contents).once
# Stubbing
MusicBrainz::Client.http.stub(:get).and_return(OpenStruct.new(status: 200, body: @test_response))
MusicBrainz::Client.http.should_receive(:get).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)
2.times do
artist = MusicBrainz::Artist.find(@test_mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
File.exist?(@test_cache_file).should be_true
end
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
MusicBrainz::Client.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
mbid = "69b39eab-6577-46a4-a9f5-817839092033"
it "calls http twice when requesting the resource twice" do
MusicBrainz.config.perform_caching = false
File.exist?(@test_cache_file).should be_false
MusicBrainz::Tools::Proxy.stub(:get_contents).and_return(@test_response)
MusicBrainz::Tools::Proxy.should_receive(:get_contents).twice
# Hacking for test performance purposes
MusicBrainz.config.query_interval = 0.0
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)
# Stubbing
MusicBrainz::Client.http.stub(:get).and_return(OpenStruct.new(status: 200, body: @test_response))
MusicBrainz::Client.http.should_receive(:get).twice
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)
2.times do
artist = MusicBrainz::Artist.find(@test_mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
File.exist?(@test_cache_file).should be_false
end
MusicBrainz.config.perform_caching = true
MusicBrainz.config.query_interval = 1.5
end
end
end