5 use Test::More tests => 3;
7 use t::lib::Mocks qw(mock_preference);
12 use_ok('Koha::Z3950Responder');
13 use_ok('Koha::Z3950Responder::ZebraSession');
18 subtest 'test_search' => sub {
22 t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
24 my $marc_record_1 = MARC::Record->new();
25 $marc_record_1->leader(' cam 22 a 4500');
26 $marc_record_1->append_fields(
27 MARC::Field->new('001', '123'),
28 MARC::Field->new('020', '', '', a => '1-56619-909-3'),
29 MARC::Field->new('100', '', '', a => 'Author 1'),
30 MARC::Field->new('110', '', '', a => 'Corp Author'),
31 MARC::Field->new('210', '', '', a => 'Title 1'),
32 MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
33 MARC::Field->new('999', '', '', c => '1234567'),
36 my $marc_record_2 = MARC::Record->new();
37 $marc_record_2->leader(' cam 22 a 4500');
38 $marc_record_2->append_fields(
39 MARC::Field->new('001', '234'),
40 MARC::Field->new('020', '', '', a => '1-56619-909-3'),
41 MARC::Field->new('100', '', '', a => 'Author 2'),
42 MARC::Field->new('110', '', '', a => 'Corp Author'),
43 MARC::Field->new('210', '', '', a => 'Title 2'),
44 MARC::Field->new('245', '', '', a => 'Title:', b => 'second record'),
45 MARC::Field->new('999', '', '', c => '1234567'),
48 my $context = Test::MockModule->new('C4::Context');
49 $context->mock('Zconn', sub {
50 my $Zconn = Test::MockObject->new();
51 $Zconn->mock('connect', sub {});
52 $Zconn->mock('err_code', sub {
55 $Zconn->mock('search_pqf', sub {
56 my $results = Test::MockObject->new();
57 $results->mock('size', sub {
60 $results->mock('record_immediate', sub {
61 my ($self, $index) = @_;
65 $record = $marc_record_1;
66 } elsif ($index == 1) {
67 $record = $marc_record_2;
69 my $Zrecord = Test::MockObject->new();
70 $Zrecord->mock('raw', sub {
71 return $record->as_xml();
75 $results->mock('records', sub {});
76 $results->mock('destroy', sub {});
82 my @yaz_options = ( '@:42111' );
83 my $z = Koha::Z3950Responder->new( {
85 yaz_options => [ @yaz_options ]
90 sleep(10); # Just a try to see if it fixes Jenkins
92 my $o = ZOOM::Options->new();
93 $o->option(preferredRecordSyntax => 'xml');
94 $o->option(elementSetName => 'marcxml');
95 $o->option(databaseName => 'biblios');
97 my $Zconn = ZOOM::Connection->create($o);
98 ok($Zconn, 'ZOOM connection created');
100 $Zconn->connect('127.0.0.1:42111', 0);
101 is($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
103 my $rs = $Zconn->search_pqf('@and @attr 1=1 author @attr 1=4 title');
104 is($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
106 is($rs->size(), 2, 'Two results returned');
108 my $returned1 = MARC::Record->new_from_xml($rs->record(0)->raw(), 'UTF-8');
109 ok ($returned1, 'Record 1 returned as MARCXML');
110 is($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
112 my $returned2= MARC::Record->new_from_xml($rs->record(1)->raw(), 'UTF-8');
113 ok ($returned2, 'Record 2 returned as MARCXML');
114 is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
126 # Fall back to make sure that the Zebra process
127 # and files get cleaned up