Lines 38-160
SKIP: {
Link Here
|
38 |
|
38 |
|
39 |
eval { Koha::SearchEngine::Elasticsearch->get_elasticsearch_params; }; |
39 |
eval { Koha::SearchEngine::Elasticsearch->get_elasticsearch_params; }; |
40 |
|
40 |
|
41 |
skip 'Elasticsearch configuration not available', 2 |
41 |
skip 'Elasticsearch configuration not available', 3 |
42 |
if $@; |
42 |
if $@; |
43 |
|
43 |
|
44 |
my $builder = t::lib::TestBuilder->new; |
44 |
my $builder = t::lib::TestBuilder->new; |
45 |
my $biblio = $builder->build_sample_biblio; # create biblio before we start mocking to avoid trouble indexing on creation |
45 |
my $biblio = |
46 |
|
46 |
$builder->build_sample_biblio; # create biblio before we start mocking to avoid trouble indexing on creation |
47 |
subtest 'create_index() tests' => sub { |
47 |
|
48 |
plan tests => 6; |
48 |
subtest 'create_index() tests' => sub { |
49 |
my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' ); |
49 |
plan tests => 6; |
50 |
$se->mock( '_read_configuration', sub { |
50 |
my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch'); |
51 |
my ($self, $sub ) = @_; |
51 |
$se->mock( |
52 |
my $method = $se->original( '_read_configuration' ); |
52 |
'_read_configuration', |
53 |
my $conf = $method->( $self ); |
53 |
sub { |
54 |
$conf->{index_name} .= '__test'; |
54 |
my ( $self, $sub ) = @_; |
55 |
return $conf; |
55 |
my $method = $se->original('_read_configuration'); |
56 |
}); |
56 |
my $conf = $method->($self); |
57 |
|
57 |
$conf->{index_name} .= '__test'; |
58 |
my $indexer; |
58 |
return $conf; |
59 |
ok( |
59 |
} |
60 |
$indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }), |
60 |
); |
61 |
'Creating a new indexer object' |
61 |
|
62 |
); |
62 |
my $indexer; |
63 |
|
63 |
ok( |
64 |
is( |
64 |
$indexer = Koha::SearchEngine::Elasticsearch::Indexer->new( { 'index' => 'biblios' } ), |
65 |
$indexer->create_index(), |
65 |
'Creating a new indexer object' |
66 |
Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_OK(), |
66 |
); |
67 |
'Creating an index' |
67 |
|
68 |
); |
68 |
is( |
69 |
|
69 |
$indexer->create_index(), |
70 |
my $marc_record = MARC::Record->new(); |
70 |
Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_OK(), |
71 |
$marc_record->append_fields( |
71 |
'Creating an index' |
72 |
MARC::Field->new('001', '1234567'), |
72 |
); |
73 |
MARC::Field->new('020', '', '', 'a' => '1234567890123'), |
73 |
|
74 |
MARC::Field->new('245', '', '', 'a' => 'Title') |
74 |
my $marc_record = MARC::Record->new(); |
75 |
); |
75 |
$marc_record->append_fields( |
76 |
my $records = [$marc_record]; |
76 |
MARC::Field->new( '001', '1234567' ), |
77 |
|
77 |
MARC::Field->new( '020', '', '', 'a' => '1234567890123' ), |
78 |
my $response = $indexer->update_index([1], $records); |
78 |
MARC::Field->new( '245', '', '', 'a' => 'Title' ) |
79 |
is( $response->{errors}, 0, "no error on update_index" ); |
79 |
); |
80 |
is( scalar(@{$response->{items}}), 1, "1 item indexed" ); |
80 |
my $records = [$marc_record]; |
81 |
is( $response->{items}[0]->{index}->{_id},"1", "We should get a string matching the bibnumber passed in"); |
81 |
|
82 |
|
82 |
my $response = $indexer->update_index( [1], $records ); |
83 |
is( |
83 |
is( $response->{errors}, 0, "no error on update_index" ); |
84 |
$indexer->drop_index(), |
84 |
is( scalar( @{ $response->{items} } ), 1, "1 item indexed" ); |
85 |
Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_RECREATE_REQUIRED(), |
85 |
is( $response->{items}[0]->{index}->{_id}, "1", "We should get a string matching the bibnumber passed in" ); |
86 |
'Dropping the index' |
86 |
|
87 |
); |
87 |
is( |
88 |
}; |
88 |
$indexer->drop_index(), |
89 |
|
89 |
Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_RECREATE_REQUIRED(), |
90 |
subtest 'index_records() tests' => sub { |
90 |
'Dropping the index' |
91 |
plan tests => 4; |
91 |
); |
92 |
my $mock_index = Test::MockModule->new("Koha::SearchEngine::Elasticsearch::Indexer"); |
92 |
}; |
93 |
$mock_index->mock( update_index => sub { |
93 |
|
94 |
my ($self, $record_ids, $records) = @_; |
94 |
subtest 'index_records() tests' => sub { |
95 |
warn "Update " . $record_ids->[0] . $records->[0]->as_usmarc; |
95 |
plan tests => 4; |
96 |
}); |
96 |
my $mock_index = Test::MockModule->new("Koha::SearchEngine::Elasticsearch::Indexer"); |
97 |
$mock_index->mock( update_index_background => sub { |
97 |
$mock_index->mock( |
98 |
my ($self, $record_ids) = @_; |
98 |
update_index => sub { |
99 |
warn "Update background " . $record_ids->[0]; |
99 |
my ( $self, $record_ids, $records ) = @_; |
100 |
}); |
100 |
warn "Update " . $record_ids->[0] . $records->[0]->as_usmarc; |
101 |
|
101 |
} |
102 |
my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'authorities' }); |
102 |
); |
103 |
|
103 |
$mock_index->mock( |
104 |
my $marc_record = MARC::Record->new(); |
104 |
update_index_background => sub { |
105 |
$marc_record->append_fields( |
105 |
my ( $self, $record_ids ) = @_; |
106 |
MARC::Field->new('001', '1234567'), |
106 |
warn "Update background " . $record_ids->[0]; |
107 |
MARC::Field->new('100', '', '', 'a' => 'Rosenstock, Jeff'), |
107 |
} |
108 |
); |
108 |
); |
109 |
warning_is { |
109 |
|
110 |
$indexer->index_records( [42], 'specialUpdate', 'authorityserver', |
110 |
my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new( { 'index' => 'authorities' } ); |
111 |
[$marc_record] ); |
111 |
|
112 |
} |
112 |
my $marc_record = MARC::Record->new(); |
113 |
"Update 42" . $marc_record->as_usmarc, |
113 |
$marc_record->append_fields( |
114 |
"When passing record and ids to index_records they are correctly passed through to update_index"; |
114 |
MARC::Field->new( '001', '1234567' ), |
115 |
|
115 |
MARC::Field->new( '100', '', '', 'a' => 'Rosenstock, Jeff' ), |
116 |
$indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }); |
116 |
); |
117 |
$marc_record = $biblio->metadata->record({ embed_items => 1 }); |
117 |
warning_is { |
118 |
warning_is { |
118 |
$indexer->index_records( |
119 |
$indexer->index_records( [ $biblio->biblionumber ], |
119 |
[42], 'specialUpdate', 'authorityserver', |
120 |
'specialUpdate', 'biblioserver' ); |
120 |
[$marc_record] |
121 |
} |
121 |
); |
122 |
"Update background " . $biblio->biblionumber, |
|
|
123 |
"When passing id only to index_records the marc record is fetched and passed through to update_index"; |
124 |
|
125 |
my $chunks = 0; |
126 |
$mock_index->mock( |
127 |
update_index => sub { |
128 |
my ( $self, $record_ids, $records ) = @_; |
129 |
$chunks++; |
130 |
} |
122 |
} |
131 |
); |
123 |
"Update 42" . $marc_record->as_usmarc, |
132 |
|
124 |
"When passing record and ids to index_records they are correctly passed through to update_index"; |
133 |
t::lib::Mocks::mock_config( 'elasticsearch', { server => 'false', index_name => 'pseudo' } ); |
125 |
|
134 |
my @big_array = 1 .. 10000; |
126 |
$indexer = Koha::SearchEngine::Elasticsearch::Indexer->new( { 'index' => 'biblios' } ); |
135 |
$indexer->index_records( \@big_array, 'specialUpdate', 'biblioserver', \@big_array ); |
127 |
$marc_record = $biblio->metadata->record( { embed_items => 1 } ); |
136 |
is( $chunks, 2, "We split 10000 records into two chunks when chunk size not set" ); |
128 |
warning_is { |
137 |
|
129 |
$indexer->index_records( |
138 |
$chunks = 0; |
130 |
[ $biblio->biblionumber ], |
139 |
t::lib::Mocks::mock_config( 'elasticsearch', { server => 'false', index_name => 'pseudo', chunk_size => 10 } ); |
131 |
'specialUpdate', 'biblioserver' |
140 |
$indexer->index_records( \@big_array, 'specialUpdate', 'biblioserver', \@big_array ); |
132 |
); |
141 |
is( $chunks, 1000, "We split 10000 records into 1000 chunks when chunk size is 10" ); |
133 |
} |
142 |
|
134 |
"Update background " . $biblio->biblionumber, |
143 |
}; |
135 |
"When passing id only to index_records the marc record is fetched and passed through to update_index"; |
144 |
|
136 |
|
145 |
subtest 'update_index' => sub { |
137 |
my $chunks = 0; |
146 |
plan tests => 1; |
138 |
$mock_index->mock( |
147 |
|
139 |
update_index => sub { |
148 |
my $biblio = $builder->build_sample_biblio; |
140 |
my ( $self, $record_ids, $records ) = @_; |
149 |
my $biblionumber = $biblio->biblionumber; |
141 |
$chunks++; |
150 |
$biblio->delete; |
142 |
} |
151 |
|
143 |
); |
152 |
my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }); |
144 |
|
153 |
warning_is { |
145 |
t::lib::Mocks::mock_config( 'elasticsearch', { server => 'false', index_name => 'pseudo' } ); |
154 |
$indexer->update_index([$biblionumber]); |
146 |
my @big_array = 1 .. 10000; |
|
|
147 |
$indexer->index_records( \@big_array, 'specialUpdate', 'biblioserver', \@big_array ); |
148 |
is( $chunks, 2, "We split 10000 records into two chunks when chunk size not set" ); |
149 |
|
150 |
$chunks = 0; |
151 |
t::lib::Mocks::mock_config( |
152 |
'elasticsearch', |
153 |
{ server => 'false', index_name => 'pseudo', chunk_size => 10 } |
154 |
); |
155 |
$indexer->index_records( \@big_array, 'specialUpdate', 'biblioserver', \@big_array ); |
156 |
is( $chunks, 1000, "We split 10000 records into 1000 chunks when chunk size is 10" ); |
157 |
|
158 |
}; |
159 |
|
160 |
subtest 'update_index' => sub { |
161 |
plan tests => 1; |
162 |
|
163 |
my $biblio = $builder->build_sample_biblio; |
164 |
my $biblionumber = $biblio->biblionumber; |
165 |
$biblio->delete; |
166 |
|
167 |
my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new( { 'index' => 'biblios' } ); |
168 |
warning_is { |
169 |
$indexer->update_index( [$biblionumber] ); |
155 |
|
170 |
|
156 |
} "", "update_index called with deleted biblionumber should not crash"; |
171 |
} |
|
|
172 |
"", "update_index called with deleted biblionumber should not crash"; |
157 |
|
173 |
|
158 |
}; |
174 |
}; |
159 |
|
175 |
|
160 |
} |
176 |
} |
161 |
- |
|
|