Skip to content

Commit 192d8f2

Browse files
committed
add some tests and start on standardizing interface
1 parent 3733f34 commit 192d8f2

File tree

5 files changed

+178
-13
lines changed

5 files changed

+178
-13
lines changed

cpanfile.snapshot

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,19 @@ DISTRIBUTIONS
12401240
Hash::Merge::Simple 0
12411241
List::Util 0
12421242
Moo 0
1243+
Const-Fast-0.014
1244+
pathname: L/LE/LEONT/Const-Fast-0.014.tar.gz
1245+
provides:
1246+
Const::Fast 0.014
1247+
requirements:
1248+
Carp 0
1249+
Module::Build::Tiny 0.021
1250+
Scalar::Util 0
1251+
Storable 0
1252+
Sub::Exporter::Progressive 0.001007
1253+
perl 5.008
1254+
strict 0
1255+
warnings 0
12431256
Context-Preserve-0.01
12441257
pathname: J/JR/JROCKWAY/Context-Preserve-0.01.tar.gz
12451258
provides:

lib/MetaCPAN/Model/Search.pm

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ use MetaCPAN::Types qw( Object Str );
1313
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );
1414

1515
has es => (
16-
is => 'ro',
17-
isa => Object,
18-
handles => {
19-
_run_query => 'search',
20-
},
16+
is => 'ro',
17+
isa => Object,
18+
handles => { _run_query => 'search', },
2119
required => 1,
2220
);
2321

@@ -336,9 +334,8 @@ sub build_query {
336334
}
337335
},
338336
{
339-
term => {
340-
'module.indexed' => 1
341-
}
337+
term =>
338+
{ 'module.indexed' => 1 }
342339
}
343340
]
344341
},
@@ -392,10 +389,8 @@ sub _build_search_descriptions_query {
392389
my $query = {
393390
query => {
394391
filtered => {
395-
query => { match_all => {} },
396-
filter => {
397-
or => [ map { { term => { id => $_ } } } @ids ]
398-
}
392+
query => { match_all => {} },
393+
filter => { or => [ map { { term => { id => $_ } } } @ids ] }
399394
}
400395
},
401396
fields => [qw(description id)],
@@ -406,7 +401,10 @@ sub _build_search_descriptions_query {
406401

407402
sub search_descriptions {
408403
my ( $self, @ids ) = @_;
409-
return {} unless @ids;
404+
return {
405+
descriptions => {},
406+
took => 0,
407+
} unless @ids;
410408

411409
my $query = $self->_build_search_descriptions_query(@ids);
412410
my $data = $self->run_query( file => $query );

t/model/search.t

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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();

xt/README.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Tests in here are for development only
2+
3+
# Setup port forwarding to our staging server
4+
ssh -L 9200:localhost:9200 [email protected]
5+
6+
# Run tests - with ES env
7+
bin/prove_live xt/...

xt/search_web.t

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use strict;
2+
use warnings;
3+
4+
# USE `bin/prove_live` to run this
5+
# READ the README.txt in this dir
6+
7+
use Data::Dumper;
8+
use MetaCPAN::Model::Search ();
9+
use MetaCPAN::TestServer ();
10+
use Test::More;
11+
12+
# Just use this to get an es object.
13+
my $server = MetaCPAN::TestServer->new;
14+
my $search = MetaCPAN::Model::Search->new(
15+
es => $server->es_client,
16+
index => 'cpan',
17+
);
18+
19+
my %tests = (
20+
'anyevent http' => 'AnyEvent::HTTP',
21+
'anyevent' => 'AnyEvent',
22+
'AnyEvent' => 'AnyEvent',
23+
'dbi' => 'DBI',
24+
'dbix class resultset' => 'DBIx::Class::ResultSet',
25+
'DBIx::Class' => 'DBIx::Class',
26+
'Dist::Zilla' => 'Dist::Zilla',
27+
'HTML::Element' => 'HTML::Element',
28+
'HTML::TokeParser' => 'HTML::TokeParser',
29+
'net dns' => 'Net::DNS',
30+
'net::amazon::s3' => 'Net::Amazon::S3',
31+
'Perl::Critic' => 'Perl::Critic',
32+
);
33+
34+
for my $q (sort keys %tests) {
35+
my $match = $tests{$q};
36+
my $returned = $search->search_web($q);
37+
my $first_match = $returned->{results}->[0]->[0];
38+
39+
is($first_match->{documentation}, $match, "Search for ${q} matched ${match}");
40+
# or diag Dumper($first_match);
41+
}
42+
43+
done_testing();

0 commit comments

Comments
 (0)