View | Details | Raw Unified | Return to bug 13937
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Z3950Responder/GenericSession.t (+129 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 3;
6
use t::lib::Mocks qw(mock_preference);
7
8
use YAML;
9
use ZOOM;
10
11
BEGIN {
12
    use_ok('Koha::Z3950Responder');
13
    use_ok('Koha::Z3950Responder::GenericSession');
14
}
15
16
our $child;
17
18
subtest 'test_search' => sub {
19
20
    plan tests => 9;
21
22
    t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
23
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'),
34
    );
35
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'),
46
    );
47
48
    my $yaml = new Test::MockModule('YAML');
49
    $yaml->mock('LoadFile', sub {
50
        return {
51
            biblios => {
52
                use => {
53
                    1 => 'author',
54
                    4 => 'title'
55
                }
56
            }
57
        };
58
    });
59
60
    my $builder = new Test::MockModule('Koha::SearchEngine::Elasticsearch::QueryBuilder');
61
    $builder->mock('build_query_compat', sub {
62
        my ( $self, $operators, $operands ) = @_;
63
64
        return (undef, $operands->[0]);
65
    });
66
67
    my $search = new Test::MockModule('Koha::SearchEngine::Elasticsearch::Search');
68
    $search->mock('simple_search_compat', sub {
69
        my ( $self, $query ) = @_;
70
71
        return (1, undef, 0) unless $query eq '((author:(author)) AND (title:(title)))';
72
73
        my @records = ($marc_record_1, $marc_record_2);
74
        return (undef, \@records, 2);
75
    });
76
77
    $child = fork();
78
    if ($child == 0) {
79
        my @yaz_options = ( '@:42111' );
80
        my $z = Koha::Z3950Responder->new( {
81
            config_dir => '',
82
            yaz_options => [ @yaz_options ]
83
        });
84
        $z->start();
85
        exit;
86
    }
87
    sleep(1);
88
89
    my $o = new ZOOM::Options();
90
    $o->option(preferredRecordSyntax => 'xml');
91
    $o->option(elementSetName => 'marcxml');
92
    $o->option(databaseName => 'biblios');
93
94
    my $Zconn = ZOOM::Connection->create($o);
95
    ok($Zconn, 'ZOOM connection created');
96
97
    $Zconn->connect('127.0.0.1:42111', 0);
98
    is($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
99
100
    my $rs = $Zconn->search_pqf('@and @attr 1=1 author @attr 1=4 title');
101
    is($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
102
103
    is($rs->size(), 2, 'Two results returned');
104
105
    my $returned1 = MARC::Record->new_from_xml($rs->record(0)->raw());
106
    ok ($returned1, 'Record 1 returned as MARCXML');
107
    is($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
108
109
    my $returned2= MARC::Record->new_from_xml($rs->record(1)->raw());
110
    ok ($returned2, 'Record 2 returned as MARCXML');
111
    is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
112
113
    is($rs->record(2), undef, 'Record 3 does not exist');
114
115
    cleanup();
116
};
117
118
sub cleanup {
119
    if ($child) {
120
        kill 9, $child;
121
        $child = undef;
122
    }
123
}
124
125
# Fall back to make sure that the Zebra process
126
# and files get cleaned up
127
END {
128
    cleanup();
129
}
(-)a/t/db_dependent/Koha/Z3950Responder/ZebraSession.t (-1 / +133 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 3;
6
use Test::MockObject;
7
use t::lib::Mocks qw(mock_preference);
8
9
use YAML;
10
use ZOOM;
11
12
BEGIN {
13
    use_ok('Koha::Z3950Responder');
14
    use_ok('Koha::Z3950Responder::ZebraSession');
15
}
16
17
our $child;
18
19
subtest 'test_search' => sub {
20
21
    plan tests => 9;
22
23
    t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
24
25
    my $marc_record_1 = MARC::Record->new();
26
    $marc_record_1->leader('     cam  22      a 4500');
27
    $marc_record_1->append_fields(
28
        MARC::Field->new('001', '123'),
29
        MARC::Field->new('020', '', '', a => '1-56619-909-3'),
30
        MARC::Field->new('100', '', '', a => 'Author 1'),
31
        MARC::Field->new('110', '', '', a => 'Corp Author'),
32
        MARC::Field->new('210', '', '', a => 'Title 1'),
33
        MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
34
        MARC::Field->new('999', '', '', c => '1234567'),
35
    );
36
37
    my $marc_record_2 = MARC::Record->new();
38
    $marc_record_2->leader('     cam  22      a 4500');
39
    $marc_record_2->append_fields(
40
        MARC::Field->new('001', '234'),
41
        MARC::Field->new('020', '', '', a => '1-56619-909-3'),
42
        MARC::Field->new('100', '', '', a => 'Author 2'),
43
        MARC::Field->new('110', '', '', a => 'Corp Author'),
44
        MARC::Field->new('210', '', '', a => 'Title 2'),
45
        MARC::Field->new('245', '', '', a => 'Title:', b => 'second record'),
46
        MARC::Field->new('999', '', '', c => '1234567'),
47
    );
48
49
    my $context = new Test::MockModule('C4::Context');
50
    $context->mock('Zconn', sub {
51
        my $Zconn = new Test::MockObject();
52
        $Zconn->mock('connect', sub {});
53
        $Zconn->mock('err_code', sub {
54
            return 0;
55
        });
56
        $Zconn->mock('search_pqf', sub {
57
            my $results = new Test::MockObject();
58
            $results->mock('size', sub {
59
                return 2;
60
            });
61
            $results->mock('record_immediate', sub {
62
                my ($self, $index) = @_;
63
64
                my $record;
65
                if ($index == 0) {
66
                    $record = $marc_record_1;
67
                } elsif ($index == 1) {
68
                    $record = $marc_record_2;
69
                }
70
                my $Zrecord = new Test::MockObject();
71
                $Zrecord->mock('raw', sub {
72
                    return $record->as_xml();
73
                });
74
                return $Zrecord;
75
            });
76
            $results->mock('records', sub {});
77
            $results->mock('destroy', sub {});
78
        });
79
    });
80
81
    $child = fork();
82
    if ($child == 0) {
83
        my @yaz_options = ( '@:42111' );
84
        my $z = Koha::Z3950Responder->new( {
85
            config_dir => '',
86
            yaz_options => [ @yaz_options ]
87
        });
88
        $z->start();
89
        exit;
90
    }
91
    sleep(1);
92
93
    my $o = new ZOOM::Options();
94
    $o->option(preferredRecordSyntax => 'xml');
95
    $o->option(elementSetName => 'marcxml');
96
    $o->option(databaseName => 'biblios');
97
98
    my $Zconn = ZOOM::Connection->create($o);
99
    ok($Zconn, 'ZOOM connection created');
100
101
    $Zconn->connect('127.0.0.1:42111', 0);
102
    is($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
103
104
    my $rs = $Zconn->search_pqf('@and @attr 1=1 author @attr 1=4 title');
105
    is($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
106
107
    is($rs->size(), 2, 'Two results returned');
108
109
    my $returned1 = MARC::Record->new_from_xml($rs->record(0)->raw());
110
    ok ($returned1, 'Record 1 returned as MARCXML');
111
    is($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
112
113
    my $returned2= MARC::Record->new_from_xml($rs->record(1)->raw());
114
    ok ($returned2, 'Record 2 returned as MARCXML');
115
    is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
116
117
    is($rs->record(2), undef, 'Record 3 does not exist');
118
119
    cleanup();
120
};
121
122
sub cleanup {
123
    if ($child) {
124
        kill 9, $child;
125
        $child = undef;
126
    }
127
}
128
129
# Fall back to make sure that the Zebra process
130
# and files get cleaned up
131
END {
132
    cleanup();
133
}

Return to bug 13937