Skip to content

Commit 37fe00c

Browse files
authored
Merge pull request #1218 from metacpan/haarg/fake-rating-scroll
provide scrolled search for ratings with a fake id
2 parents 76e879d + 31fcad7 commit 37fe00c

File tree

3 files changed

+112
-10
lines changed

3 files changed

+112
-10
lines changed

lib/MetaCPAN/Server/Controller/Rating.pm

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,46 @@ sub _mapping : Path('_mapping') : Args(0) {
2929
}
3030

3131
sub find : Path('_search') : Args(0) {
32-
my ( $self, $c ) = @_;
32+
my ( $self, $c, $scroll ) = @_;
33+
34+
my @hits;
35+
36+
# fake results for MetaCPAN::Client so it doesn't fail its tests
37+
if ( ( $c->req->user_agent // '' )
38+
=~ m{^MetaCPAN::Client-testing/([0-9.]+)} )
39+
{
40+
if ( $1 <= 2.031001 ) {
41+
my $query = $c->read_param('query');
42+
if ( $query
43+
&& $query->[0]
44+
&& $query->[0]{term}
45+
&& ( $query->[0]{term}{distribution} // '' ) eq 'Moose' )
46+
{
47+
48+
push @hits,
49+
{
50+
_source => {
51+
distribution => "Moose"
52+
},
53+
};
54+
}
55+
}
56+
}
57+
3358
$c->stash_or_detach( {
34-
timed_out => \0,
35-
took => 0,
36-
_shards => {
59+
$c->req->param('scroll') ? ( _scroll_id => 'FAKE_SCROLL_ID' ) : (),
60+
_shards => {
61+
failed => 0,
3762
successful => 0,
3863
total => 0,
39-
failed => 0,
4064
},
4165
hits => {
42-
total => 0,
43-
hits => [],
66+
hits => \@hits,
4467
max_score => undef,
45-
}
68+
total => scalar @hits,
69+
},
70+
timed_out => \0,
71+
took => 0,
4672
} );
4773
}
4874

lib/MetaCPAN/Server/Controller/Scroll.pm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ sub index : Path('/_search/scroll') : Args {
3434
};
3535
}
3636

37+
if ( $scroll_id eq 'FAKE_SCROLL_ID' ) {
38+
$c->stash( {
39+
_scroll_id => $scroll_id,
40+
_shards => {
41+
failed => 0,
42+
successful => 0,
43+
total => 0,
44+
},
45+
hits => {
46+
hits => [],
47+
max_score => undef,
48+
total => 0,
49+
},
50+
timed_out => \0,
51+
took => 0,
52+
} );
53+
return;
54+
}
55+
3756
my $res = eval {
3857
$c->model('CPAN')->es->scroll( {
3958
scroll_id => $scroll_id,

t/server/controller/rating.t

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use lib 't/lib';
44

55
use MetaCPAN::Server::Test;
66
use MetaCPAN::TestHelpers qw( decode_json_ok test_cache_headers );
7+
use Cpanel::JSON::XS qw(encode_json);
78
use Test::More;
89

910
test_psgi app, sub {
@@ -44,8 +45,8 @@ test_psgi app, sub {
4445

4546
ok(
4647
$res = $cb->(
47-
POST '/rating/_search',
48-
Content => '{"query":{"term":{"distribution":"Moose"}}}'
48+
POST '/rating',
49+
Content => '{"query":{"term":{"distribution":"Moose"}}}',
4950
),
5051
'POST /rating'
5152
);
@@ -55,6 +56,62 @@ test_psgi app, sub {
5556
$ratings = decode_json_ok($res);
5657

5758
is_deeply $ratings->{hits}{hits}, [], 'no hits';
59+
60+
ok(
61+
$res = $cb->(
62+
POST '/rating/_search?scroll=5m',
63+
Content => '{"query":{"term":{"distribution":"Moose"}}}',
64+
),
65+
'POST /rating'
66+
);
67+
68+
is $res->code, 200, 'found';
69+
70+
$ratings = decode_json_ok($res);
71+
72+
is_deeply $ratings->{hits}{hits}, [], 'no hits';
73+
74+
is_deeply $ratings->{_scroll_id}, 'FAKE_SCROLL_ID',
75+
'gives fake scroll id';
76+
77+
ok(
78+
$res
79+
= $cb->( POST "/_search/scroll/$ratings->{_scroll_id}?scroll=5m",
80+
),
81+
'POST /_search/scroll/$id',
82+
);
83+
84+
is $res->code, 200, 'found'
85+
or diag $res->as_string;
86+
87+
$ratings = decode_json_ok($res);
88+
89+
is_deeply $ratings->{hits}{hits}, [], 'working with no hits';
90+
is $ratings->{_shards}{total}, 0, 'results are fake';
91+
92+
ok(
93+
$res = $cb->(
94+
POST '/rating/_search',
95+
'User-Agent' => 'MetaCPAN::Client-testing/2.031001',
96+
Content => '{"query":{"term":{"distribution":"Moose"}}}',
97+
),
98+
'POST /rating with MetaCPAN::Client test UA'
99+
);
100+
101+
is $res->code, 200, 'found';
102+
103+
$ratings = decode_json_ok($res);
104+
105+
is_deeply $ratings->{hits}{hits},
106+
[
107+
{
108+
_source => {
109+
distribution => 'Moose',
110+
},
111+
},
112+
],
113+
'no hits';
114+
58115
};
59116

60117
done_testing;

0 commit comments

Comments
 (0)