Skip to content

Commit

Permalink
More test, added offset, check some search fields
Browse files Browse the repository at this point in the history
  • Loading branch information
bfaist committed Mar 5, 2017
1 parent 6036952 commit 72781c3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/WebService/MusicBrainz.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ has subquery_map => sub {
return \%subquery_map;
};

has search_fields_by_resource => sub {
my %search_fields;

$search_fields{area} = ['aid','alias','area','begin','comment','end','ended','sortname','iso','iso1','iso2','iso3','type'];
$search_fields{artist} = ['area','beginarea','endarea','arid','artist','artistaccent','alias','begin','comment','country','end','ended','gender','ipi','sortname','tag','type'];
$search_fields{label} = ['alias','area','begin','code','comment','country','end','ended','ipi','label','labelaccent','laid','sortname','type','tag'];
$search_fields{recording} = ['arid','artist','artistname','creditname','comment','country','date','dur','format','isrc','number','position','primarytype','puid','qdur','recording','recordingaccent','reid','release','rgid','rid','secondarytype','status','tid','trnum','tracks','tracksrelease','tag','type','video'];
$search_fields{release_group} = ['arid','artist','comment','creditname','primarytype','rgid','releasegroup','releasegroupaccent','releases','release','reid','secondarytype','status','tag','type'];
$search_fields{release} = ['arid','artist','artistname','asin','barcode','catno','comment','country','creditname','date','discids','discidsmedium','format','laid','label','lang','mediums','primarytype','puid','quality','reid','release','releaseaccent','rgid','script','secondarytype','status','tag','tracks','tracksmedium','type'];
$search_fields{work} = ['alias','arid','artist','comment','iswc','lang','tag','type','wid','work','workaccent'];

return \%search_fields;
};

has is_valid_subquery => sub {
my $self = shift;
my $resource = shift;
Expand Down Expand Up @@ -98,6 +112,17 @@ sub search {
delete $search_query->{format};
}

if(exists $search_query->{offset}) {
$self->request()->offset($search_query->{offset});
delete $search_query->{offset};
}

foreach my $search_field (keys %{ $search_query }) {
if(! grep /^$search_field$/, @{ $self->search_fields_by_resource->{$search_resource} }) {
die "Not a valid search field ($search_field) for resource \"$search_resource\"";
}
}

$self->request->query_params($search_query);

my $request_result = $self->request()->result();
Expand Down Expand Up @@ -142,7 +167,7 @@ API to search the musicbrainz.org database
my $results = $mb->search($resource => { param1 => 'value1' });
Valid values for $resource are: area, artist, event, instrument, label, recording, release, release-group, series, work, url
The default is to return a decoded JSON as a perl data structure. Specify format => 'xml' to return the results as an instance of Mojo::DOM.
The default is to return decoded JSON as a perl data structure. Specify format => 'xml' to return the results as an instance of Mojo::DOM.
=head3 Search by MBID
Expand All @@ -151,10 +176,13 @@ The default is to return a decoded JSON as a perl data structure. Specify forma
=head3 Search area
my $areas = $mb->search(area => { area => 'cincinnati' });
my $areas = $mb->search(area => { iso => 'US-OH' });
=head3 Search artist
my $artists = $mb->search(artist => { name => 'Ryan Adams' });
my $artists = $mb->search(artist => { artist => 'Ryan Adams', type => 'Person' });
my $artist_country = $artists->{artists}->[0]->{country};
=head1 AUTHOR
Expand Down
3 changes: 3 additions & 0 deletions lib/WebService/MusicBrainz/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ has 'search_resource';
has 'mbid';
has 'inc' => sub { [] };
has 'query_params';
has offset => 0;
has debug => sub { $ENV{MUSICBRAINZ_DEBUG} || 0 };;

our $VERSION = '1.0';
Expand Down Expand Up @@ -47,6 +48,8 @@ sub make_url {
$url_str .= '&query=' . $extra_param_str;
}

$url_str .= '&offset=' . $self->offset();

print "REQUEST URL: $url_str\n" if $self->debug();

my $url = Mojo::URL->new($url_str);
Expand Down
11 changes: 11 additions & 0 deletions t/Area.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ ok($s2_res->{count} == 2);
ok($s2_res->{areas}->[0]->{type} eq 'City');
ok($s2_res->{areas}->[1]->{type} eq 'City');

my $s3_res = $ws->search(area => { iso => 'US-OH' });
ok($s3_res->{count} == 1);
ok($s3_res->{areas}->[0]->{name} eq 'Ohio');

eval { my $s4_res = $ws->search(area => { something => '99999' }) };
if($@) { ok($@) } # catch error

my $s5_res = $ws->search(area => { iso => 'US-CA', format => 'xml' });
ok($s5_res->find('name')->first->text eq 'California');
ok($s5_res->at('area')->attr('ext:score') == 100);

done_testing();
11 changes: 11 additions & 0 deletions t/Artist.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ ok($s6_res->{count} == 1);
ok($s6_res->{artists}->[0]->{type} eq 'Group');
ok($s6_res->{artists}->[0]->{id} eq 'b665b768-0d83-4363-950c-31ed39317c15');

my $s7_res = $ws->search(artist => { artist => 'Ryan Adams', type => 'person' });
ok($s7_res->{artists}->[0]->{'sort-name'} eq 'Adams, Ryan');

my $s8_res = $ws->search(artist => { artist => 'red' });
ok($s8_res->{count} >= 1700);
ok($s8_res->{offset} == 0);

my $s9_res = $ws->search(artist => { artist => 'red', offset => 30 });
ok($s9_res->{count} >= 1700);
ok($s9_res->{offset} == 30);

# XML TESTS
my $s1_dom = $ws->search(artist => { mbid => '070d193a-845c-479f-980e-bef15710653e', format => 'xml' });
ok($s1_dom->at('sort-name')->text eq 'Prince');
Expand Down

0 comments on commit 72781c3

Please sign in to comment.