From 1a7abd7a65b0d8e10a30a6b30374ed6efdb82453 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 7 Mar 2023 19:23:22 +0000 Subject: [PATCH] Bug 33159: Unit tests --- t/db_dependent/Heading.t | 61 +++++++++++++++++++++++++++++++-- t/db_dependent/Heading_MARC21.t | 12 +++++-- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/t/db_dependent/Heading.t b/t/db_dependent/Heading.t index bef0162d2e..9a21534828 100755 --- a/t/db_dependent/Heading.t +++ b/t/db_dependent/Heading.t @@ -18,9 +18,10 @@ use strict; use warnings; -use Test::More tests => 3; +use Test::More tests => 4; use t::lib::Mocks; +use Test::MockModule;; BEGIN { use_ok('C4::Heading', qw( field valid_heading_subfield )); @@ -60,4 +61,60 @@ subtest "UNIMARC tests" => sub { ok(!C4::Heading::valid_heading_subfield('600', 'i'), '600i not valid for bib'); ok(!C4::Heading::valid_heading_subfield('012', 'a'), '012a invalid field for bib'); -} +}; + +subtest "_search tests" => sub { + plan tests => 4; + + t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); + t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch'); + my $search = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search'); + + $search->mock('search_auth_compat', sub { + my $self = shift; + my $search_query = shift; + return $search_query; + }); + + my $field = MARC::Field->new( '650', ' ', '0', a => 'Uncles', x => 'Fiction' ); + my $heading = C4::Heading->new_from_field($field); + my $search_query = $heading->_search( 'match-heading' ); + my $terms = $search_query->{query}->{bool}->{must}; + my $expected_terms = [ + { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } }, + { term => { 'subject-heading-thesaurus.ci_raw' => 'a' } }, + ]; + is_deeply( $terms, $expected_terms, "Search formed as expected for a subject with second indicator 0"); + + $field = MARC::Field->new( '650', ' ', '3', a => 'Uncles', x => 'Fiction' ); + $heading = C4::Heading->new_from_field($field); + $search_query = $heading->_search( 'match-heading' ); + $terms = $search_query->{query}->{bool}->{must}; + $expected_terms = [ + { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } }, + { term => { 'subject-heading-thesaurus.ci_raw' => 'd' } }, + ]; + is_deeply( $terms, $expected_terms, "Search formed as expected with second indicator 3"); + + $field = MARC::Field->new( '650', ' ', '7', a => 'Uncles', x => 'Fiction', 2 => 'special_sauce' ); + $heading = C4::Heading->new_from_field($field); + $search_query = $heading->_search( 'match-heading' ); + $terms = $search_query->{query}->{bool}->{must}; + $expected_terms = [ + { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } }, + { term => { 'subject-heading-thesaurus-conventions.ci_raw' => 'special_sauce' } }, + { term => { 'subject-heading-thesaurus.ci_raw' => 'z' } }, + ]; + is_deeply( $terms, $expected_terms, "Search formed as expected with second indicator 7 and subfield 2"); + + $field = MARC::Field->new( '100', ' ', '', a => 'Yankovic, Al', d => '1959-,', e => '[author]' ); + $heading = C4::Heading->new_from_field($field); + $search_query = $heading->_search( 'match-heading' ); + $terms = $search_query->{query}->{bool}->{must}; + $expected_terms = [ + { term => { 'match-heading.ci_raw' => 'Yankovic, Al 1959-' } }, + { term => { 'subject-heading-thesaurus.ci_raw' => 'a' } }, + ]; + is_deeply( $terms, $expected_terms, "Search formed as expected for a non-subject field"); + +}; diff --git a/t/db_dependent/Heading_MARC21.t b/t/db_dependent/Heading_MARC21.t index 357d3126d4..32bd0a07cc 100755 --- a/t/db_dependent/Heading_MARC21.t +++ b/t/db_dependent/Heading_MARC21.t @@ -6,7 +6,7 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 10; use C4::Context; BEGIN { @@ -15,14 +15,22 @@ BEGIN { SKIP: { skip "MARC21 heading tests not applicable to UNIMARC", 2 if C4::Context->preference('marcflavour') eq 'UNIMARC'; - my $field = MARC::Field->new( '650', ' ', '0', a => 'Uncles', x => 'Fiction' ); + my $field = MARC::Field->new( '650', ' ', '2', a => 'Uncles', x => 'Fiction' ); my $heading = C4::Heading->new_from_field($field); is($heading->display_form(), 'Uncles--Fiction', 'Display form generation'); is($heading->search_form(), 'Uncles generalsubdiv Fiction', 'Search form generation'); + is($heading->{thesaurus}, 'mesh', 'Thesaurus generation'); $field = MARC::Field->new( '830', ' ', '4', a => 'The dark is rising ;', v => '3' ); $heading = C4::Heading->new_from_field($field); is($heading->display_form(), 'The dark is rising ;', 'Display form generation'); is($heading->search_form(), 'The dark is rising', 'Search form generation'); + is($heading->{thesaurus}, 'lcsh', 'Thesaurus generation'); + + $field = MARC::Field->new( '100', '1', '', a => 'Yankovic, Al', d => '1959-' ); + $heading = C4::Heading->new_from_field($field); + is($heading->display_form(), 'Yankovic, Al 1959-', 'Display form generation'); + is($heading->search_form(), 'Yankovic, Al 1959', 'Search form generation'); + is($heading->{thesaurus}, 'lcsh', 'Thesaurus generation'); } -- 2.30.2