From 4a3fbd975a480e9697999b9ef3219cea06d47fb7 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 20:52:06 +0100 Subject: [PATCH 01/12] Update bindings tests --- spec/bindings/recording_search_spec.rb | 41 ++++++++- spec/bindings/relations_spec.rb | 65 ++++++++----- spec/bindings/release_group_search_spec.rb | 26 ++++-- spec/bindings/release_spec.rb | 102 ++++++++++++++++++--- 4 files changed, 190 insertions(+), 44 deletions(-) diff --git a/spec/bindings/recording_search_spec.rb b/spec/bindings/recording_search_spec.rb index d8d93d4..d484f24 100644 --- a/spec/bindings/recording_search_spec.rb +++ b/spec/bindings/recording_search_spec.rb @@ -2,12 +2,45 @@ describe MusicBrainz::Bindings::RecordingSearch do describe '.parse' do + let(:response) { + <<-XML + + + + King Fred + + + + Too Much Joy + + + + + + Green Eggs and Crack + + + + + + XML + } + let(:xml) { + Nokogiri::XML.parse(response).remove_namespaces! + } + let(:metadata) { + described_class.parse(xml.xpath('/metadata')) + } + it "gets correct Recording data" do - response = 'King FredToo Much JoyGreen Eggs and Crack' - expect(described_class.parse(Nokogiri::XML.parse(response).remove_namespaces!.xpath('/metadata'))).to eq [ + expect(metadata).to eq [ { - 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 + 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, } ] end diff --git a/spec/bindings/relations_spec.rb b/spec/bindings/relations_spec.rb index 7fabf30..dd7ef15 100644 --- a/spec/bindings/relations_spec.rb +++ b/spec/bindings/relations_spec.rb @@ -5,34 +5,55 @@ describe 'attributes' do describe 'urls' do context '1 url for relation type' do - it 'returns a string' do - xml = Nokogiri::XML.parse( - %Q{ - - https://plus.google.com/+Madonna - - } - ) + let(:response) { + <<-XML + + + + https://plus.google.com/+Madonna + + + + XML + } + let(:xml) { + Nokogiri::XML.parse(response) + } + let(:artist) { + described_class.parse(xml.xpath('./artist')) + } - expect(described_class.parse(xml.xpath('./artist'))[:urls][:social_network]).to eq 'https://plus.google.com/+Madonna' + it 'returns a string' do + expect(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( - %Q{ - - https://plus.google.com/+Madonna - - - https://www.facebook.com/madonna - - } - ) + let(:response) { + <<-XML + + + + https://plus.google.com/+Madonna + + + https://www.facebook.com/madonna + + + + XML + } + let(:xml) { + Nokogiri::XML.parse(response) + } + let(:artist) { + described_class.parse(xml.xpath('./artist')) + } - expect(described_class.parse(xml.xpath('./artist'))[:urls][:social_network]).to eq [ - 'https://plus.google.com/+Madonna', 'https://www.facebook.com/madonna' + it 'returns an array' do + expect(artist[:urls][:social_network]).to eq [ + 'https://plus.google.com/+Madonna', + 'https://www.facebook.com/madonna', ] end end diff --git a/spec/bindings/release_group_search_spec.rb b/spec/bindings/release_group_search_spec.rb index ad20ec7..eee5348 100644 --- a/spec/bindings/release_group_search_spec.rb +++ b/spec/bindings/release_group_search_spec.rb @@ -3,19 +3,33 @@ describe MusicBrainz::Bindings::ReleaseGroupSearch do describe '.parse' do let(:response) { - 'Empire' + <<-XML + + + + Empire + + + + XML + } + let(:xml) { + Nokogiri::XML.parse(response) } let(:metadata) { - described_class.parse(Nokogiri::XML.parse(response).remove_namespaces!.xpath('/metadata')) + described_class.parse(xml.remove_namespaces!.xpath('/metadata')) } it "gets correct release group data" do - expect(metadata).to eq([ + expect(metadata).to eq [ { - id: '246bc928-2dc8-35ba-80ee-7a0079de1632', mbid: '246bc928-2dc8-35ba-80ee-7a0079de1632', - title: 'Empire', type: 'Single', score: 100 + id: '246bc928-2dc8-35ba-80ee-7a0079de1632', + mbid: '246bc928-2dc8-35ba-80ee-7a0079de1632', + title: 'Empire', + type: 'Single', + score: 100, } - ]) + ] end end end diff --git a/spec/bindings/release_spec.rb b/spec/bindings/release_spec.rb index d506e7e..49ace9c 100644 --- a/spec/bindings/release_spec.rb +++ b/spec/bindings/release_spec.rb @@ -5,34 +5,112 @@ describe 'attributes' do describe 'format' do context 'single cd' do + let(:response) { + <<-XML + + + + CD + + + + XML + } + let(:xml) { + Nokogiri::XML.parse(response) + } + let(:release) { + described_class.parse(xml) + } + it 'returns CD' do - response = 'CD' - xml = Nokogiri::XML.parse(response) - expect(described_class.parse(xml)[:format]).to eq 'CD' + expect(release[:format]).to eq 'CD' end end context 'multiple cds' do + let(:response) { + <<-XML + + + + CD + + + + bonus disc + CD + + + + XML + } + let(:xml) { + Nokogiri::XML.parse(response) + } + let(:release) { + described_class.parse(xml) + } + it 'returns 2xCD' do - response = 'CDbonus discCD' - xml = Nokogiri::XML.parse(response) - expect(described_class.parse(xml)[:format]).to eq '2xCD' + expect(release[:format]).to eq '2xCD' end end context 'different formats' do + let(:response) { + <<-XML + + + + DVD + + + CD + + + + XML + } + let(:xml) { + Nokogiri::XML.parse(response) + } + let(:release) { + described_class.parse(xml) + } + it 'returns DVD + CD' do - response = 'DVDCD' - xml = Nokogiri::XML.parse(response) - expect(described_class.parse(xml)[:format]).to eq 'DVD + CD' + expect(release[:format]).to eq 'DVD + CD' end end context 'different formats plus multiple mediums with same format' do + let(:response) { + <<-XML + + + + CD + + + CD + + + DVD + + + + XML + } + let(:xml) { + Nokogiri::XML.parse(response) + } + let(:release) { + described_class.parse(xml) + } + it 'returns 2xCD + DVD' do - response = 'CDCDDVD' - xml = Nokogiri::XML.parse(response) - expect(described_class.parse(xml)[:format]).to eq '2xCD + DVD' + expect(release[:format]).to eq '2xCD + DVD' end end end From 6a538b6c22c5a193e2b95ab85c6228d1e0f2fdfb Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 21:39:21 +0100 Subject: [PATCH 02/12] Update artist tests, use fixtures --- ...d_69b39eab-6577-46a4-a9f5-817839092033.xml | 168 +++++++ ...s_69b39eab-6577-46a4-a9f5-817839092033.xml | 412 ++++++++++++++++++ spec/fixtures/artist/search.xml | 67 --- spec/fixtures/artist/search_kasabian.xml | 104 +++++ spec/models/artist_spec.rb | 118 +++-- spec/spec_helper.rb | 2 + spec/support/mock_helpers.rb | 11 + 7 files changed, 772 insertions(+), 110 deletions(-) create mode 100644 spec/fixtures/artist/find_69b39eab-6577-46a4-a9f5-817839092033.xml create mode 100644 spec/fixtures/artist/release_groups_69b39eab-6577-46a4-a9f5-817839092033.xml delete mode 100644 spec/fixtures/artist/search.xml create mode 100644 spec/fixtures/artist/search_kasabian.xml create mode 100644 spec/support/mock_helpers.rb diff --git a/spec/fixtures/artist/find_69b39eab-6577-46a4-a9f5-817839092033.xml b/spec/fixtures/artist/find_69b39eab-6577-46a4-a9f5-817839092033.xml new file mode 100644 index 0000000..9601d86 --- /dev/null +++ b/spec/fixtures/artist/find_69b39eab-6577-46a4-a9f5-817839092033.xml @@ -0,0 +1,168 @@ + + + + Kasabian + Kasabian + + 0000000106652390 + + GB + + United Kingdom + United Kingdom + + GB + + + + Leicester + Leicester + + GB-LCE + + + + 1997 + + + + https://www.allmusic.com/artist/mn0000361541 + forward + + + https://www.bbc.co.uk/music/artists/69b39eab-6577-46a4-a9f5-817839092033 + forward + 2020-11-19 + true + + + https://www.discogs.com/artist/235133 + forward + + + https://open.spotify.com/artist/11wRdbnoYqRddKBrpHt4Ue + forward + + + https://www.deezer.com/artist/1247 + forward + + + https://commons.wikimedia.org/wiki/File:Kasabian_at_Brixton_Academy_2009.jpg + forward + + + https://www.imdb.com/name/nm1868442/ + forward + + + https://www.last.fm/music/Kasabian + forward + + + https://genius.com/artists/Kasabian + forward + + + https://muzikum.eu/en/kasabian/lyrics + forward + + + https://myspace.com/kasabian + forward + + + https://www.kasabian.co.uk/ + forward + + + http://id.loc.gov/authorities/names/n2005068609 + forward + + + https://catalogue.bnf.fr/ark:/12148/cb145796883 + forward + + + https://d-nb.info/gnd/10336310-5 + forward + + + https://rateyourmusic.com/artist/kasabian + forward + + + http://www.worldcat.org/identities/lccn-n2005068609/ + forward + true + + + https://play.google.com/store/music/artist?id=A2chuocuj6o6rswcqvmb6h2fzn4 + forward + 2020-10-12 + true + + + https://itunes.apple.com/us/artist/id20478838 + forward + + + https://secondhandsongs.com/artist/25440 + forward + + + https://twitter.com/kasabianhq + forward + + + https://www.facebook.com/kasabian + forward + + + https://www.instagram.com/kasabianofficial/ + forward + + + https://www.songkick.com/artists/175029 + forward + + + https://soundcloud.com/kasabian + forward + + + https://music.apple.com/gb/artist/20478838 + forward + + + https://music.youtube.com/channel/UCmJDzMXYTdyjtp5VNONQRKg + forward + + + https://tidal.com/artist/1556 + forward + + + http://viaf.org/viaf/138707200 + forward + + + https://www.wikidata.org/wiki/Q272517 + forward + + + https://www.youtube.com/channel/UC_niYTRmUdRvVNdGYIA1vcg + forward + + + https://www.youtube.com/user/KasabianVEVO + forward + + + https://www.youtube.com/user/KasabianTour + forward + true + + + + diff --git a/spec/fixtures/artist/release_groups_69b39eab-6577-46a4-a9f5-817839092033.xml b/spec/fixtures/artist/release_groups_69b39eab-6577-46a4-a9f5-817839092033.xml new file mode 100644 index 0000000..b12275c --- /dev/null +++ b/spec/fixtures/artist/release_groups_69b39eab-6577-46a4-a9f5-817839092033.xml @@ -0,0 +1,412 @@ + + + + + Kasabian + 2004-07-23 + Album + + + https://www.allmusic.com/album/mw0000261419 + forward + + + https://www.discogs.com/master/89974 + forward + + + https://genius.com/albums/Kasabian/Kasabian + forward + + + https://rateyourmusic.com/release/album/kasabian/kasabian_f2/ + forward + + + https://www.bbc.co.uk/music/reviews/64w6 + forward + + + https://www.wikidata.org/wiki/Q1535408 + forward + + + + + Empire + 2006-08-25 + Album + + + https://www.allmusic.com/album/mw0000445384 + forward + + + https://www.discogs.com/master/89980 + forward + + + https://www.bbc.co.uk/music/reviews/389x + forward + + + https://www.wikidata.org/wiki/Q748870 + forward + + + + + West Ryder Pauper Lunatic Asylum + 2009-04-06 + Album + + + https://www.allmusic.com/album/mw0000818188 + forward + + + https://www.discogs.com/master/125155 + forward + + + https://rateyourmusic.com/release/album/kasabian/west_ryder_pauper_lunatic_asylum/ + forward + + + https://www.bbc.co.uk/music/reviews/bn3n + forward + + + https://www.wikidata.org/wiki/Q1932087 + forward + + + + + Velociraptor! + 2011-09-16 + Album + + + https://www.allmusic.com/album/mw0002169520 + forward + + + https://www.discogs.com/master/368845 + forward + + + https://www.bbc.co.uk/music/reviews/vcm5 + forward + + + https://www.wikidata.org/wiki/Q1951148 + forward + + + + + 48:13 + 2014-06-09 + Album + + + https://www.allmusic.com/album/mw0002668450 + forward + + + https://www.discogs.com/master/696552 + forward + + + https://www.wikidata.org/wiki/Q16735842 + forward + + + + + For Crying Out Loud + 2017-05-05 + Album + + + https://www.discogs.com/master/1174013 + forward + + + https://www.wikidata.org/wiki/Q28951744 + forward + + + + + The Alchemist’s Euphoria + 2022-08-02 + Album + + + https://www.discogs.com/master/2743487 + forward + + + https://www.offiziellecharts.de/album-details-497860 + forward + + + https://www.plattentests.de/rezi.php?show=18727 + forward + + + https://www.wikidata.org/wiki/Q113456602 + forward + + + + + Happenings + 2024-07-05 + Album + + + Empire / West Ryder Pauper Lunatic Asylum + 2011 + Album + + Compilation + + + + Live at Brixton Academy + 2005-07-04 + Album + + Live + + + + https://www.wikidata.org/wiki/Q1949413 + forward + + + https://en.wikipedia.org/wiki/Live_from_Brixton_Academy + forward + + + + + iTunes Festival: London 2007 + 2007-08-03 + Album + + Live + + + + https://www.wikidata.org/wiki/Q3789916 + forward + + + + + Live in London + 2011 + Album + + Live + + + + Reason Is Treason + 2004-02-23 + Single + + + https://www.discogs.com/master/125172 + forward + + + + + Club Foot + 2004-05-10 + Single + + + https://www.discogs.com/master/125150 + forward + + + https://www.wikidata.org/wiki/Q2327037 + forward + + + https://en.wikipedia.org/wiki/Club_Foot_(song) + forward + + + + + L.S.F. + 2004-08-17 + Single + + + https://www.discogs.com/master/125163 + forward + + + https://www.wikidata.org/wiki/Q607352 + forward + + + https://en.wikipedia.org/wiki/L.S.F._(song) + forward + + + + + Processed Beats + 2004-10-13 + Single + + + https://www.wikidata.org/wiki/Q1463837 + forward + + + + + Cutt Off + 2005-01-03 + Single + + + https://www.wikidata.org/wiki/Q3699798 + forward + + + https://en.wikipedia.org/wiki/Cutt_Off + forward + + + + + Empire + 2006-07-24 + Single + + + https://www.discogs.com/master/125174 + forward + + + https://www.wikidata.org/wiki/Q1463830 + forward + + + https://en.wikipedia.org/wiki/Empire_(Kasabian_song) + forward + + + + + Shoot the Runner + 2006-11-06 + Single + + + https://www.discogs.com/master/125179 + forward + + + https://www.wikidata.org/wiki/Q1463795 + forward + + + + + Me Plus One + 2007-01-29 + Single + + + https://www.discogs.com/master/125175 + forward + + + https://www.wikidata.org/wiki/Q1120372 + forward + + + https://en.wikipedia.org/wiki/Me_Plus_One + forward + + + + + Fire + 2009-05-28 + Single + + + https://www.wikidata.org/wiki/Q783320 + forward + + + https://en.wikipedia.org/wiki/Fire_(Kasabian_song) + forward + + + + + Where Did All the Love Go? + 2009-08-10 + Single + + + https://www.wikidata.org/wiki/Q4019433 + forward + + + https://en.wikipedia.org/wiki/Where_Did_All_the_Love_Go%3F + forward + + + + + Underdog + 2009-10-22 + Single + + + https://www.wikidata.org/wiki/Q4004596 + forward + + + + + Days Are Forgotten + 2011-08 + Single + + + https://www.discogs.com/master/381246 + forward + + + https://www.wikidata.org/wiki/Q3703828 + forward + + + https://en.wikipedia.org/wiki/Days_Are_Forgotten + forward + + + + + Re‐Wired + 2011-11-21 + Single + + + diff --git a/spec/fixtures/artist/search.xml b/spec/fixtures/artist/search.xml deleted file mode 100644 index fae1233..0000000 --- a/spec/fixtures/artist/search.xml +++ /dev/null @@ -1,67 +0,0 @@ - - - - - Chris Martin - Martin, Chris - Reggae / Dancehall singer Christopher Martin - - false - - - - Chris Martin - Martin, Chris - Illustrator - - false - - - - Chris Martin - Martin, Chris - male - GB - - United Kingdom - United Kingdom - - lead singer of Coldplay - - 00280536565 - - - 1977-03-02 - false - - - Christopher Anthony John Martin - - - - pop/rock - - - - - Chris Martin - Martin, Chris - hip hop producer DJ Premier, DJ Primo - - 1965-05-05 - false - - - Christopher Edward Martin - - - - Chris Martin - Martin, Chris - Disney songwriter/composer - - false - - - - diff --git a/spec/fixtures/artist/search_kasabian.xml b/spec/fixtures/artist/search_kasabian.xml new file mode 100644 index 0000000..83c0495 --- /dev/null +++ b/spec/fixtures/artist/search_kasabian.xml @@ -0,0 +1,104 @@ + + + + + Kasabian + Kasabian + GB + + United Kingdom + United Kingdom + + false + + + + Leicester + Leicester + + false + + + + 0000000106652390 + + + 1997 + false + + + + rock + + + alternative rock + + + british + + + indie rock + + + indie + + + alternative + + + neo-psychedelia + + + alternative dance + + + dance-punk + + + indietronica + + + rock and indie + + + post-baggy + + + + + Mother Kasabian + Mother Kasabian + + Stockholm + Stockholm + + false + + + + 2011 + false + + + + psychedelic + + + experimental + + + metal + + + doom + + + stoner + + + sludge + + + + + diff --git a/spec/models/artist_spec.rb b/spec/models/artist_spec.rb index 12b35a9..62bc476 100644 --- a/spec/models/artist_spec.rb +++ b/spec/models/artist_spec.rb @@ -1,58 +1,90 @@ require "spec_helper" describe MusicBrainz::Artist do - it "gets no exception while loading artist info" do - expect { - MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033') - }.to_not raise_error(Exception) - end + describe '.search' do + before(:each) { + mock url: 'http://musicbrainz.org/ws/2/artist?query=artist:"Kasabian"&limit=10', + fixture: 'artist/search_kasabian.xml' + } + let(:matches) { + MusicBrainz::Artist.search('Kasabian') + } - it "gets correct instance" do - artist = MusicBrainz::Artist.find_by_name('Kasabian') - expect(artist).to be_instance_of(MusicBrainz::Artist) + it "searches artist by name" do + 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 + expect(matches.length).to be >= 2 + expect(matches[0][:score]).to eq 100 + expect(matches[0][:id]).to eq "69b39eab-6577-46a4-a9f5-817839092033" + expect(matches[1][:score]).to eq 73 + expect(matches[1][:id]).to eq "d76aed40-1332-44db-9b19-aee7ec17464b" + end end - it "searches artist by name" do - matches = MusicBrainz::Artist.search('Kasabian') - expect(matches).to_not be_empty - expect(matches.first[:name]).to eq("Kasabian") + describe '.find' do + before(:each) { + mock url: 'http://musicbrainz.org/ws/2/artist/69b39eab-6577-46a4-a9f5-817839092033?inc=url-rels', + fixture: 'artist/find_69b39eab-6577-46a4-a9f5-817839092033.xml' + } + let(:artist) { + MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033') + } + + it "gets no exception while loading artist info" do + expect{ artist }.to_not raise_error(Exception) + expect(artist.id).to eq '69b39eab-6577-46a4-a9f5-817839092033' + expect(artist.name).to eq 'Kasabian' + end 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 - 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}) + describe '.find_by_name' do + before(:each) { + mock url: 'http://musicbrainz.org/ws/2/artist?query=artist:"Kasabian"&limit=10', + fixture: 'artist/search_kasabian.xml' + mock url: 'http://musicbrainz.org/ws/2/artist/69b39eab-6577-46a4-a9f5-817839092033?inc=url-rels', + fixture: 'artist/find_69b39eab-6577-46a4-a9f5-817839092033.xml' + } + let(:artist) { + MusicBrainz::Artist.find_by_name('Kasabian') + } - matches = MusicBrainz::Artist.search('Chris Martin') + it "gets correct instance" do + expect(artist).to be_instance_of(MusicBrainz::Artist) + end - 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 + expect(artist.id).to eq "69b39eab-6577-46a4-a9f5-817839092033" + end - it "gets correct result by name" do - artist = MusicBrainz::Artist.find_by_name('Kasabian') - expect(artist.id).to eq "69b39eab-6577-46a4-a9f5-817839092033" - end + it "gets correct artist data" do + 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 data" do - artist = MusicBrainz::Artist.find_by_name('Kasabian') - 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 + describe '#release_groups' do + before(:each) { + mock url: 'http://musicbrainz.org/ws/2/release-group?artist=69b39eab-6577-46a4-a9f5-817839092033&inc=url-rels', + fixture: 'artist/release_groups_69b39eab-6577-46a4-a9f5-817839092033.xml' + } + let(:release_groups) { + artist.release_groups + } - it "gets correct artist's release groups" do - release_groups = MusicBrainz::Artist.find_by_name('Kasabian').release_groups - 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' + it "gets correct artist's release groups" do + release_groups = MusicBrainz::Artist.find_by_name('Kasabian').release_groups + expect(release_groups.length).to be >= 16 + expect(release_groups.first.id).to eq "5547285f-578f-3236-85aa-b65cc0923b58" + expect(release_groups.first.type).to eq "Single" + expect(release_groups.first.title).to eq "Reason Is Treason" + expect(release_groups.first.first_release_date).to eq Date.new(2004, 2, 23) + expect(release_groups.first.urls[:discogs]).to eq 'https://www.discogs.com/master/125172' + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7c0779e..e280957 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,8 +3,10 @@ require "musicbrainz" require "pry" require "awesome_print" +require_relative 'support/mock_helpers' RSpec.configure do |c| + include MockHelpers c.order = 'random' end diff --git a/spec/support/mock_helpers.rb b/spec/support/mock_helpers.rb new file mode 100644 index 0000000..77ccc6a --- /dev/null +++ b/spec/support/mock_helpers.rb @@ -0,0 +1,11 @@ +module MockHelpers + def mock(url:, fixture:) + spec_path = File.join(File.dirname(__FILE__), "..") + file_path = File.join(spec_path, "fixtures", fixture) + response = File.open(file_path).read + + allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents) + .with(url) + .and_return({ status: 200, body: response}) + end +end From c7b30c5c7da6feed0ed973bd1c88cdb66f7e1db4 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 21:52:17 +0100 Subject: [PATCH 03/12] Update base model spec --- spec/models/base_model_spec.rb | 54 ++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/spec/models/base_model_spec.rb b/spec/models/base_model_spec.rb index f29e0c4..e6f0e9d 100644 --- a/spec/models/base_model_spec.rb +++ b/spec/models/base_model_spec.rb @@ -3,38 +3,68 @@ describe MusicBrainz::BaseModel do describe '#validate_type' do describe 'Date' do + let(:xml) { + Nokogiri::XML.parse(response) + } + let(:bindings) { + MusicBrainz::Bindings::ReleaseGroup.parse(xml) + } + let(:release_group) { + MusicBrainz::ReleaseGroup.new(bindings) + } + context 'nil value' do + let(:response) { + <<-XML + + + + XML + } + it 'returns 2030-12-31' do - response = '' - xml = Nokogiri::XML.parse(response) - release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml) expect(release_group.first_release_date).to eq Date.new(2030, 12, 31) end end context 'year only' do + let(:response) { + <<-XML + + 1995 + + XML + } + it 'returns 1995-12-31' do - response = '1995' - xml = Nokogiri::XML.parse(response) - release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml) expect(release_group.first_release_date).to eq Date.new(1995, 12, 31) end end context 'year and month only' do + let(:response) { + <<-XML + + 1995-04 + + XML + } + it 'returns 1995-04-30' do - response = '1995-04' - xml = Nokogiri::XML.parse(response) - release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml) expect(release_group.first_release_date).to eq Date.new(1995, 4, 30) end end context 'year, month and day' do + let(:response) { + <<-XML + + 1995-04-30 + + XML + } + it 'returns 1995-04-30' do - response = '1995-04-30' - xml = Nokogiri::XML.parse(response) - release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml) expect(release_group.first_release_date).to eq Date.new(1995, 4, 30) end end From 6a7affb9106d298cb415c97859ca8747a8986447 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 21:59:57 +0100 Subject: [PATCH 04/12] Update recording spec --- ...d_b3015bab-1540-4d4e-9f30-14872a1525f7.xml | 8 + .../search_bound_for_the_floor_local_h.xml | 647 ++++++++++++++++++ spec/models/recording_spec.rb | 30 +- 3 files changed, 676 insertions(+), 9 deletions(-) create mode 100644 spec/fixtures/recording/find_b3015bab-1540-4d4e-9f30-14872a1525f7.xml create mode 100644 spec/fixtures/recording/search_bound_for_the_floor_local_h.xml diff --git a/spec/fixtures/recording/find_b3015bab-1540-4d4e-9f30-14872a1525f7.xml b/spec/fixtures/recording/find_b3015bab-1540-4d4e-9f30-14872a1525f7.xml new file mode 100644 index 0000000..3fdb56b --- /dev/null +++ b/spec/fixtures/recording/find_b3015bab-1540-4d4e-9f30-14872a1525f7.xml @@ -0,0 +1,8 @@ + + + + Empire + 233013 + 2006-08-25 + + diff --git a/spec/fixtures/recording/search_bound_for_the_floor_local_h.xml b/spec/fixtures/recording/search_bound_for_the_floor_local_h.xml new file mode 100644 index 0000000..83479c1 --- /dev/null +++ b/spec/fixtures/recording/search_bound_for_the_floor_local_h.xml @@ -0,0 +1,647 @@ + + + + + Bound for the Floor + + + Local H + + Local H + Local H + + Local H. + + + + + 1997-04-28 + + + Blackrock + Official + + + Various Artists + + Various Artists + Various Artists + add compilations to this artist + + + + + Blackrock + Album + + Soundtrack + + + 1997-04-28 + AU + + + 1997-04-28 + + Australia + Australia + + AU + + + + + + 15 + + 1 + + + 8 + Bound for the Floor + + + + + + + + + Bound for the Floor + 224000 + + + Local H + + Local H + Local H + + Local H. + + + + + 2001 + + + Killer Buzz + Official + + + Various Artists + + Various Artists + Various Artists + add compilations to this artist + + + + + Killer Buzz + Album + + Compilation + + + 2001 + US + + + 2001 + + United States + United States + + US + + + + + + 32 + + 1 + CD + + + 11 + Bound for the Floor + 224000 + + + + + + + + + Bound for the Floor + 230773 + + + Local H + + Local H + Local H + + Local H. + + + + + + + Unplugged & Burnt Out + Official + + + Various Artists + + Various Artists + Various Artists + add compilations to this artist + + + + + Unplugged & Burnt Out + Album + + Compilation + + + + 12 + + 1 + + + 8 + Bound for the Floor + 230773 + + + + + + + + + Bound for the Floor + 231880 + + + Local H + + Local H + Local H + + Local H. + + + + + 1997-11-25 + + + WAAF 107.3 FM: Royal Flush: Live On‐air + Official + + + Various Artists + + Various Artists + Various Artists + add compilations to this artist + + + + + WAAF 107.3 FM: Royal Flush: Live On‐air + Album + + Live + + + 1997-11-25 + US + + + 1997-11-25 + + United States + United States + + US + + + + + + 26 + + 1 + CD + + + 25 + Bound for the Floor + 231880 + + + + + + + + + Bound for the Floor + 218360 + live + + + Local H + + Local H + Local H + + Local H. + + + + + 1998 + + + The Point Platinum Version 1.0 + Official + + + Various Artists + + Various Artists + Various Artists + add compilations to this artist + + + + + The Point Platinum Version 1.0 + Album + + Compilation + + + 1998 + US + + + 1998 + + United States + United States + + US + + + + + + 32 + + 2 + CD + + + 6 + Bound for the Floor + 218360 + + + + + + + + + Bound for the Floor + 223866 + + + Local H + + Local H + Local H + + Local H. + + + + + + + Radio 104 WMRQ Back to School + + + Various Artists + + Various Artists + Various Artists + add compilations to this artist + + + + + Radio 104 WMRQ Back to School + Album + + Compilation + + + + 15 + + 1 + + + 2 + Bound for the Floor + 223866 + + + + + + + + + Bound For the Floor + 225000 + + + Local H + + Local H + Local H + + Local H. + + + + + 2018-02-06 + + + Live In Europe + Official + + + Local H + + Local H + Local H + + + + + Live In Europe + Album + + Live + + + 2018-02-06 + US + + + 2018-02-06 + + United States + United States + + US + + + + + + 18 + + 1 + CD + + + 9 + Bound For the Floor + 225000 + + + + + + + + + Bound for the Floor + 223947 + + + Local H + + Local H + Local H + + Local H. + + + + + 1996 + + + Promo Only: Modern Rock Radio, July 1996 + Promotion + + + Various Artists + + Various Artists + Various Artists + add compilations to this artist + + + + + Promo Only: Modern Rock Radio, July 1996 + Album + + Compilation + + + 1996 + US + + + 1996 + + United States + United States + + US + + + + + + 19 + + 1 + CD + + + 15 + Bound for the Floor + 223947 + + + + + + + + + Bound For The Floor + 213000 + + + Local H + + Local H + Local H + + Local H. + + + + + 1999-06-26 + + + Live at The Metro / Smart Bar + Official + + + Local H + + Local H + Local H + + + + + Live at The Metro / Smart Bar + Album + + Live + + + 1999-06-26 + US + + + 1999-06-26 + + United States + United States + + US + + + + + + 19 + + 1 + CD + + + 3 + Bound For The Floor + 213000 + + + + + + + + + Bound For The Floor + 381000 + + + Local H + + Local H + Local H + + Local H. + + + + + 2006-02-11 + + + Live at Durty Nellies + Official + + + Local H + + Local H + Local H + + + + + Live at Durty Nellies + Album + + Live + + + 2006-02-11 + US + + + 2006-02-11 + + United States + United States + + US + + + + + + 17 + + 2 + CD + + + 2 + Bound For The Floor + 381000 + + + + + + + + + diff --git a/spec/models/recording_spec.rb b/spec/models/recording_spec.rb index 6a5f470..24dc1ad 100644 --- a/spec/models/recording_spec.rb +++ b/spec/models/recording_spec.rb @@ -2,27 +2,39 @@ describe MusicBrainz::Recording do describe '.find' do + before(:each) { + mock url: 'http://musicbrainz.org/ws/2/recording/b3015bab-1540-4d4e-9f30-14872a1525f7?', + fixture: 'recording/find_b3015bab-1540-4d4e-9f30-14872a1525f7.xml' + } + let(:recording) { + MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7") + } + it "gets no exception while loading release info" do - expect { - MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7") - }.to_not raise_error(Exception) + expect{ recording }.to_not raise_error(Exception) end it "gets correct instance" do - track = MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7") - expect(track).to be_an_instance_of(MusicBrainz::Recording) + expect(recording).to be_an_instance_of(MusicBrainz::Recording) end it "gets correct track data" do - track = MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7") - expect(track.title).to eq "Empire" + expect(recording.title).to eq "Empire" end end describe '.search' do + before(:each) { + mock url: 'http://musicbrainz.org/ws/2/recording?query=recording:"Bound+for+the+floor" AND artist:"Local+H"&limit=10', + fixture: 'recording/search_bound_for_the_floor_local_h.xml' + } + let(:matches) { + MusicBrainz::Recording.search('Bound for the floor', 'Local H') + } + it "searches tracks (aka recordings) by artist name and title" do - matches = MusicBrainz::Recording.search('Bound for the floor', 'Local H') - expect(matches.length).to be > 0 + expect(matches).to_not be_empty + expect(matches.first[:id]).to eq 'bb940e26-4265-4909-b128-4906d8b1079e' expect(matches.first[:title]).to eq "Bound for the Floor" expect(matches.first[:artist]).to eq "Local H" end From cc0a9f6bbd435b41b84b4fbefe4bc5de5592bd77 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 22:03:07 +0100 Subject: [PATCH 05/12] Update track spec --- spec/models/track_spec.rb | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/spec/models/track_spec.rb b/spec/models/track_spec.rb index 14d4a5f..f667543 100644 --- a/spec/models/track_spec.rb +++ b/spec/models/track_spec.rb @@ -1,23 +1,27 @@ require "spec_helper" describe MusicBrainz::Track do - describe '.find' do - it "gets no exception while loading release info" do - expect { - MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") - }.to_not raise_error(Exception) - end + describe '.find' do + before(:each) { + mock url: 'http://musicbrainz.org/ws/2/recording/b3015bab-1540-4d4e-9f30-14872a1525f7?', + fixture: 'recording/find_b3015bab-1540-4d4e-9f30-14872a1525f7.xml' + } + let(:track) { + MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") + } - it "gets correct instance" do - track = MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") - expect(track).to be_an_instance_of(MusicBrainz::Track) - end + it "gets no exception while loading release info" do + expect { track }.to_not raise_error(Exception) + end - it "gets correct track data" do - track = MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") - expect(track.recording_id).to eq "b3015bab-1540-4d4e-9f30-14872a1525f7" - expect(track.title).to eq "Empire" - expect(track.length).to eq 233013 - end - end + it "gets correct instance" do + expect(track).to be_an_instance_of(MusicBrainz::Track) + end + + it "gets correct track data" do + expect(track.recording_id).to eq "b3015bab-1540-4d4e-9f30-14872a1525f7" + expect(track.title).to eq "Empire" + expect(track.length).to eq 233013 + end + end end From 026879e8bebeed52280c6816a0c5365583141eef Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 22:20:26 +0100 Subject: [PATCH 06/12] Update release group spec --- spec/fixtures/release/list.xml | 344 ------------------ ...t_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml | 344 ++++++++++++++++++ ..._6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml} | 0 ...{search.xml => search_kasabian_empire.xml} | 0 .../search_kasabian_empire_album.xml | 117 ++++++ spec/models/release_group_spec.rb | 68 ++-- 6 files changed, 497 insertions(+), 376 deletions(-) delete mode 100644 spec/fixtures/release/list.xml create mode 100644 spec/fixtures/release/list_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml rename spec/fixtures/release_group/{entity.xml => find_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml} (100%) rename spec/fixtures/release_group/{search.xml => search_kasabian_empire.xml} (100%) create mode 100644 spec/fixtures/release_group/search_kasabian_empire_album.xml diff --git a/spec/fixtures/release/list.xml b/spec/fixtures/release/list.xml deleted file mode 100644 index e0329da..0000000 --- a/spec/fixtures/release/list.xml +++ /dev/null @@ -1,344 +0,0 @@ - - - - - Empire - Official - normal - Jewel Case - - eng - - - - Empire - 2006-08-28 - Album - - 2006 - XE - - - 2006 - - Europe - Europe - - XE - - - - - 828768934227 - - false - 0 - false - false - - - - 1 - CD - - - - - - Empire - Official - normal - - eng - - - - Empire - 2006-08-28 - Album - - 2006-08-28 - GB - - - 2006-08-28 - - United Kingdom - United Kingdom - - GB - - - - - B000H49P28 - - false - 0 - false - false - - - - 1 - - - - - - Empire - Official - normal - - eng - - - - Empire - 2006-08-28 - Album - - 2006-09-19 - US - - - 2006-09-19 - - United States - United States - - US - - - - - 828768832325 - B000HEVYQS - - false - 0 - false - false - - - - 1 - CD - - - - - - Empire - Official - normal - - eng - - - - Empire - 2006-08-28 - Album - - 2006-09-22 - XW - - - 2006-09-22 - - [Worldwide] - [Worldwide] - - XW - - - - - - false - 0 - false - false - - - - 1 - Digital Media - - - - - - Empire - Official - normal - - eng - - - - Empire - 2006-08-28 - Album - - 2006-08-28 - GB - - - 2006-08-28 - - United Kingdom - United Kingdom - - GB - - - - - 828768849927 - B000H49P28 - - false - 0 - false - false - - - - 1 - CD - - - - - - Empire - Official - normal - Jewel Case - - eng - - - - Empire - 2006-08-28 - Album - - 2006 - GB - - - 2006 - - United Kingdom - United Kingdom - - GB - - - - - 828768934227 - - false - 0 - false - false - - - - 1 - CD - - - - - - Empire - Official - normal - - eng - - - - Empire - 2006-08-28 - Album - - 2006-12-20 - JP - - - 2006-12-20 - - Japan - Japan - - JP - - - - - 4988017645901 - B000K2QL9W - - false - 0 - false - false - - - - 1 - CD - - - - - - Empire - Official - normal - Jewel Case - - eng - - - - Empire - 2006-08-28 - Album - - 2006-12-18 - GB - - - 2006-12-18 - - United Kingdom - United Kingdom - - GB - - - - - - true - 1 - true - false - - - - 1 - Digital Media - - - - - - \ No newline at end of file diff --git a/spec/fixtures/release/list_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml b/spec/fixtures/release/list_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml new file mode 100644 index 0000000..c42a9cb --- /dev/null +++ b/spec/fixtures/release/list_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml @@ -0,0 +1,344 @@ + + + + + Empire + Official + normal + Jewel Case + + eng + + + + Empire + 2006-08-28 + Album + + 2006 + XE + + + 2006 + + Europe + Europe + + XE + + + + + 828768934227 + + false + 0 + false + false + + + + 1 + CD + + + + + + Empire + Official + normal + + eng + + + + Empire + 2006-08-28 + Album + + 2006-08-28 + GB + + + 2006-08-28 + + United Kingdom + United Kingdom + + GB + + + + + B000H49P28 + + false + 0 + false + false + + + + 1 + + + + + + Empire + Official + normal + + eng + + + + Empire + 2006-08-28 + Album + + 2006-09-19 + US + + + 2006-09-19 + + United States + United States + + US + + + + + 828768832325 + B000HEVYQS + + false + 0 + false + false + + + + 1 + CD + + + + + + Empire + Official + normal + + eng + + + + Empire + 2006-08-28 + Album + + 2006-09-22 + XW + + + 2006-09-22 + + [Worldwide] + [Worldwide] + + XW + + + + + + false + 0 + false + false + + + + 1 + Digital Media + + + + + + Empire + Official + normal + + eng + + + + Empire + 2006-08-28 + Album + + 2006-08-28 + GB + + + 2006-08-28 + + United Kingdom + United Kingdom + + GB + + + + + 828768849927 + B000H49P28 + + false + 0 + false + false + + + + 1 + CD + + + + + + Empire + Official + normal + Jewel Case + + eng + + + + Empire + 2006-08-28 + Album + + 2006 + GB + + + 2006 + + United Kingdom + United Kingdom + + GB + + + + + 828768934227 + + false + 0 + false + false + + + + 1 + CD + + + + + + Empire + Official + normal + + eng + + + + Empire + 2006-08-28 + Album + + 2006-12-20 + JP + + + 2006-12-20 + + Japan + Japan + + JP + + + + + 4988017645901 + B000K2QL9W + + false + 0 + false + false + + + + 1 + CD + + + + + + Empire + Official + normal + Jewel Case + + eng + + + + Empire + 2006-08-28 + Album + + 2006-12-18 + GB + + + 2006-12-18 + + United Kingdom + United Kingdom + + GB + + + + + + true + 1 + true + false + + + + 1 + Digital Media + + + + + + diff --git a/spec/fixtures/release_group/entity.xml b/spec/fixtures/release_group/find_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml similarity index 100% rename from spec/fixtures/release_group/entity.xml rename to spec/fixtures/release_group/find_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml diff --git a/spec/fixtures/release_group/search.xml b/spec/fixtures/release_group/search_kasabian_empire.xml similarity index 100% rename from spec/fixtures/release_group/search.xml rename to spec/fixtures/release_group/search_kasabian_empire.xml diff --git a/spec/fixtures/release_group/search_kasabian_empire_album.xml b/spec/fixtures/release_group/search_kasabian_empire_album.xml new file mode 100644 index 0000000..3f47986 --- /dev/null +++ b/spec/fixtures/release_group/search_kasabian_empire_album.xml @@ -0,0 +1,117 @@ + + + + + Empire + 2006-08-25 + Album + + + Kasabian + + Kasabian + Kasabian + + + + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + Empire + Official + + + + + rock + + + alternative rock + + + indie rock + + + indie + + + rock and indie + + + + + Empire / West Ryder Pauper Lunatic Asylum + 2011 + Album + + Compilation + + + + Kasabian + + Kasabian + Kasabian + + + + + + Empire / West Ryder Pauper Lunatic Asylum + Official + + + + + diff --git a/spec/models/release_group_spec.rb b/spec/models/release_group_spec.rb index 3fe425b..577836c 100644 --- a/spec/models/release_group_spec.rb +++ b/spec/models/release_group_spec.rb @@ -1,20 +1,34 @@ require "spec_helper" describe MusicBrainz::ReleaseGroup do + before(:each) { + mock url: 'http://musicbrainz.org/ws/2/release-group/6f33e0f0-cde2-38f9-9aee-2c60af8d1a61?inc=url-rels', + fixture: 'release_group/find_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml' + + mock url: 'http://musicbrainz.org/ws/2/release-group?query=artist:"Kasabian" AND releasegroup:"Empire"&limit=10', + fixture: 'release_group/search_kasabian_empire.xml' + + mock url: 'http://musicbrainz.org/ws/2/release-group?query=artist:"Kasabian" AND releasegroup:"Empire" AND type:"Album"&limit=10', + fixture: 'release_group/search_kasabian_empire_album.xml' + + mock url: 'http://musicbrainz.org/ws/2/release?release-group=6f33e0f0-cde2-38f9-9aee-2c60af8d1a61&inc=media+release-groups&limit=100', + fixture: 'release/list_6f33e0f0-cde2-38f9-9aee-2c60af8d1a61.xml' + } + describe '.find' do + let(:release_group) { + MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61") + } + it "gets no exception while loading release group info" do - expect { - MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61") - }.to_not raise_error(Exception) + expect { release_group }.to_not raise_error(Exception) end it "gets correct instance" do - release_group = MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61") expect(release_group).to be_an_instance_of(MusicBrainz::ReleaseGroup) end it "gets correct release group data" do - release_group = MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61") expect(release_group.id).to eq "6f33e0f0-cde2-38f9-9aee-2c60af8d1a61" expect(release_group.type).to eq "Album" expect(release_group.title).to eq "Empire" @@ -25,13 +39,11 @@ describe '.search' do context 'without type filter' do - it "searches release group by artist name and title" do - response = File.open(File.join(File.dirname(__FILE__), "../fixtures/release_group/search.xml")).read - allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents) - .with('http://musicbrainz.org/ws/2/release-group?query=artist:"Kasabian" AND releasegroup:"Empire"&limit=10') - .and_return({ status: 200, body: response}) + let(:matches) { + MusicBrainz::ReleaseGroup.search('Kasabian', 'Empire') + } - matches = MusicBrainz::ReleaseGroup.search('Kasabian', 'Empire') + it "searches release group by artist name and title" do expect(matches.length).to be > 0 expect(matches.first[:title]).to eq 'Empire' expect(matches.first[:type]).to eq 'Album' @@ -39,8 +51,11 @@ end context 'with type filter' do + let(:matches) { + MusicBrainz::ReleaseGroup.search('Kasabian', 'Empire', 'Album') + } + it "searches release group by artist name and title" do - matches = MusicBrainz::ReleaseGroup.search('Kasabian', 'Empire', 'Album') expect(matches.length).to be > 0 expect(matches.first[:title]).to eq 'Empire' expect(matches.first[:type]).to eq 'Album' @@ -49,34 +64,23 @@ end describe '.find_by_artist_and_title' do + let(:release_group) { + MusicBrainz::ReleaseGroup.find_by_artist_and_title('Kasabian', 'Empire') + } + it "gets first release group by artist name and title" do - response = File.open(File.join(File.dirname(__FILE__), "../fixtures/release_group/search.xml")).read - allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents) - .with('http://musicbrainz.org/ws/2/release-group?query=artist:"Kasabian" AND releasegroup:"Empire"&limit=10') - .and_return({ status: 200, body: response}) - - response = File.open(File.join(File.dirname(__FILE__), "../fixtures/release_group/entity.xml")).read - allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents) - .with('http://musicbrainz.org/ws/2/release-group/6f33e0f0-cde2-38f9-9aee-2c60af8d1a61?inc=url-rels') - .and_return({ status: 200, body: response}) - release_group = MusicBrainz::ReleaseGroup.find_by_artist_and_title('Kasabian', 'Empire') expect(release_group.id).to eq '6f33e0f0-cde2-38f9-9aee-2c60af8d1a61' end end describe '#releases' do - it "gets correct release group's releases" do - allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents) - .with('http://musicbrainz.org/ws/2/release-group/6f33e0f0-cde2-38f9-9aee-2c60af8d1a61?inc=url-rels') - .and_return({ status: 200, body: File.open(File.join(File.dirname(__FILE__), "../fixtures/release_group/entity.xml")).read}) - - allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents) - .with('http://musicbrainz.org/ws/2/release?release-group=6f33e0f0-cde2-38f9-9aee-2c60af8d1a61&inc=media+release-groups&limit=100') - .and_return({ status: 200, body: File.open(File.join(File.dirname(__FILE__), "../fixtures/release/list.xml")).read}) + let(:releases) { + MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61").releases + } - releases = MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61").releases + it "gets correct release group's releases" do expect(releases.length).to be >= 5 - expect(releases.first.id).to eq "30d5e730-ce0a-464d-93e1-7d76e4bb3e31" + expect(releases.first.id).to eq "2225dd4c-ae9a-403b-8ea0-9e05014c778f" expect(releases.first.status).to eq "Official" expect(releases.first.title).to eq "Empire" expect(releases.first.date).to eq Date.new(2006, 8, 28) From bfaa9c5c1ab2a4d31eb7098ef8a9fd960cf45c29 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 22:35:21 +0100 Subject: [PATCH 07/12] Update release spec --- ...d_2225dd4c-ae9a-403b-8ea0-9e05014c778f.xml | 44 +++++ ...d4c-ae9a-403b-8ea0-9e05014c778f_tracks.xml | 151 ++++++++++++++++++ ...d_b94cb547-cf7a-4357-894c-53c3bf33b093.xml | 52 ++++++ ...by_discid_pmzhT6ZlFiwSRCdVwV0eqire5_Y-.xml | 141 ++++++++++++++++ spec/models/release_spec.rb | 95 +++++++---- 5 files changed, 450 insertions(+), 33 deletions(-) create mode 100644 spec/fixtures/release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f.xml create mode 100644 spec/fixtures/release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f_tracks.xml create mode 100644 spec/fixtures/release/find_b94cb547-cf7a-4357-894c-53c3bf33b093.xml create mode 100644 spec/fixtures/release/find_by_discid_pmzhT6ZlFiwSRCdVwV0eqire5_Y-.xml diff --git a/spec/fixtures/release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f.xml b/spec/fixtures/release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f.xml new file mode 100644 index 0000000..65fece9 --- /dev/null +++ b/spec/fixtures/release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f.xml @@ -0,0 +1,44 @@ + + + + Empire + Official + normal + + eng + + + + Empire + 2006-08-25 + Album + + 2006-08-28 + GB + + + 2006-08-28 + + United Kingdom + United Kingdom + + GB + + + + + B000H49P28 + + true + 1 + true + false + + + + 1 + + + + + diff --git a/spec/fixtures/release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f_tracks.xml b/spec/fixtures/release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f_tracks.xml new file mode 100644 index 0000000..a82fda3 --- /dev/null +++ b/spec/fixtures/release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f_tracks.xml @@ -0,0 +1,151 @@ + + + + Empire + Official + normal + + eng + + + 2006-08-28 + GB + + + 2006-08-28 + + United Kingdom + United Kingdom + + GB + + + + + B000H49P28 + + true + 1 + true + false + + + + 1 + + + 1 + 1 + 233013 + + Empire + 233013 + 2006-08-25 + + + + 2 + 2 + 207373 + + Shoot the Runner + 207373 + album version + 2006-08-25 + + + + 3 + 3 + 173160 + + Last Trip (In Flight) + 173160 + 2006-08-25 + + + + 4 + 4 + 148733 + + Me Plus One + 148733 + 2006-08-25 + + + + 5 + 5 + 248653 + + Sun Rise Light Flies + 248653 + 2006-08-25 + + + + 6 + 6 + 107973 + + Apnoea + 107973 + 2006-08-25 + + + + 7 + 7 + 254373 + + By My Side + 254373 + 2006-08-25 + + + + 8 + 8 + 319626 + + Stuntman + 319626 + 2006-08-25 + + + + 9 + 9 + 135053 + + Seek & Destroy + 135053 + 2006-08-25 + + + + 10 + 10 + 199866 + + British Legion + 199866 + 2006-08-25 + + + + 11 + 11 + 1156293 + + The Doberman / [untitled] + 333973 + 2006-08-28 + + + + + + + diff --git a/spec/fixtures/release/find_b94cb547-cf7a-4357-894c-53c3bf33b093.xml b/spec/fixtures/release/find_b94cb547-cf7a-4357-894c-53c3bf33b093.xml new file mode 100644 index 0000000..9ef28eb --- /dev/null +++ b/spec/fixtures/release/find_b94cb547-cf7a-4357-894c-53c3bf33b093.xml @@ -0,0 +1,52 @@ + + + + Humanoid + Official + normal + + eng + + + + Humanoid + English version + 2009-10-02 + Album + + 2009-10-06 + US + + + 2009-10-06 + + United States + United States + + US + + + + + 602527197692 + B002NOYX6I + + false + 0 + false + false + + + + 1 + CD + + + + 2 + CD + + + + + diff --git a/spec/fixtures/release/find_by_discid_pmzhT6ZlFiwSRCdVwV0eqire5_Y-.xml b/spec/fixtures/release/find_by_discid_pmzhT6ZlFiwSRCdVwV0eqire5_Y-.xml new file mode 100644 index 0000000..f5ba044 --- /dev/null +++ b/spec/fixtures/release/find_by_discid_pmzhT6ZlFiwSRCdVwV0eqire5_Y-.xml @@ -0,0 +1,141 @@ + + + + 217171 + + 150 + 35000 + 63711 + 86440 + 105891 + 128017 + 154668 + 177007 + 200379 + + + + Kveikur + Official + normal + Cardboard/Paper Sleeve + + isl + + + 2013-06-17 + XE + + + 2013-06-17 + + Europe + Europe + + XE + + + + + 2013-06-18 + + United States + United States + + US + + + + + 634904060626 + B00C1GBOU6 + + true + 14 + true + true + + + + 1 + CD + + + 217171 + + 150 + 35000 + 63711 + 86440 + 105891 + 128017 + 154668 + 177007 + 200379 + + + + + + + + + Kveikur + Official + normal + Cardboard/Paper Sleeve + + eng + + + 2013-06-14 + TW + + + 2013-06-14 + + Taiwan + Taiwan + + TW + + + CN-71 + + + + + 4712765169088 + + true + 1 + true + false + + + + 1 + CD + + + 217171 + + 150 + 35000 + 63711 + 86440 + 105891 + 128017 + 154668 + 177007 + 200379 + + + + + + + + + + diff --git a/spec/models/release_spec.rb b/spec/models/release_spec.rb index fcc8751..0b90537 100644 --- a/spec/models/release_spec.rb +++ b/spec/models/release_spec.rb @@ -1,44 +1,73 @@ require "spec_helper" describe MusicBrainz::Release do - it "gets no exception while loading release info" do - expect { - MusicBrainz::Release.find("2225dd4c-ae9a-403b-8ea0-9e05014c778f") - }.to_not raise_error(Exception) - end + before(:each) { + mock url: 'http://musicbrainz.org/ws/2/release/2225dd4c-ae9a-403b-8ea0-9e05014c778f?inc=media+release-groups', + fixture: 'release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f.xml' - it "gets correct instance" do - release = MusicBrainz::Release.find("2225dd4c-ae9a-403b-8ea0-9e05014c778f") - expect(release).to be_an_instance_of(MusicBrainz::Release) - end + mock url: 'http://musicbrainz.org/ws/2/release/2225dd4c-ae9a-403b-8ea0-9e05014c778f?inc=recordings+media&limit=100', + fixture: 'release/find_2225dd4c-ae9a-403b-8ea0-9e05014c778f_tracks.xml' + + mock url: 'http://musicbrainz.org/ws/2/release/b94cb547-cf7a-4357-894c-53c3bf33b093?inc=media+release-groups', + fixture: 'release/find_b94cb547-cf7a-4357-894c-53c3bf33b093.xml' + + mock url: 'http://musicbrainz.org/ws/2/discid/pmzhT6ZlFiwSRCdVwV0eqire5_Y-?', + fixture: 'release/find_by_discid_pmzhT6ZlFiwSRCdVwV0eqire5_Y-.xml' + } + + describe '.find' do + let(:release) { + MusicBrainz::Release.find("b94cb547-cf7a-4357-894c-53c3bf33b093") + } - it "gets correct release data" do - release = MusicBrainz::Release.find("b94cb547-cf7a-4357-894c-53c3bf33b093") - expect(release.id).to eq "b94cb547-cf7a-4357-894c-53c3bf33b093" - expect(release.title).to eq "Humanoid" - expect(release.status).to eq "Official" - expect(release.date).to eq Date.new(2009, 10, 6) - expect(release.country).to eq "US" - expect(release.asin).to eq 'B002NOYX6I' - expect(release.barcode).to eq '602527197692' - expect(release.quality).to eq 'normal' - expect(release.type).to eq 'Album' + it "gets no exception while loading release info" do + expect { release }.to_not raise_error(Exception) + end + + it "gets correct instance" do + expect(release).to be_an_instance_of(MusicBrainz::Release) + end + + it "gets correct release data" do + expect(release.id).to eq "b94cb547-cf7a-4357-894c-53c3bf33b093" + expect(release.title).to eq "Humanoid" + expect(release.status).to eq "Official" + expect(release.date).to eq Date.new(2009, 10, 6) + expect(release.country).to eq "US" + expect(release.asin).to eq 'B002NOYX6I' + expect(release.barcode).to eq '602527197692' + expect(release.quality).to eq 'normal' + expect(release.type).to eq 'Album' + end end - it "gets correct release tracks" do - tracks = MusicBrainz::Release.find("2225dd4c-ae9a-403b-8ea0-9e05014c778f").tracks - expect(tracks.length).to eq 11 - expect(tracks.first.position).to eq 1 - expect(tracks.first.recording_id).to eq "b3015bab-1540-4d4e-9f30-14872a1525f7" - expect(tracks.first.title).to eq "Empire" - expect(tracks.first.length).to eq 233013 + describe '#tracks' do + let(:release) { + MusicBrainz::Release.find("2225dd4c-ae9a-403b-8ea0-9e05014c778f") + } + let(:tracks) { + release.tracks + } + + it "gets correct release tracks" do + expect(tracks.length).to eq 11 + expect(tracks.first.position).to eq 1 + expect(tracks.first.recording_id).to eq "b3015bab-1540-4d4e-9f30-14872a1525f7" + expect(tracks.first.title).to eq "Empire" + expect(tracks.first.length).to eq 233013 + end end - it "gets a list of matching releases for a discid" do - releases = MusicBrainz::Release.find_by_discid("pmzhT6ZlFiwSRCdVwV0eqire5_Y-") - expect(releases.length).to eq 2 - expect(releases.first.id).to eq "7a31cd5f-6a57-4fca-a731-c521df1d3b78" - expect(releases.first.title).to eq "Kveikur" - expect(releases.first.asin).to eq "B00C1GBOU6" + describe '.find_by_discid' do + let(:releases) { + MusicBrainz::Release.find_by_discid("pmzhT6ZlFiwSRCdVwV0eqire5_Y-") + } + + it "gets a list of matching releases for a discid" do + expect(releases.length).to eq 2 + expect(releases.first.id).to eq "7a31cd5f-6a57-4fca-a731-c521df1d3b78" + expect(releases.first.title).to eq "Kveikur" + expect(releases.first.asin).to eq "B00C1GBOU6" + end end end From f82c7b2154e5a5974bc70f57175ca406d64f809a Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 23:42:52 +0100 Subject: [PATCH 08/12] Update caching proxy spec --- .../client_modules/caching_proxy.rb | 40 +++++++++++++------ spec/client_modules/cache_spec.rb | 8 ++-- spec/fixtures/kasabian.xml | 1 - spec/support/mock_helpers.rb | 12 +++--- 4 files changed, 38 insertions(+), 23 deletions(-) delete mode 100644 spec/fixtures/kasabian.xml diff --git a/lib/musicbrainz/client_modules/caching_proxy.rb b/lib/musicbrainz/client_modules/caching_proxy.rb index 0d3ecaf..a67291d 100644 --- a/lib/musicbrainz/client_modules/caching_proxy.rb +++ b/lib/musicbrainz/client_modules/caching_proxy.rb @@ -19,28 +19,42 @@ def get_contents(url) response = { body: nil, status: 500 } if File.exist?(file_path) - response = { - body: File.open(file_path, 'rb').gets, - status: 200 - } + get_cached_contents(file_path) else - response = super + response = get_live_contents(file_path, url) if response[:status] == 200 - FileUtils.mkpath(dir_path) - File.open(file_path, 'wb') do |f| - f.puts(response[:body]) - f.chmod(0755) - f.close - end + cache_contents!(file_path, response[:body]) end + response end - - response end def caching? MusicBrainz.config.perform_caching end + + private + + def get_cached_contents(file_path) + { + body: File.open(file_path, 'rb').gets, + status: 200 + } + end + + def get_live_contents(file_path, url) + http.get(url) + end + + def cache_contents!(file_path, body) + dir_path = File.dirname(file_path) + FileUtils.mkpath(dir_path) + File.open(file_path, 'wb') do |f| + f.puts(body) + f.chmod(0755) + f.close + end + end end end end diff --git a/spec/client_modules/cache_spec.rb b/spec/client_modules/cache_spec.rb index f449c58..114fb59 100644 --- a/spec/client_modules/cache_spec.rb +++ b/spec/client_modules/cache_spec.rb @@ -5,8 +5,7 @@ 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 } + let(:test_response){ read_fixture('artist/find_69b39eab-6577-46a4-a9f5-817839092033.xml') } before(:all) do MusicBrainz.config.cache_path = File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'cache_module_spec_cache') @@ -28,8 +27,9 @@ expect(File).to_not exist(test_cache_file) # Stubbing - allow(MusicBrainz.client.http).to receive(:get).and_return(OpenStruct.new(status: 200, body: test_response)) - expect(MusicBrainz.client.http).to receive(:get).once + allow(MusicBrainz.client).to receive(:get_live_contents) + .and_return({status: 200, body: test_response}) + expect(MusicBrainz.client).to receive(:get_live_contents).once 2.times do artist = MusicBrainz::Artist.find(test_mbid) diff --git a/spec/fixtures/kasabian.xml b/spec/fixtures/kasabian.xml deleted file mode 100644 index 964db40..0000000 --- a/spec/fixtures/kasabian.xml +++ /dev/null @@ -1 +0,0 @@ -KasabianKasabianGB1999http://allmusic.com/artist/p678134http://en.wikipedia.org/wiki/Kasabianhttp://twitter.com/kasabianhqhttp://www.bbc.co.uk/music/artists/69b39eab-6577-46a4-a9f5-817839092033http://www.discogs.com/artist/Kasabianhttp://www.facebook.com/kasabianhttp://www.imdb.com/name/nm1868442/http://www.kasabian.co.uk/http://www.myspace.com/kasabianhttp://www.youtube.com/kasabianvevohttp://www.youtube.com/user/KasabianTour diff --git a/spec/support/mock_helpers.rb b/spec/support/mock_helpers.rb index 77ccc6a..df6dd9d 100644 --- a/spec/support/mock_helpers.rb +++ b/spec/support/mock_helpers.rb @@ -1,11 +1,13 @@ module MockHelpers def mock(url:, fixture:) - spec_path = File.join(File.dirname(__FILE__), "..") - file_path = File.join(spec_path, "fixtures", fixture) - response = File.open(file_path).read - allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents) .with(url) - .and_return({ status: 200, body: response}) + .and_return({ status: 200, body: read_fixture(fixture)}) + end + + def read_fixture(name) + spec_path = File.join(File.dirname(__FILE__), "..") + file_path = File.join(spec_path, "fixtures", name) + File.open(file_path).read end end From a1f207800442537bcb1e12821e0289bc4e344b60 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 23:46:11 +0100 Subject: [PATCH 09/12] Remove test response caching --- .rspec | 1 - spec/spec_helper.rb | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) delete mode 100644 .rspec diff --git a/.rspec b/.rspec deleted file mode 100644 index 4e1e0d2..0000000 --- a/.rspec +++ /dev/null @@ -1 +0,0 @@ ---color diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e280957..6c284f3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,7 +7,9 @@ RSpec.configure do |c| include MockHelpers + c.order = 'random' + c.color = true end MusicBrainz.configure do |c| @@ -17,7 +19,4 @@ c.app_name = "MusicBrainzGemTestSuite" c.app_version = MusicBrainz::VERSION c.contact = test_email - - c.cache_path = File.join(File.dirname(__FILE__), '..', 'tmp', 'spec_cache') - c.perform_caching = true end From fb549ddda376de2e8f8ef5d5515422ea03234804 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 23:53:04 +0100 Subject: [PATCH 10/12] Add Github test action --- .github/workflows/test.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ee06a42 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,26 @@ +# Adopted from +# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-ruby +name: Ruby CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: ['3.3'] + steps: + - uses: actions/checkout@v4 + - name: Set up Ruby ${{ matrix.ruby-version }} + uses: ruby/setup-ruby@v1.173.0 + with: + ruby-version: ${{ matrix.ruby-version }} + - name: Install dependencies + run: bundle install + - name: Run tests + run: bundle exec rspec From e4a86a8779bb65688eb193f13f24d183b76d9a9f Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 23:56:54 +0100 Subject: [PATCH 11/12] Update branch name --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ee06a42..6a650e7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,9 +4,9 @@ name: Ruby CI on: push: - branches: [ main ] + branches: [ master ] pull_request: - branches: [ main ] + branches: [ master ] jobs: test: From c222602109c4a44b431f55776a0bca6aed4fd5a7 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 20 Mar 2024 23:58:13 +0100 Subject: [PATCH 12/12] Remove email requirement in specs --- spec/spec_helper.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6c284f3..5822594 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,10 +13,7 @@ end MusicBrainz.configure do |c| - test_email = `git config user.email`.chomp - raise 'Configure user.email in Git before running tests' if test_email.empty? - c.app_name = "MusicBrainzGemTestSuite" c.app_version = MusicBrainz::VERSION - c.contact = test_email + c.contact = "root@localhost" end