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

(-)a/t/Koha/SearchEngine/ElasticSearch/Indexer.t (-78 lines)
Lines 1-78 Link Here
1
#!/usr/bin/perl
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 => 1;
21
use Test::Exception;
22
23
use t::lib::Mocks;
24
25
use Test::MockModule;
26
27
use MARC::Record;
28
use Try::Tiny;
29
30
use Koha::SearchEngine::Elasticsearch;
31
use Koha::SearchEngine::Elasticsearch::Indexer;
32
33
subtest '_sanitise_records() tests' => sub {
34
    plan tests => 5;
35
36
    my $indexer;
37
    ok(
38
        $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }),
39
        'Creating a new indexer object'
40
    );
41
42
    my $record1 = MARC::Record->new();
43
    $record1->append_fields(
44
        MARC::Field->new('999', '', '', 'c' => 1, 'd' => 2)
45
    );
46
47
    my $record2 = MARC::Record->new();
48
    $record2->append_fields(
49
        MARC::Field->new('999', '', '', 'c' => 3, 'd' => 4)
50
    );
51
    my $records = [$record1, $record2];
52
53
    my $biblionumbers = [5, 6];
54
55
    $indexer->_sanitise_records($biblionumbers, $records);
56
57
    is(
58
        $record1->subfield('999', 'c'),
59
        '5',
60
        'First record has 5 in 999c'
61
    );
62
    is(
63
        $record1->subfield('999', 'd'),
64
        '5',
65
        'First record has 5 in 999d'
66
    );
67
68
    is(
69
        $record2->subfield('999', 'c'),
70
        '6',
71
        'Second record has 6 in 999c'
72
    );
73
    is(
74
        $record2->subfield('999', 'd'),
75
        '6',
76
        'Second record has 6 in 999d'
77
    );
78
};
(-)a/t/Koha/SearchEngine/Elasticsearch.t (-49 / +45 lines)
Lines 117-123 subtest 'get_elasticsearch_mappings() tests' => sub { Link Here
117
117
118
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
118
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
119
119
120
    plan tests => 50;
120
    plan tests => 49;
121
121
122
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
122
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
123
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
123
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
Lines 322-348 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
322
322
323
    # First record:
323
    # First record:
324
    is(scalar @{$docs}, 2, 'Two records converted to documents');
324
    is(scalar @{$docs}, 2, 'Two records converted to documents');
325
    is($docs->[0][0], '1234567', 'First document biblionumber should be set as first element in document touple');
326
325
327
    is_deeply($docs->[0][1]->{control_number}, ['123'], 'First record control number should be set correctly');
326
    is_deeply($docs->[0]->{control_number}, ['123'], 'First record control number should be set correctly');
328
327
329
    is_deeply($docs->[0][1]->{'ff7-00'}, ['k'], 'First record ff7-00 should be set correctly');
328
    is_deeply($docs->[0]->{'ff7-00'}, ['k'], 'First record ff7-00 should be set correctly');
330
329
331
    is(scalar @{$docs->[0][1]->{author}}, 2, 'First document author field should contain two values');
330
    is(scalar @{$docs->[0]->{author}}, 2, 'First document author field should contain two values');
332
    is_deeply($docs->[0][1]->{author}, ['Author 1', 'Corp Author'], 'First document author field should be set correctly');
331
    is_deeply($docs->[0]->{author}, ['Author 1', 'Corp Author'], 'First document author field should be set correctly');
333
332
334
    is(scalar @{$docs->[0][1]->{author__sort}}, 1, 'First document author__sort field should have a single value');
333
    is(scalar @{$docs->[0]->{author__sort}}, 1, 'First document author__sort field should have a single value');
335
    is_deeply($docs->[0][1]->{author__sort}, ['Author 1 Corp Author'], 'First document author__sort field should be set correctly');
334
    is_deeply($docs->[0]->{author__sort}, ['Author 1 Corp Author'], 'First document author__sort field should be set correctly');
336
335
337
    is(scalar @{$docs->[0][1]->{title__sort}}, 1, 'First document title__sort field should have a single');
336
    is(scalar @{$docs->[0]->{title__sort}}, 1, 'First document title__sort field should have a single');
338
    is_deeply($docs->[0][1]->{title__sort}, ['Title: first record Title: first record'], 'First document title__sort field should be set correctly');
337
    is_deeply($docs->[0]->{title__sort}, ['Title: first record Title: first record'], 'First document title__sort field should be set correctly');
339
338
340
    is(scalar @{$docs->[0][1]->{title_wildcard}}, 2, 'First document title_wildcard field should have two values');
339
    is(scalar @{$docs->[0]->{title_wildcard}}, 2, 'First document title_wildcard field should have two values');
341
    is_deeply($docs->[0][1]->{title_wildcard}, ['Title:', 'first record'], 'First document title_wildcard field should be set correctly');
340
    is_deeply($docs->[0]->{title_wildcard}, ['Title:', 'first record'], 'First document title_wildcard field should be set correctly');
342
341
343
    is(scalar @{$docs->[0][1]->{author__suggestion}}, 2, 'First document author__suggestion field should contain two values');
342
    is(scalar @{$docs->[0]->{author__suggestion}}, 2, 'First document author__suggestion field should contain two values');
344
    is_deeply(
343
    is_deeply(
345
        $docs->[0][1]->{author__suggestion},
344
        $docs->[0]->{author__suggestion},
346
        [
345
        [
347
            {
346
            {
348
                'input' => 'Author 1'
347
                'input' => 'Author 1'
Lines 354-362 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
354
        'First document author__suggestion field should be set correctly'
353
        'First document author__suggestion field should be set correctly'
355
    );
354
    );
356
355
357
    is(scalar @{$docs->[0][1]->{title__suggestion}}, 3, 'First document title__suggestion field should contain three values');
356
    is(scalar @{$docs->[0]->{title__suggestion}}, 3, 'First document title__suggestion field should contain three values');
358
    is_deeply(
357
    is_deeply(
359
        $docs->[0][1]->{title__suggestion},
358
        $docs->[0]->{title__suggestion},
360
        [
359
        [
361
            { 'input' => 'Title:' },
360
            { 'input' => 'Title:' },
362
            { 'input' => 'first record' },
361
            { 'input' => 'first record' },
Lines 365-451 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
365
        'First document title__suggestion field should be set correctly'
364
        'First document title__suggestion field should be set correctly'
366
    );
365
    );
367
366
368
    ok(!(defined $docs->[0][1]->{title__facet}), 'First document should have no title__facet field');
367
    ok(!(defined $docs->[0]->{title__facet}), 'First document should have no title__facet field');
369
368
370
    is(scalar @{$docs->[0][1]->{author__facet}}, 2, 'First document author__facet field should have two values');
369
    is(scalar @{$docs->[0]->{author__facet}}, 2, 'First document author__facet field should have two values');
371
    is_deeply(
370
    is_deeply(
372
        $docs->[0][1]->{author__facet},
371
        $docs->[0]->{author__facet},
373
        ['Author 1', 'Corp Author'],
372
        ['Author 1', 'Corp Author'],
374
        'First document author__facet field should be set correctly'
373
        'First document author__facet field should be set correctly'
375
    );
374
    );
376
375
377
    is(scalar @{$docs->[0][1]->{items_withdrawn_status}}, 2, 'First document items_withdrawn_status field should have two values');
376
    is(scalar @{$docs->[0]->{items_withdrawn_status}}, 2, 'First document items_withdrawn_status field should have two values');
378
    is_deeply(
377
    is_deeply(
379
        $docs->[0][1]->{items_withdrawn_status},
378
        $docs->[0]->{items_withdrawn_status},
380
        ['false', 'true'],
379
        ['false', 'true'],
381
        'First document items_withdrawn_status field should be set correctly'
380
        'First document items_withdrawn_status field should be set correctly'
382
    );
381
    );
383
382
384
    is(
383
    is(
385
        $docs->[0][1]->{sum_item_price},
384
        $docs->[0]->{sum_item_price},
386
        '250.5',
385
        '250.5',
387
        'First document sum_item_price field should be set correctly'
386
        'First document sum_item_price field should be set correctly'
388
    );
387
    );
389
388
390
    ok(defined $docs->[0][1]->{marc_data}, 'First document marc_data field should be set');
389
    ok(defined $docs->[0]->{marc_data}, 'First document marc_data field should be set');
391
    ok(defined $docs->[0][1]->{marc_format}, 'First document marc_format field should be set');
390
    ok(defined $docs->[0]->{marc_format}, 'First document marc_format field should be set');
392
    is($docs->[0][1]->{marc_format}, 'base64ISO2709', 'First document marc_format should be set correctly');
391
    is($docs->[0]->{marc_format}, 'base64ISO2709', 'First document marc_format should be set correctly');
393
392
394
    my $decoded_marc_record = $see->decode_record_from_result($docs->[0][1]);
393
    my $decoded_marc_record = $see->decode_record_from_result($docs->[0]);
395
394
396
    ok($decoded_marc_record->isa('MARC::Record'), "base64ISO2709 record successfully decoded from result");
395
    ok($decoded_marc_record->isa('MARC::Record'), "base64ISO2709 record successfully decoded from result");
397
    is($decoded_marc_record->as_usmarc(), $marc_record_1->as_usmarc(), "Decoded base64ISO2709 record has same data as original record");
396
    is($decoded_marc_record->as_usmarc(), $marc_record_1->as_usmarc(), "Decoded base64ISO2709 record has same data as original record");
398
397
399
    is(scalar @{$docs->[0][1]->{type_of_record}}, 1, 'First document type_of_record field should have one value');
398
    is(scalar @{$docs->[0]->{type_of_record}}, 1, 'First document type_of_record field should have one value');
400
    is_deeply(
399
    is_deeply(
401
        $docs->[0][1]->{type_of_record},
400
        $docs->[0]->{type_of_record},
402
        ['a'],
401
        ['a'],
403
        'First document type_of_record field should be set correctly'
402
        'First document type_of_record field should be set correctly'
404
    );
403
    );
405
404
406
    is(scalar @{$docs->[0][1]->{type_of_record_and_bib_level}}, 1, 'First document type_of_record_and_bib_level field should have one value');
405
    is(scalar @{$docs->[0]->{type_of_record_and_bib_level}}, 1, 'First document type_of_record_and_bib_level field should have one value');
407
    is_deeply(
406
    is_deeply(
408
        $docs->[0][1]->{type_of_record_and_bib_level},
407
        $docs->[0]->{type_of_record_and_bib_level},
409
        ['am'],
408
        ['am'],
410
        'First document type_of_record_and_bib_level field should be set correctly'
409
        'First document type_of_record_and_bib_level field should be set correctly'
411
    );
410
    );
412
411
413
    is(scalar @{$docs->[0][1]->{isbn}}, 4, 'First document isbn field should contain four values');
412
    is(scalar @{$docs->[0]->{isbn}}, 4, 'First document isbn field should contain four values');
414
    is_deeply($docs->[0][1]->{isbn}, ['978-1-56619-909-4', '9781566199094', '1-56619-909-3', '1566199093'], 'First document isbn field should be set correctly');
413
    is_deeply($docs->[0]->{isbn}, ['978-1-56619-909-4', '9781566199094', '1-56619-909-3', '1566199093'], 'First document isbn field should be set correctly');
415
414
416
    is_deeply(
415
    is_deeply(
417
        $docs->[0][1]->{'local_classification'},
416
        $docs->[0]->{'local_classification'},
418
        [$callno, $callno2, $long_callno],
417
        [$callno, $callno2, $long_callno],
419
        'First document local_classification field should be set correctly'
418
        'First document local_classification field should be set correctly'
420
    );
419
    );
421
420
422
    # Second record:
421
    # Second record:
423
422
424
    is(scalar @{$docs->[1][1]->{author}}, 1, 'Second document author field should contain one value');
423
    is(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value');
425
    is_deeply($docs->[1][1]->{author}, ['Author 2'], 'Second document author field should be set correctly');
424
    is_deeply($docs->[1]->{author}, ['Author 2'], 'Second document author field should be set correctly');
426
425
427
    is(scalar @{$docs->[1][1]->{items_withdrawn_status}}, 1, 'Second document items_withdrawn_status field should have one value');
426
    is(scalar @{$docs->[1]->{items_withdrawn_status}}, 1, 'Second document items_withdrawn_status field should have one value');
428
    is_deeply(
427
    is_deeply(
429
        $docs->[1][1]->{items_withdrawn_status},
428
        $docs->[1]->{items_withdrawn_status},
430
        ['true'],
429
        ['true'],
431
        'Second document items_withdrawn_status field should be set correctly'
430
        'Second document items_withdrawn_status field should be set correctly'
432
    );
431
    );
433
432
434
    is(
433
    is(
435
        $docs->[1][1]->{sum_item_price},
434
        $docs->[1]->{sum_item_price},
436
        0,
435
        0,
437
        'Second document sum_item_price field should be set correctly'
436
        'Second document sum_item_price field should be set correctly'
438
    );
437
    );
439
438
440
    is_deeply(
439
    is_deeply(
441
        $docs->[1][1]->{local_classification__sort},
440
        $docs->[1]->{local_classification__sort},
442
        [substr($long_callno, 0, 255)],
441
        [substr($long_callno, 0, 255)],
443
        'Second document local_classification__sort field should be set correctly'
442
        'Second document local_classification__sort field should be set correctly'
444
    );
443
    );
445
444
446
    # Mappings marc_type:
445
    # Mappings marc_type:
447
446
448
    ok(!(defined $docs->[0][1]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
447
    ok(!(defined $docs->[0]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
449
448
450
    # Marc serialization format fallback for records exceeding ISO2709 max record size
449
    # Marc serialization format fallback for records exceeding ISO2709 max record size
451
450
Lines 468-476 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
468
467
469
    $docs = $see->marc_records_to_documents([$large_marc_record]);
468
    $docs = $see->marc_records_to_documents([$large_marc_record]);
470
469
471
    is($docs->[0][1]->{marc_format}, 'MARCXML', 'For record exceeding max record size marc_format should be set correctly');
470
    is($docs->[0]->{marc_format}, 'MARCXML', 'For record exceeding max record size marc_format should be set correctly');
472
471
473
    $decoded_marc_record = $see->decode_record_from_result($docs->[0][1]);
472
    $decoded_marc_record = $see->decode_record_from_result($docs->[0]);
474
473
475
    ok($decoded_marc_record->isa('MARC::Record'), "MARCXML record successfully decoded from result");
474
    ok($decoded_marc_record->isa('MARC::Record'), "MARCXML record successfully decoded from result");
476
    is($decoded_marc_record->as_xml_record(), $large_marc_record->as_xml_record(), "Decoded MARCXML record has same data as original record");
475
    is($decoded_marc_record->as_xml_record(), $large_marc_record->as_xml_record(), "Decoded MARCXML record has same data as original record");
Lines 521-527 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
521
520
522
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents_array () tests' => sub {
521
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents_array () tests' => sub {
523
522
524
    plan tests => 6;
523
    plan tests => 5;
525
524
526
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
525
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
527
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ARRAY');
526
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ARRAY');
Lines 588-600 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents_array () t Link Here
588
    # First record:
587
    # First record:
589
    is(scalar @{$docs}, 2, 'Two records converted to documents');
588
    is(scalar @{$docs}, 2, 'Two records converted to documents');
590
589
591
    is($docs->[0][0], '1234567', 'First document biblionumber should be set as first element in document touple');
590
    is_deeply($docs->[0]->{control_number}, ['123'], 'First record control number should be set correctly');
592
591
593
    is_deeply($docs->[0][1]->{control_number}, ['123'], 'First record control number should be set correctly');
592
    is($docs->[0]->{marc_format}, 'ARRAY', 'First document marc_format should be set correctly');
594
593
595
    is($docs->[0][1]->{marc_format}, 'ARRAY', 'First document marc_format should be set correctly');
594
    my $decoded_marc_record = $see->decode_record_from_result($docs->[0]);
596
597
    my $decoded_marc_record = $see->decode_record_from_result($docs->[0][1]);
598
595
599
    ok($decoded_marc_record->isa('MARC::Record'), "ARRAY record successfully decoded from result");
596
    ok($decoded_marc_record->isa('MARC::Record'), "ARRAY record successfully decoded from result");
600
    is($decoded_marc_record->as_usmarc(), $marc_record_1->as_usmarc(), "Decoded ARRAY record has same data as original record");
597
    is($decoded_marc_record->as_usmarc(), $marc_record_1->as_usmarc(), "Decoded ARRAY record has same data as original record");
601
- 

Return to bug 23630