|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | + |
| 4 | +use MetaCPAN::Model::Search (); |
| 5 | +use MetaCPAN::TestServer (); |
| 6 | +use Test::More; |
| 7 | +use Test::Deep; |
| 8 | + |
| 9 | +# Just use this to get an es object. |
| 10 | +my $server = MetaCPAN::TestServer->new; |
| 11 | +my $search = MetaCPAN::Model::Search->new( |
| 12 | + es => $server->es_client, |
| 13 | + index => 'cpan', |
| 14 | +); |
| 15 | + |
| 16 | +ok( $search, 'search' ); |
| 17 | +ok( $search->_not_rogue, '_not_rogue' ); |
| 18 | + |
| 19 | +{ |
| 20 | + my $results = $search->search_web('Fooxxxx'); |
| 21 | + cmp_deeply( $results, {}, 'no results on fake module' ); |
| 22 | +} |
| 23 | + |
| 24 | +{ |
| 25 | + my $collapsed_search = $search->search_web('Foo'); |
| 26 | + is( scalar @{ $collapsed_search->{results}->[0] }, |
| 27 | + 2, 'got results for collapsed search' ); |
| 28 | + |
| 29 | + ok( |
| 30 | + ${ $collapsed_search->{collapsed} }, |
| 31 | + 'results are flagged as collapsed' |
| 32 | + ); |
| 33 | + |
| 34 | + my $from = 0; |
| 35 | + my $page_size = 20; |
| 36 | + my $collapsed = 0; |
| 37 | + |
| 38 | + my $expanded |
| 39 | + = $search->search_web( 'Foo', $from, $page_size, $collapsed ); |
| 40 | + |
| 41 | + ok( !${ $expanded->{collapsed} }, 'results are flagged as expanded' ); |
| 42 | + |
| 43 | + is( $expanded->{results}->[0]->[0]->{path}, |
| 44 | + 'lib/Pod/Pm.pm', 'first expanded result is expected' ); |
| 45 | + is( $expanded->{results}->[1]->[0]->{path}, |
| 46 | + 'lib/Pod/Pm/NoPod.pod', 'second expanded result is expected' ); |
| 47 | +} |
| 48 | + |
| 49 | +{ |
| 50 | + my $results = $search->search_web('author:Mo'); |
| 51 | + is( @{ $results->{results} }, 5, '5 results on author search' ); |
| 52 | +} |
| 53 | + |
| 54 | +{ |
| 55 | + my $long_form = $search->search_web('distribution:Pod-Pm'); |
| 56 | + my $short_form = $search->search_web('dist:Pod-Pm'); |
| 57 | + |
| 58 | + cmp_deeply( |
| 59 | + $long_form->{results}, |
| 60 | + $short_form->{results}, |
| 61 | + 'dist == distribution search' |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +{ |
| 66 | + my $module = 'Binary::Data::WithPod'; |
| 67 | + my $results = $search->search_web($module); |
| 68 | + is( |
| 69 | + $results->{results}->[0]->[0]->{description}, |
| 70 | + 'razzberry pudding', |
| 71 | + 'description included in results' |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +{ |
| 76 | + my $id = 'JatCtNR2RGjcBIs1Y5C_zTzNcXU'; |
| 77 | + my $results = $search->search_descriptions($id); |
| 78 | + cmp_deeply( $results->{results}, { $id => 'TBD' }, |
| 79 | + 'search_descriptions' ); |
| 80 | +} |
| 81 | + |
| 82 | +# favorites are also tested in t/server/controller/user/favorite.t |
| 83 | +cmp_deeply( $search->search_favorites, {}, |
| 84 | + 'empty hashref when no distributions' ); |
| 85 | + |
| 86 | +cmp_deeply( |
| 87 | + $search->search_favorites('Pod-Pm'), |
| 88 | + { |
| 89 | + favorites => {}, |
| 90 | + took => ignore(), |
| 91 | + }, |
| 92 | + 'no favorites found' |
| 93 | +); |
| 94 | + |
| 95 | +cmp_deeply( |
| 96 | + $search->search_descriptions, |
| 97 | + { |
| 98 | + descriptions => {}, |
| 99 | + took => ignore(), |
| 100 | + }, |
| 101 | + 'empty hashref when no ids for descriptions' |
| 102 | +); |
| 103 | + |
| 104 | +done_testing(); |
0 commit comments