|
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('marc_type'), 'marc21', 'And the search mapping "marc_type" matches'); |
| 88 |
is($mapping->get_column('marc_field'), '007_/1', 'And the search mapping "marc_field" matches'); |
| 89 |
|
| 90 |
ok(1, 'Scenario: Get all search mappings'); |
| 91 |
$mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios'}); |
| 92 |
ok($mappings, 'When search mappings are fetched'); |
| 93 |
ok($mappings->count() > 10, 'Then we have "'.$mappings->count().'" search mappings :)') |
| 94 |
} |
| 95 |
|
| 96 |
subtest "Add a search mapping", \&add_mapping; |
| 97 |
sub add_mapping { |
| 98 |
my ($rv, $mappings, $mapping, $count); |
| 99 |
|
| 100 |
ok(1, "Scenario: Add the same mapping twice and hope for no duplicate mappings"); |
| 101 |
$rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test', |
| 102 |
label => 'original language', |
| 103 |
type => 'keyword', |
| 104 |
index_name => 'biblios', |
| 105 |
marc_type => 'marc21', |
| 106 |
marc_field => '024a', |
| 107 |
facet => 1, |
| 108 |
suggestible => 1, |
| 109 |
sort => 1}); |
| 110 |
$rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test', |
| 111 |
label => 'original language', |
| 112 |
type => 'keyword', |
| 113 |
index_name => 'biblios', |
| 114 |
marc_type => 'marc21', |
| 115 |
marc_field => '024a', |
| 116 |
facet => 1, |
| 117 |
suggestible => 1, |
| 118 |
sort => 1}); |
| 119 |
ok(1, "When the same search mapping is added twice"); |
| 120 |
|
| 121 |
$mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios', name => 'ln-test'}); |
| 122 |
$count = $mappings->count(); |
| 123 |
is($count, 1, "Then we received only one mapping from the database"); |
| 124 |
} |
| 125 |
|
| 126 |
done_testing; |
| 127 |
|