Skip to content

Commit 87f5f31

Browse files
authored
Merge pull request #1353 from metacpan/haarg/no-model-in-tests
remove most use of ElasticSearchX::Model from tests
2 parents 1071dff + b5defdb commit 87f5f31

34 files changed

+546
-438
lines changed

lib/MetaCPAN/Document/File/Set.pm

Lines changed: 0 additions & 72 deletions
This file was deleted.

lib/MetaCPAN/Query/File.pm

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,5 +677,74 @@ sub find_pod {
677677
}
678678
}
679679

680+
sub history {
681+
my ( $self, $type, $name, $path, $opts ) = @_;
682+
683+
$opts ||= {};
684+
if ( ref $path ) {
685+
$path = join '/', @$path;
686+
}
687+
688+
my $source = $opts->{fields};
689+
690+
my $query
691+
= $type eq "module"
692+
? {
693+
nested => {
694+
path => 'module',
695+
query => {
696+
constant_score => {
697+
filter => {
698+
bool => {
699+
must => [
700+
{ term => { "module.authorized" => true } },
701+
{ term => { "module.indexed" => true } },
702+
{ term => { "module.name" => $name } },
703+
]
704+
}
705+
}
706+
}
707+
}
708+
}
709+
}
710+
: $type eq "file" ? {
711+
bool => {
712+
must => [
713+
{ term => { path => $path } },
714+
{ term => { distribution => $name } },
715+
]
716+
}
717+
}
718+
719+
# XXX: to fix: no filtering on 'release' so this query
720+
# will produce modules matching duplications. -- Mickey
721+
: $type eq "documentation" ? {
722+
bool => {
723+
must => [
724+
{ match_phrase => { documentation => $name } },
725+
{ term => { indexed => true } },
726+
{ term => { authorized => true } },
727+
]
728+
}
729+
}
730+
: return undef;
731+
732+
my $res = $self->es->search(
733+
es_doc_path('file'),
734+
body => {
735+
query => $query,
736+
size => 500,
737+
sort => [ { date => 'desc' } ],
738+
( $source ? ( _source => $source ) : () ),
739+
},
740+
);
741+
742+
return {
743+
took => $res->{took},
744+
total => hit_total($res),
745+
files => [ map $_->{_source}, @{ $res->{hits}{hits} } ],
746+
};
747+
}
748+
680749
__PACKAGE__->meta->make_immutable;
681750
1;

lib/MetaCPAN/Server/Controller/Search/History.pm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ with 'MetaCPAN::Server::Role::JSONP';
1212
has '+type' => ( default => 'file' );
1313

1414
sub get : Local : Path('') : Args {
15-
my ( $self, $c, @args ) = @_;
16-
my $data = $self->model($c)->history(@args)->raw;
17-
$c->stash( $data->all );
15+
my ( $self, $c, $type, $name, @path ) = @_;
16+
my $fields = $c->res->fields;
17+
my $data = $c->model('ESQuery')
18+
->file->history( $type, $name, \@path, { fields => $fields } );
19+
$c->stash($data);
1820
}
1921

2022
1;

t/lib/MetaCPAN/Server/Test.pm

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ use strict;
44
use warnings;
55
use feature qw(state);
66

7+
use Carp qw( croak );
78
use HTTP::Request::Common qw( DELETE GET POST ); ## no perlimports
8-
use MetaCPAN::Model ();
9+
use MetaCPAN::ESConfig qw( es_doc_path );
910
use MetaCPAN::Server ();
1011
use MetaCPAN::Server::Config ();
1112
use MetaCPAN::Types::TypeTiny qw( ES );
13+
use MetaCPAN::Util qw( hit_total );
1214
use Plack::Test; ## no perlimports
1315

1416
use base 'Exporter';
1517
our @EXPORT_OK = qw(
1618
POST GET DELETE
1719
es
18-
model
20+
es_result
1921
test_psgi app
2022
query
2123
);
@@ -49,14 +51,32 @@ sub es {
4951
};
5052
}
5153

52-
sub model {
53-
state $model = MetaCPAN::Model->new( es => es() );
54-
}
55-
5654
sub query {
5755
state $query = MetaCPAN::Query->new( es => es() );
5856
}
5957

58+
sub es_result {
59+
my ( $type, $query, $size ) = @_;
60+
$size //= wantarray ? 999 : 1;
61+
if ( !wantarray && $size != 1 ) {
62+
croak "multiple results requested with scalar return!";
63+
}
64+
my $res = es()->search(
65+
es_doc_path($type),
66+
body => {
67+
size => ( wantarray ? 999 : 1 ),
68+
query => $query,
69+
},
70+
);
71+
my @hits = map $_->{_source}, @{ $res->{hits}{hits} };
72+
if ( !wantarray ) {
73+
croak "query did not return a single result"
74+
if hit_total($res) != 1;
75+
return $hits[0];
76+
}
77+
return @hits;
78+
}
79+
6080
1;
6181

6282
=pod

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

0 commit comments

Comments
 (0)