Skip to content

Commit

Permalink
Update to use modern RSpec syntax and conventions
Browse files Browse the repository at this point in the history
* Replace `should` syntax with `expect` syntax.
* Add pry as a development dependency.
* Fix failing specs.
  • Loading branch information
jvoegele committed Mar 13, 2015
1 parent 4d12555 commit ad4a53f
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 138 deletions.
1 change: 1 addition & 0 deletions musicbrainz.gemspec
Expand Up @@ -19,4 +19,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency('rspec')
gem.add_development_dependency('awesome_print')
gem.add_development_dependency('rake', '~> 10.4.0')
gem.add_development_dependency('pry')
end
10 changes: 5 additions & 5 deletions spec/bindings/relations_spec.rb
Expand Up @@ -15,11 +15,11 @@
</relation>
</relation-list></artist>}
)
described_class.parse(xml.xpath('./artist'))[:urls][:social_network].should == 'https://plus.google.com/+Madonna'

expect(described_class.parse(xml.xpath('./artist'))[:urls][:social_network]).to eq 'https://plus.google.com/+Madonna'
end
end

context 'multiple urls for relation types' do
it 'returns an array' do
xml = Nokogiri::XML.parse(
Expand All @@ -33,12 +33,12 @@
</relation-list></artist>}
)

described_class.parse(xml.xpath('./artist'))[:urls][:social_network].should == [
expect(described_class.parse(xml.xpath('./artist'))[:urls][:social_network]).to eq [
'https://plus.google.com/+Madonna', 'https://www.facebook.com/madonna'
]
end
end
end
end
end
end
end
14 changes: 10 additions & 4 deletions spec/bindings/release_group_search_spec.rb
Expand Up @@ -4,14 +4,20 @@

describe MusicBrainz::Bindings::ReleaseGroupSearch do
describe '.parse' do
let(:response) {
'<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0"><release-group-list><release-group id="246bc928-2dc8-35ba-80ee-7a0079de1632" type="Single" ext:score="100"><title>Empire</title></release-group></release-group-list></metadata>'
}
let(:metadata) {
described_class.parse(Nokogiri::XML.parse(response).remove_namespaces!.xpath('/metadata'))
}

it "gets correct release group data" do
response = '<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0"><release-group-list><release-group id="246bc928-2dc8-35ba-80ee-7a0079de1632" type="Single" ext:score="100"><title>Empire</title></release-group></release-group-list></metadata>'
described_class.parse(Nokogiri::XML.parse(response).remove_namespaces!.xpath('/metadata')).should == [
expect(metadata).to eq([
{
id: '246bc928-2dc8-35ba-80ee-7a0079de1632', mbid: '246bc928-2dc8-35ba-80ee-7a0079de1632',
id: '246bc928-2dc8-35ba-80ee-7a0079de1632', mbid: '246bc928-2dc8-35ba-80ee-7a0079de1632',
title: 'Empire', type: 'Single', score: 100
}
]
])
end
end
end
16 changes: 8 additions & 8 deletions spec/bindings/release_spec.rb
Expand Up @@ -10,34 +10,34 @@
it 'returns CD' do
response = '<release><medium-list count="1"><medium><format>CD</format></medium></medium-list></release>'
xml = Nokogiri::XML.parse(response)
described_class.parse(xml)[:format].should == 'CD'
expect(described_class.parse(xml)[:format]).to eq 'CD'
end
end

context 'multiple cds' do
it 'returns 2xCD' do
response = '<release><medium-list count="2"><medium><format>CD</format><track-list count="11" /></medium><medium><title>bonus disc</title><format>CD</format></medium></medium-list></release>'
xml = Nokogiri::XML.parse(response)
described_class.parse(xml)[:format].should == '2xCD'
expect(described_class.parse(xml)[:format]).to eq '2xCD'
end
end

context 'different formats' do
it 'returns DVD + CD' do
response = '<release><medium-list count="2"><medium><format>DVD</format></medium><medium><format>CD</format></medium></medium-list></release>'
xml = Nokogiri::XML.parse(response)
described_class.parse(xml)[:format].should == 'DVD + CD'
expect(described_class.parse(xml)[:format]).to eq 'DVD + CD'
end
end

context 'different formats plus multiple mediums with same format' do
it 'returns 2xCD + DVD' do
response = '<release><medium-list count="2"><medium><format>CD</format></medium><medium><format>CD</format></medium><medium><format>DVD</format></medium></medium-list></release>'
xml = Nokogiri::XML.parse(response)
described_class.parse(xml)[:format].should == '2xCD + DVD'
expect(described_class.parse(xml)[:format]).to eq '2xCD + DVD'
end
end
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/bindings/track_search_spec.rb
Expand Up @@ -6,9 +6,9 @@
describe '.parse' do
it "gets correct Track (really recording) data" do
response = '<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0"><recording-list offset="0" count="1"><recording id="0b382a13-32f0-4743-9248-ba5536a6115e" ext:score="100"><title>King Fred</title><artist-credit><name-credit><artist id="f52f7a92-d495-4d32-89e7-8b1e5b8541c8"><name>Too Much Joy</name></artist></name-credit></artist-credit><release-list><release id="8442e42b-c40a-4817-89a0-dbe663c94d2d"><title>Green Eggs and Crack</title></release></release-list></recording></recording-list></metadata>'
described_class.parse(Nokogiri::XML.parse(response).remove_namespaces!.xpath('/metadata')).should == [
expect(described_class.parse(Nokogiri::XML.parse(response).remove_namespaces!.xpath('/metadata'))).to eq [
{
id: '0b382a13-32f0-4743-9248-ba5536a6115e', mbid: '0b382a13-32f0-4743-9248-ba5536a6115e',
id: '0b382a13-32f0-4743-9248-ba5536a6115e', mbid: '0b382a13-32f0-4743-9248-ba5536a6115e',
title: 'King Fred', artist: 'Too Much Joy', releases: ['Green Eggs and Crack'], score: 100
}
]
Expand Down
29 changes: 16 additions & 13 deletions spec/client_modules/cache_spec.rb
Expand Up @@ -4,9 +4,8 @@
require "spec_helper"

describe MusicBrainz::ClientModules::CachingProxy do
let(:old_cache_path){ File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'spec_cache') }
let(:tmp_cache_path){ File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'cache_module_spec_cache') }
let(:test_mbid){ "69b39eab-6577-46a4-a9f5-817839092033" }
let(:tmp_cache_path){ File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'cache_module_spec_cache') }
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 }
Expand All @@ -15,25 +14,29 @@
MusicBrainz.config.cache_path = File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'cache_module_spec_cache')
end

before do
File.delete(test_cache_file) if File.exist?(test_cache_file)
end

after(:all) do
MusicBrainz.config.cache_path = old_cache_path
MusicBrainz.config.cache_path = File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'spec_cache')
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
File.exist?(test_cache_file).should be_false
expect(File).to_not exist(test_cache_file)

# Stubbing
MusicBrainz.client.http.stub(:get).and_return(OpenStruct.new(status: 200, body: test_response))
MusicBrainz.client.http.should_receive(:get).once
allow(MusicBrainz.client.http).to receive(:get).and_return(OpenStruct.new(status: 200, body: test_response))
expect(MusicBrainz.client.http).to receive(:get).once

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
expect(artist).to be_kind_of(MusicBrainz::Artist)
expect(File).to exist(test_cache_file)
end

MusicBrainz.client.clear_cache
Expand All @@ -43,19 +46,19 @@
context "with cache disabled" do
it "calls http twice when requesting the resource twice" do
MusicBrainz.config.perform_caching = false
File.exist?(test_cache_file).should be_false
expect(File).to_not exist(test_cache_file)

# Hacking for test performance purposes
MusicBrainz.config.query_interval = 0.0

# Stubbing
MusicBrainz.client.http.stub(:get).and_return(OpenStruct.new(status: 200, body: test_response))
MusicBrainz.client.http.should_receive(:get).twice
allow(MusicBrainz.client.http).to receive(:get).and_return(OpenStruct.new(status: 200, body: test_response))
expect(MusicBrainz.client.http).to receive(:get).twice

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
expect(artist).to be_kind_of(MusicBrainz::Artist)
expect(File).to_not exist(test_cache_file)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/deprecated/cache_config_spec.rb
Expand Up @@ -18,15 +18,15 @@
it "allows deprecated use of cache_path" do
MusicBrainz.config.cache_path = "test1"

MusicBrainz::Tools::Cache.cache_path.should == "test1"
MusicBrainz.cache_path.should == "test1"
expect(MusicBrainz::Tools::Cache.cache_path).to eq "test1"
expect(MusicBrainz.cache_path).to eq "test1"
end

it "allows deprecated use of cache_path=" do
MusicBrainz::Tools::Cache.cache_path = "test2"
MusicBrainz.config.cache_path.should == "test2"
expect(MusicBrainz.config.cache_path).to eq "test2"

MusicBrainz.cache_path = "test3"
MusicBrainz.config.cache_path.should == "test3"
expect(MusicBrainz.config.cache_path).to eq "test3"
end
end
8 changes: 4 additions & 4 deletions spec/deprecated/proxy_config_spec.rb
Expand Up @@ -18,15 +18,15 @@
it "allows deprecated use of query_interval" do
MusicBrainz.config.query_interval = 2

MusicBrainz::Tools::Proxy.query_interval.should == 2
MusicBrainz.query_interval.should == 2
expect(MusicBrainz::Tools::Proxy.query_interval).to eq 2
expect(MusicBrainz.query_interval).to eq 2
end

it "allows deprecated use of query_interval=" do
MusicBrainz::Tools::Proxy.query_interval = 3
MusicBrainz.config.query_interval.should == 3
expect(MusicBrainz.config.query_interval).to eq 3

MusicBrainz.query_interval = 4
MusicBrainz.config.query_interval.should == 4
expect(MusicBrainz.config.query_interval).to eq 4
end
end
49 changes: 25 additions & 24 deletions spec/models/artist_spec.rb
Expand Up @@ -4,56 +4,57 @@

describe MusicBrainz::Artist do
it "gets no exception while loading artist info" do
lambda {
expect {
MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
}.should_not raise_error(Exception)
}.to_not raise_error(Exception)
end

it "gets correct instance" do
artist = MusicBrainz::Artist.find_by_name('Kasabian')
artist.should be_an_instance_of(MusicBrainz::Artist)
expect(artist).to be_instance_of(MusicBrainz::Artist)
end

it "searches artist by name" do
matches = MusicBrainz::Artist.search('Kasabian')
matches.length.should be > 0
matches.first[:name].should == "Kasabian"
expect(matches).to_not be_empty
expect(matches.first[:name]).to eq("Kasabian")
end

it "should return search results in the right order and pass back the correct score" do
response = File.open(File.join(File.dirname(__FILE__), "../fixtures/artist/search.xml")).read
MusicBrainz::Client.any_instance.stub(:get_contents).with('http://musicbrainz.org/ws/2/artist?query=artist:"Chris+Martin"&limit=10').
and_return({ status: 200, body: response})

allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents)
.with('http://musicbrainz.org/ws/2/artist?query=artist:"Chris+Martin"&limit=10')
.and_return({ status: 200, body: response})

matches = MusicBrainz::Artist.search('Chris Martin')

matches[0][:score].should == 100
matches[0][:id].should == "90fff570-a4ef-4cd4-ba21-e00c7261b05a"
matches[1][:score].should == 100
matches[1][:id].should == "b732a912-af95-472c-be52-b14610734c64"
expect(matches[0][:score]).to eq 100
expect(matches[0][:id]).to eq "90fff570-a4ef-4cd4-ba21-e00c7261b05a"
expect(matches[1][:score]).to eq 100
expect(matches[1][:id]).to eq "b732a912-af95-472c-be52-b14610734c64"
end

it "gets correct result by name" do
artist = MusicBrainz::Artist.find_by_name('Kasabian')
artist.id.should == "69b39eab-6577-46a4-a9f5-817839092033"
expect(artist.id).to eq "69b39eab-6577-46a4-a9f5-817839092033"
end

it "gets correct artist data" do
artist = MusicBrainz::Artist.find_by_name('Kasabian')
artist.id.should == "69b39eab-6577-46a4-a9f5-817839092033"
artist.type.should == "Group"
artist.name.should == "Kasabian"
artist.country.should == "GB"
artist.date_begin.year.should == 1999
expect(artist.id).to eq "69b39eab-6577-46a4-a9f5-817839092033"
expect(artist.type).to eq "Group"
expect(artist.name).to eq "Kasabian"
expect(artist.country).to eq "GB"
expect(artist.date_begin.year).to eq 1997
end

it "gets correct artist's release groups" do
release_groups = MusicBrainz::Artist.find_by_name('Kasabian').release_groups
release_groups.length.should be >= 16
release_groups.first.id.should == "533cbc5f-ec7e-32ab-95f3-8d1f804a5176"
release_groups.first.type.should == "Single"
release_groups.first.title.should == "Club Foot"
release_groups.first.first_release_date.should == Date.new(2004, 5, 10)
release_groups.first.urls[:discogs].should == 'http://www.discogs.com/master/125150'
expect(release_groups.length).to be >= 16
expect(release_groups.first.id).to eq "533cbc5f-ec7e-32ab-95f3-8d1f804a5176"
expect(release_groups.first.type).to eq "Single"
expect(release_groups.first.title).to eq "Club Foot"
expect(release_groups.first.first_release_date).to eq Date.new(2004, 5, 10)
expect(release_groups.first.urls[:discogs]).to eq 'http://www.discogs.com/master/125150'
end
end
16 changes: 8 additions & 8 deletions spec/models/base_model_spec.rb
Expand Up @@ -10,36 +10,36 @@
response = '<release-group><first-release-date></first-release-date></release-group>'
xml = Nokogiri::XML.parse(response)
release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml)
release_group.first_release_date.should == Date.new(2030, 12, 31)
expect(release_group.first_release_date).to eq Date.new(2030, 12, 31)
end
end

context 'year only' do
it 'returns 1995-12-31' do
response = '<release-group><first-release-date>1995</first-release-date></release-group>'
xml = Nokogiri::XML.parse(response)
release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml)
release_group.first_release_date.should == Date.new(1995, 12, 31)
expect(release_group.first_release_date).to eq Date.new(1995, 12, 31)
end
end

context 'year and month only' do
it 'returns 1995-04-30' do
response = '<release-group><first-release-date>1995-04</first-release-date></release-group>'
xml = Nokogiri::XML.parse(response)
release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml)
release_group.first_release_date.should == Date.new(1995, 4, 30)
expect(release_group.first_release_date).to eq Date.new(1995, 4, 30)
end
end

context 'year, month and day' do
it 'returns 1995-04-30' do
response = '<release-group><first-release-date>1995-04-30</first-release-date></release-group>'
xml = Nokogiri::XML.parse(response)
release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml)
release_group.first_release_date.should == Date.new(1995, 4, 30)
expect(release_group.first_release_date).to eq Date.new(1995, 4, 30)
end
end
end
end
end
end

0 comments on commit ad4a53f

Please sign in to comment.