Bug 15905 - Update unit tests
[koha.git] / t / db_dependent / Koha_SearchEngine_Elasticsearch_Search.t
1 # Copyright 2015 Catalyst IT
2 #
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 11;
21
22 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
23
24 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
25
26 use_ok('Koha::SearchEngine::Elasticsearch::Search');
27
28 ok(
29     my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
30         { 'nodes' => ['localhost:9200'], 'index' => 'mydb' }
31     ),
32     'Creating a Koha::SearchEngine::Elasticsearch::Search object'
33 );
34
35 is( $searcher->index, 'mydb', 'Testing basic accessor' );
36
37 ok( my $query = $builder->build_query('easy'), 'Build a search query');
38
39 SKIP: {
40
41     eval { $builder->get_elasticsearch_params; };
42
43     skip 'ElasticSeatch configuration not available', 6
44         if $@;
45
46     ok( my $results = $searcher->search( $query) , 'Do a search ' );
47
48     ok( my $marc = $searcher->json2marc( $results->first ), 'Convert JSON to MARC');
49
50     is (my $count = $searcher->count( $query ), 0 , 'Get a count of the results, without returning results ');
51
52     ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
53
54     ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
55
56     is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
57
58 }
59
60 subtest 'json2marc' => sub {
61     plan tests => 4;
62     my $leader = '00626nam a2200193   4500';
63     my $_001 = 42;
64     my $_010a = '123456789';
65     my $_010d = 145;
66     my $_200a = 'a title';
67     my $json = [ # It's not a JSON, see the POD of json2marc
68         [ 'LDR', undef, undef, '_', $leader ],
69         [ '001', undef, undef, '_', $_001 ],
70         [ '010', ' ', ' ', 'a', $_010a, 'd', $_010d ],
71         [ '200', '1', ' ', 'a', $_200a, ], # Yes UNIMARC but we don't mind here
72     ];
73
74     my $marc = $searcher->json2marc( $json );
75     is( $marc->leader, $leader, );
76     is( $marc->field('001')->data, $_001, );
77     is( $marc->subfield('010', 'a'), $_010a, );
78     is( $marc->subfield('200', 'a'), $_200a, );
79
80 };
81
82 1;