Bug 13937: Add tests for search and retrieval

Sponsored-by: National Library of Finland
Signed-off-by: Stefan Berndtsson <stefan.berndtsson@ub.gu.se>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Ere Maijala 2018-11-27 13:40:44 +02:00 committed by Martin Renvoize
parent ac28d8043d
commit fe59ab8165
Signed by: martin.renvoize
GPG key ID: 422B469130441A0F
2 changed files with 262 additions and 0 deletions

View file

@ -0,0 +1,129 @@
#!/usr/bin/perl
use Modern::Perl;
use Test::More tests => 3;
use t::lib::Mocks qw(mock_preference);
use YAML;
use ZOOM;
BEGIN {
use_ok('Koha::Z3950Responder');
use_ok('Koha::Z3950Responder::GenericSession');
}
our $child;
subtest 'test_search' => sub {
plan tests => 9;
t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
my $marc_record_1 = MARC::Record->new();
$marc_record_1->leader(' cam 22 a 4500');
$marc_record_1->append_fields(
MARC::Field->new('001', '123'),
MARC::Field->new('020', '', '', a => '1-56619-909-3'),
MARC::Field->new('100', '', '', a => 'Author 1'),
MARC::Field->new('110', '', '', a => 'Corp Author'),
MARC::Field->new('210', '', '', a => 'Title 1'),
MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
MARC::Field->new('999', '', '', c => '1234567'),
);
my $marc_record_2 = MARC::Record->new();
$marc_record_2->leader(' cam 22 a 4500');
$marc_record_2->append_fields(
MARC::Field->new('001', '234'),
MARC::Field->new('020', '', '', a => '1-56619-909-3'),
MARC::Field->new('100', '', '', a => 'Author 2'),
MARC::Field->new('110', '', '', a => 'Corp Author'),
MARC::Field->new('210', '', '', a => 'Title 2'),
MARC::Field->new('245', '', '', a => 'Title:', b => 'second record'),
MARC::Field->new('999', '', '', c => '1234567'),
);
my $yaml = new Test::MockModule('YAML');
$yaml->mock('LoadFile', sub {
return {
biblios => {
use => {
1 => 'author',
4 => 'title'
}
}
};
});
my $builder = new Test::MockModule('Koha::SearchEngine::Elasticsearch::QueryBuilder');
$builder->mock('build_query_compat', sub {
my ( $self, $operators, $operands ) = @_;
return (undef, $operands->[0]);
});
my $search = new Test::MockModule('Koha::SearchEngine::Elasticsearch::Search');
$search->mock('simple_search_compat', sub {
my ( $self, $query ) = @_;
return (1, undef, 0) unless $query eq '((author:(author)) AND (title:(title)))';
my @records = ($marc_record_1, $marc_record_2);
return (undef, \@records, 2);
});
$child = fork();
if ($child == 0) {
my @yaz_options = ( '@:42111' );
my $z = Koha::Z3950Responder->new( {
config_dir => '',
yaz_options => [ @yaz_options ]
});
$z->start();
exit;
}
sleep(1);
my $o = new ZOOM::Options();
$o->option(preferredRecordSyntax => 'xml');
$o->option(elementSetName => 'marcxml');
$o->option(databaseName => 'biblios');
my $Zconn = ZOOM::Connection->create($o);
ok($Zconn, 'ZOOM connection created');
$Zconn->connect('127.0.0.1:42111', 0);
is($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
my $rs = $Zconn->search_pqf('@and @attr 1=1 author @attr 1=4 title');
is($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
is($rs->size(), 2, 'Two results returned');
my $returned1 = MARC::Record->new_from_xml($rs->record(0)->raw());
ok ($returned1, 'Record 1 returned as MARCXML');
is($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
my $returned2= MARC::Record->new_from_xml($rs->record(1)->raw());
ok ($returned2, 'Record 2 returned as MARCXML');
is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
is($rs->record(2), undef, 'Record 3 does not exist');
cleanup();
};
sub cleanup {
if ($child) {
kill 9, $child;
$child = undef;
}
}
# Fall back to make sure that the Zebra process
# and files get cleaned up
END {
cleanup();
}

View file

@ -0,0 +1,133 @@
#!/usr/bin/perl
use Modern::Perl;
use Test::More tests => 3;
use Test::MockObject;
use t::lib::Mocks qw(mock_preference);
use YAML;
use ZOOM;
BEGIN {
use_ok('Koha::Z3950Responder');
use_ok('Koha::Z3950Responder::ZebraSession');
}
our $child;
subtest 'test_search' => sub {
plan tests => 9;
t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
my $marc_record_1 = MARC::Record->new();
$marc_record_1->leader(' cam 22 a 4500');
$marc_record_1->append_fields(
MARC::Field->new('001', '123'),
MARC::Field->new('020', '', '', a => '1-56619-909-3'),
MARC::Field->new('100', '', '', a => 'Author 1'),
MARC::Field->new('110', '', '', a => 'Corp Author'),
MARC::Field->new('210', '', '', a => 'Title 1'),
MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
MARC::Field->new('999', '', '', c => '1234567'),
);
my $marc_record_2 = MARC::Record->new();
$marc_record_2->leader(' cam 22 a 4500');
$marc_record_2->append_fields(
MARC::Field->new('001', '234'),
MARC::Field->new('020', '', '', a => '1-56619-909-3'),
MARC::Field->new('100', '', '', a => 'Author 2'),
MARC::Field->new('110', '', '', a => 'Corp Author'),
MARC::Field->new('210', '', '', a => 'Title 2'),
MARC::Field->new('245', '', '', a => 'Title:', b => 'second record'),
MARC::Field->new('999', '', '', c => '1234567'),
);
my $context = new Test::MockModule('C4::Context');
$context->mock('Zconn', sub {
my $Zconn = new Test::MockObject();
$Zconn->mock('connect', sub {});
$Zconn->mock('err_code', sub {
return 0;
});
$Zconn->mock('search_pqf', sub {
my $results = new Test::MockObject();
$results->mock('size', sub {
return 2;
});
$results->mock('record_immediate', sub {
my ($self, $index) = @_;
my $record;
if ($index == 0) {
$record = $marc_record_1;
} elsif ($index == 1) {
$record = $marc_record_2;
}
my $Zrecord = new Test::MockObject();
$Zrecord->mock('raw', sub {
return $record->as_xml();
});
return $Zrecord;
});
$results->mock('records', sub {});
$results->mock('destroy', sub {});
});
});
$child = fork();
if ($child == 0) {
my @yaz_options = ( '@:42111' );
my $z = Koha::Z3950Responder->new( {
config_dir => '',
yaz_options => [ @yaz_options ]
});
$z->start();
exit;
}
sleep(1);
my $o = new ZOOM::Options();
$o->option(preferredRecordSyntax => 'xml');
$o->option(elementSetName => 'marcxml');
$o->option(databaseName => 'biblios');
my $Zconn = ZOOM::Connection->create($o);
ok($Zconn, 'ZOOM connection created');
$Zconn->connect('127.0.0.1:42111', 0);
is($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
my $rs = $Zconn->search_pqf('@and @attr 1=1 author @attr 1=4 title');
is($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
is($rs->size(), 2, 'Two results returned');
my $returned1 = MARC::Record->new_from_xml($rs->record(0)->raw());
ok ($returned1, 'Record 1 returned as MARCXML');
is($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
my $returned2= MARC::Record->new_from_xml($rs->record(1)->raw());
ok ($returned2, 'Record 2 returned as MARCXML');
is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
is($rs->record(2), undef, 'Record 3 does not exist');
cleanup();
};
sub cleanup {
if ($child) {
kill 9, $child;
$child = undef;
}
}
# Fall back to make sure that the Zebra process
# and files get cleaned up
END {
cleanup();
}