Skip to content

Commit ee9f591

Browse files
committed
convert MetaCPAN::Tests system to avoid ElasticSearchX::Model
1 parent 86cf1f2 commit ee9f591

20 files changed

+153
-126
lines changed

t/lib/MetaCPAN/Tests/Distribution.pm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ use Test::Routine;
44
use version;
55
use MetaCPAN::Types::TypeTiny qw( Str );
66

7-
with qw(
8-
MetaCPAN::Tests::Model
9-
);
7+
with qw( MetaCPAN::Tests::Query );
108

119
sub _build_type {'distribution'}
1210

1311
sub _build_search {
14-
return [ get => $_[0]->name ];
12+
my $self = shift;
13+
return { term => { name => $self->name } };
1514
}
1615

1716
my @attrs = qw(
@@ -27,7 +26,7 @@ test 'distribution attributes' => sub {
2726
my ($self) = @_;
2827

2928
foreach my $attr (@attrs) {
30-
is $self->data->$attr, $self->$attr, $attr;
29+
is $self->data->{$attr}, $self->$attr, $attr;
3130
}
3231
};
3332

t/lib/MetaCPAN/Tests/Extra.pm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ use Test::More;
33
use Test::Routine;
44
use MetaCPAN::Types::TypeTiny qw( CodeRef );
55

6+
around BUILDARGS => sub {
7+
my ( $orig, $class, @args ) = @_;
8+
my $attr = $class->$orig(@args);
9+
10+
delete $attr->{_expect}{extra_tests};
11+
12+
return $attr;
13+
};
14+
615
has _extra_tests => (
716
is => 'ro',
817
isa => CodeRef,

t/lib/MetaCPAN/Tests/Model.pm renamed to t/lib/MetaCPAN/Tests/Query.pm

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,59 @@
1-
package MetaCPAN::Tests::Model;
1+
package MetaCPAN::Tests::Query;
22

33
use Test::Routine;
44

5+
use MetaCPAN::ESConfig qw( es_doc_path );
56
use MetaCPAN::Server::Test ();
6-
use MetaCPAN::Types::TypeTiny qw( ArrayRef HashRef InstanceOf Str );
7+
use MetaCPAN::Types::TypeTiny qw( ES ArrayRef HashRef InstanceOf Str );
78
use Test::More;
89
use Try::Tiny qw( try );
910

10-
with qw(
11-
MetaCPAN::Tests::Extra
12-
MetaCPAN::Tests::PSGI
13-
);
14-
1511
around BUILDARGS => sub {
1612
my ( $orig, $class, @args ) = @_;
17-
my $attr = $class->$orig(@args);
18-
my $expect = {};
19-
20-
# Get a list of defined attributes.
21-
my %known = map { ( $_ => 1 ) }
22-
map { $_->init_arg() } $class->meta->get_all_attributes();
23-
24-
# We could extract any keys that don't have defined attributes
25-
# and only test those, but it shouldn't hurt to test the others
26-
# (the ones that do have attributes defined). This way we won't *not*
27-
# test something by accident if we define an attribute for it
28-
# and really anything we specify shouldn't be different on the result.
29-
while ( my ( $k, $v ) = each %$attr ) {
30-
$expect->{$k} = $attr->{$k};
31-
delete $attr->{$k} if !$known{$k};
32-
}
13+
my $attr = $class->$orig(@args);
14+
15+
my $expect = {%$attr};
3316

3417
return { _expect => $expect, %$attr };
3518
};
3619

20+
with qw(
21+
MetaCPAN::Tests::Extra
22+
MetaCPAN::Tests::PSGI
23+
);
24+
3725
has _type => (
3826
is => 'ro',
3927
isa => Str,
4028
builder => '_build_type',
4129
);
4230

43-
has model => (
31+
has es => (
4432
is => 'ro',
45-
isa => InstanceOf ['MetaCPAN::Model'],
33+
isa => ES,
4634
lazy => 1,
47-
default => sub { MetaCPAN::Server::Test::model() },
35+
default => sub { MetaCPAN::Server::Test::es() },
4836
);
4937

5038
has search => (
5139
is => 'ro',
52-
isa => ArrayRef,
40+
isa => HashRef,
5341
lazy => 1,
5442
builder => '_build_search',
5543
);
5644

5745
sub _do_search {
5846
my ($self) = @_;
59-
my ( $method, @params ) = @{ $self->search };
60-
return $self->model->doc( $self->_type )->$method(@params);
47+
my $query = $self->search;
48+
my $res = $self->es->search(
49+
es_doc_path( $self->_type ),
50+
body => {
51+
query => $query,
52+
size => 1,
53+
},
54+
);
55+
my $hit = $res->{hits}{hits}[0];
56+
return $hit ? $hit->{_source} : undef;
6157
}
6258

6359
has data => (
@@ -73,18 +69,18 @@ has _expectations => (
7369
init_arg => '_expect',
7470
);
7571

76-
test 'expected model attributes' => sub {
72+
test 'expected attributes' => sub {
7773
my ($self) = @_;
7874
my $exp = $self->_expectations;
7975
my $data = $self->data;
8076

8177
foreach my $key ( sort keys %$exp ) {
8278

8379
# Skip attributes of the test class that aren't attributes of the model.
84-
next unless $data->can($key);
80+
#next unless exists $data->{$key};
8581

86-
is_deeply $data->$key, $exp->{$key}, $key
87-
or diag Test::More::explain $data->$key;
82+
is_deeply $data->{$key}, $exp->{$key}, $key
83+
or diag Test::More::explain $data->{$key};
8884
}
8985
};
9086

t/lib/MetaCPAN/Tests/Release.pm

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ use version;
66

77
use HTTP::Request::Common qw( GET );
88
use List::Util ();
9+
use MetaCPAN::ESConfig qw( es_doc_path );
910
use MetaCPAN::Types::TypeTiny qw( ArrayRef HashRef Str );
1011
use Test::More;
1112

12-
with('MetaCPAN::Tests::Model');
13+
with qw( MetaCPAN::Tests::Query );
1314

1415
sub _build_type {'release'}
1516

1617
sub _build_search {
1718
my ($self) = @_;
18-
return [
19-
get => {
20-
author => $self->author,
21-
name => $self->name,
22-
}
23-
];
19+
return {
20+
bool => {
21+
must => [
22+
{ term => { author => $self->author } },
23+
{ term => { name => $self->name } },
24+
]
25+
},
26+
};
2427
}
2528

2629
around BUILDARGS => sub {
@@ -35,8 +38,9 @@ around BUILDARGS => sub {
3538
@$attr{qw( distribution version )} = ( $1, $2 );
3639
}
3740

38-
# We handle this one specially.
41+
# We handle these specially.
3942
delete $attr->{_expect}{tests};
43+
delete $attr->{_expect}{modules};
4044

4145
return $attr;
4246
};
@@ -92,7 +96,7 @@ sub file_content {
9296

9397
sub file_by_path {
9498
my ( $self, $path ) = @_;
95-
my $file = List::Util::first { $_->path eq $path } @{ $self->files };
99+
my $file = List::Util::first { $_->{path} eq $path } @{ $self->files };
96100
ok $file, "found file '$path'";
97101
return $file;
98102
}
@@ -117,17 +121,22 @@ sub filter_files {
117121
if $add_filters && ref($add_filters) ne 'ARRAY';
118122

119123
my $release = $self->data;
120-
return [
121-
$self->model->doc('file')->query( {
122-
bool => {
123-
must => [
124-
{ term => { 'author' => $release->author } },
125-
{ term => { 'release' => $release->name } },
126-
@{ $add_filters || [] },
127-
],
128-
}
129-
} )->size(100)->all
130-
];
124+
my $res = $self->es->search(
125+
es_doc_path('file'),
126+
body => {
127+
query => {
128+
bool => {
129+
must => [
130+
{ term => { 'author' => $release->{author} } },
131+
{ term => { 'release' => $release->{name} } },
132+
@{ $add_filters || [] },
133+
],
134+
},
135+
},
136+
size => 100,
137+
},
138+
);
139+
return [ map $_->{_source}, @{ $res->{hits}{hits} } ];
131140
}
132141

133142
has modules => (
@@ -177,7 +186,7 @@ has tests => (
177186

178187
sub has_tests_ok {
179188
my ($self) = @_;
180-
my $tests = $self->data->tests;
189+
my $tests = $self->data->{tests};
181190

182191
# Don't test the actual numbers since we copy this out of the real
183192
# database as a live test case.
@@ -198,15 +207,15 @@ test 'release attributes' => sub {
198207
my ($self) = @_;
199208

200209
foreach my $attr (@attrs) {
201-
is $self->data->$attr, $self->$attr, "release $attr";
210+
is $self->data->{$attr}, $self->$attr, "release $attr";
202211
}
203212

204213
if ( $self->expects_tests ) {
205214
if ( $self->tests eq '1' ) {
206215
$self->has_tests_ok;
207216
}
208217
else {
209-
is_deeply $self->data->tests, $self->tests, 'test results';
218+
is_deeply $self->data->{tests}, $self->tests, 'test results';
210219
}
211220
}
212221
};
@@ -218,11 +227,13 @@ test 'modules in Packages-1.103' => sub {
218227
unless scalar keys %{ $self->modules };
219228

220229
my %module_files
221-
= map { ( $_->path => $_->module ) } @{ $self->module_files };
230+
= map { ( $_->{path} => $_->{module} ) } @{ $self->module_files };
222231

223232
foreach my $path ( sort keys %{ $self->modules } ) {
224233
my $desc = "File '$path' has expected modules";
225234
if ( my $got = delete $module_files{$path} ) {
235+
my $got = [ map +{%$_}, @$got ];
236+
$_->{associated_pod} //= undef for @$got;
226237

227238
# We may need to sort modules by name, I'm not sure if order is reliable.
228239
is_deeply $got, $self->modules->{$path}, $desc

t/release/badpod.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ sub test_bad_pod {
3333

3434
my $file = $self->file_by_path('lib/BadPod.pm');
3535

36-
is $file->sloc, 3, 'sloc';
37-
is $file->slop, 4, 'slop';
36+
is $file->{sloc}, 3, 'sloc';
37+
is $file->{slop}, 4, 'slop';
3838

39-
is_deeply $file->pod_lines, [ [ 5, 7 ], ], 'no pod_lines';
39+
is_deeply $file->{pod_lines}, [ [ 5, 7 ], ], 'no pod_lines';
4040

41-
is ${ $file->pod },
41+
is $file->{pod},
4242

4343
# The unknown "=head" directive will get dropped
4444
# but the paragraph following it is valid.

t/release/binary-data.t

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ sub test_binary_data {
4545
{
4646
my $file = $self->file_by_path('lib/Binary/Data.pm');
4747

48-
is $file->sloc, 4, 'sloc';
49-
is $file->slop, 0, 'slop';
48+
is $file->{sloc}, 4, 'sloc';
49+
is $file->{slop}, 0, 'slop';
5050

5151
is_deeply $file->{pod_lines}, [], 'no pod_lines';
5252

5353
my $binary = $self->file_content($file);
5454
like $binary, qr/^=[a-zA-Z]/m, 'matches loose pod pattern';
5555

56-
is ${ $file->pod }, q[], 'no pod text';
56+
is $file->{pod}, q[], 'no pod text';
5757
}
5858

5959
{
6060
my $file = $self->file_by_path('lib/Binary/Data/WithPod.pm');
6161

62-
is $file->sloc, 4, 'sloc';
63-
is $file->slop, 7, 'slop';
62+
is $file->{sloc}, 4, 'sloc';
63+
is $file->{slop}, 7, 'slop';
6464

6565
is_deeply $file->{pod_lines}, [ [ 5, 5 ], [ 22, 6 ], ], 'pod_lines';
6666

@@ -69,7 +69,7 @@ sub test_binary_data {
6969
like $binary, qr/^=buzz9\xF0\x9F\x98\x8E/m,
7070
'matches more complex unwanted pod pattern';
7171

72-
is ${ $file->pod },
72+
is $file->{pod},
7373
q[NAME Binary::Data::WithPod - that's it DESCRIPTION razzberry pudding],
7474
'pod text';
7575
}

t/release/common-files.t

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ test_release( {
3131
{
3232
my $file = $self->file_by_path('Makefile.PL');
3333

34-
ok !$file->indexed, 'Makefile.PL not indexed';
35-
ok $file->authorized,
34+
ok !$file->{indexed}, 'Makefile.PL not indexed';
35+
ok $file->{authorized},
3636
'Makefile.PL authorized, i suppose (not *un*authorized)';
37-
is $file->sloc, 1, 'sloc';
38-
is $file->slop, 3, 'slop';
37+
is $file->{sloc}, 1, 'sloc';
38+
is $file->{slop}, 3, 'slop';
3939

40-
is scalar( @{ $file->pod_lines } ), 1, 'one pod section';
40+
is scalar( @{ $file->{pod_lines} } ), 1, 'one pod section';
4141

42-
is $file->abstract, undef, 'no abstract';
42+
is $file->{abstract}, undef, 'no abstract';
4343
}
4444

4545
},

t/release/documentation-not-readme.t

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ sub test_modules {
2222
is( @files, 1, 'includes one file with modules' );
2323

2424
my $file = shift @files;
25-
is( @{ $file->module }, 1, 'file contains one module' );
25+
is( @{ $file->{module} }, 1, 'file contains one module' );
2626

27-
my ($indexed) = grep { $_->{indexed} } @{ $file->module };
27+
my ($indexed) = grep { $_->{indexed} } @{ $file->{module} };
2828

29-
is( $indexed->name, 'Documentation::Not::Readme', 'module name' );
30-
is( $file->documentation, 'Documentation::Not::Readme', 'documentation' );
29+
is( $indexed->{name}, 'Documentation::Not::Readme', 'module name' );
30+
is( $file->{documentation},
31+
'Documentation::Not::Readme', 'documentation' );
3132

32-
is( $indexed->associated_pod,
33+
is( $indexed->{associated_pod},
3334
'RWSTAUNER/Documentation-Not-Readme-0.01/lib/Documentation/Not/Readme.pm'
3435
);
3536
}

t/release/file-duplicates.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test_release(
6565
);
6666

6767
while ( my ( $path, $count ) = each %dup ) {
68-
is( scalar( grep { $_->path =~ m{\Q$path\E$} } @$files ),
68+
is( scalar( grep { $_->{path} =~ m{\Q$path\E$} } @$files ),
6969
$count, "multiple files match $path" );
7070
}
7171
},

0 commit comments

Comments
 (0)