Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2015 Catalyst IT |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl qw(2014); |
21 |
use utf8; |
22 |
use Test::More; |
23 |
|
24 |
use Koha::SearchEngine::Elasticsearch; |
25 |
use Koha::SearchMappingManager; |
26 |
|
27 |
|
28 |
subtest "Reset Elasticsearch mappings", \&reset_elasticsearch_mappings; |
29 |
sub reset_elasticsearch_mappings { |
30 |
my ($rv, $mappings, $count, $mapping); |
31 |
|
32 |
ok(1, 'Scenario: Reset Elasticsearch mappings to an empty database'); |
33 |
#There might or might not be any mappings. Whatever the initial status is, make sure we start from empty tables |
34 |
$rv = Koha::SearchMappingManager::flush(); |
35 |
ok($rv, 'Given empty search mapping tables'); |
36 |
eval { |
37 |
$rv = Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings(); |
38 |
}; |
39 |
if ($@){ |
40 |
ok(0, $@); |
41 |
} |
42 |
ok(not($@), 'When reset_elasticsearch_mappings() has been ran'); |
43 |
|
44 |
$mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios'}); |
45 |
$count = $mappings->count(); |
46 |
ok($count, 'Then search mapping tables have been populated'); |
47 |
|
48 |
|
49 |
|
50 |
ok(1, 'Scenario: Reset Elasticsearch mappings when custom mappings already exist'); |
51 |
$rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test', |
52 |
label => 'original language', |
53 |
type => 'keyword', |
54 |
index_name => 'biblios', |
55 |
marc_type => 'marc21', |
56 |
marc_field => '024a', |
57 |
facet => 1, |
58 |
suggestible => 1, |
59 |
sort => 1}); |
60 |
ok($rv, 'Given a mapping table with a custom search field'); |
61 |
eval { |
62 |
$rv = Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings(); |
63 |
}; |
64 |
if ($@){ |
65 |
ok(0, $@); |
66 |
} |
67 |
ok(not($@), 'When reset_elasticsearch_mappings() has been ran'); |
68 |
|
69 |
$mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios'}); |
70 |
$count = $mappings->count(); |
71 |
ok($count > 10, 'Then search mapping tables have been populated'); |
72 |
} |
73 |
|
74 |
subtest "Get Elasticsearch mappings", \&get_search_mappings; |
75 |
sub get_search_mappings { |
76 |
my ($mappings, $mapping); |
77 |
|
78 |
ok(1, 'Scenario: Get a single search mapping by name'); |
79 |
$mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios', name => 'ff7-00'}); |
80 |
ok($mappings, 'When a search mappings is fetched'); |
81 |
$mapping = $mappings->next(); |
82 |
is($mapping->get_column('name'), 'ff7-00', 'Then the search mapping "name" matches'); |
83 |
is($mapping->get_column('type'), '', 'And the search mapping "type" matches'); |
84 |
is($mapping->get_column('facet'), '0', 'And the search mapping "facet" matches'); |
85 |
is($mapping->get_column('suggestible'), '0', 'And the search mapping "suggestible" matches'); |
86 |
is($mapping->get_column('sort'), undef, 'And the search mapping "sort" matches'); |
87 |
is($mapping->get_column('search'), '1', 'And the search mapping "search" matches'); |
88 |
is($mapping->get_column('marc_type'), 'marc21', 'And the search mapping "marc_type" matches'); |
89 |
is($mapping->get_column('marc_field'), '007_/0', 'And the search mapping "marc_field" matches'); |
90 |
|
91 |
ok(1, 'Scenario: Get all search mappings'); |
92 |
$mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios'}); |
93 |
ok($mappings, 'When search mappings are fetched'); |
94 |
ok($mappings->count() > 10, 'Then we have "'.$mappings->count().'" search mappings :)') |
95 |
} |
96 |
|
97 |
subtest "Add a search mapping", \&add_mapping; |
98 |
sub add_mapping { |
99 |
my ($rv, $mappings, $mapping, $count); |
100 |
|
101 |
ok(1, "Scenario: Add the same mapping twice and hope for no duplicate mappings"); |
102 |
$rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test', |
103 |
label => 'original language', |
104 |
type => 'keyword', |
105 |
index_name => 'biblios', |
106 |
marc_type => 'marc21', |
107 |
marc_field => '024a', |
108 |
facet => 1, |
109 |
suggestible => 1, |
110 |
sort => 1}); |
111 |
$rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test', |
112 |
label => 'original language', |
113 |
type => 'keyword', |
114 |
index_name => 'biblios', |
115 |
marc_type => 'marc21', |
116 |
marc_field => '024a', |
117 |
facet => 1, |
118 |
suggestible => 1, |
119 |
sort => 1}); |
120 |
ok(1, "When the same search mapping is added twice"); |
121 |
|
122 |
$mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios', name => 'ln-test'}); |
123 |
$count = $mappings->count(); |
124 |
is($count, 1, "Then we received only one mapping from the database"); |
125 |
} |
126 |
|
127 |
done_testing; |