Bug 18383 - Correct items.onloan schema comment
[koha.git] / t / QueryParser.t
blob7b694300bdb6086ddbd7df2b731b4f82905a8557
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use Test::More;
7 use Test::Deep;
8 use Module::Load::Conditional qw(can_load);
10 BEGIN {
11 use_ok( 'Koha::QueryParser::Driver::PQF' );
14 my $QParser = Koha::QueryParser::Driver::PQF->new();
16 ok(defined $QParser, 'Successfully created empty QP object');
17 ok($QParser->load_config('./etc/searchengine/queryparser.yaml'), 'Loaded QP config');
19 is($QParser->search_class_count, 4, 'Initialized 4 search classes');
20 is (scalar(@{$QParser->search_fields()->{'keyword'}}), 111, "Correct number of search fields for 'keyword' class");
22 # Set keyword search as the default
23 $QParser->default_search_class('keyword');
25 my $kwd_search = q/@attr 1=1016 @attr 4=6/;
26 my $weight1 = q/@attr 2=102 @attr 9=20 @attr 4=6/;
27 my $weight2 = q/@attr 2=102 @attr 9=34 @attr 4=6/;
29 like( $QParser->target_syntax('biblioserver', 'smith'),
30 qr/\@or \@or $kwd_search "smith" ($weight1 "smith" $weight2 "smith"|$weight2 "smith" $weight1 "smith")/,
31 'super simple keyword query');
33 is($QParser->target_syntax('biblioserver', 'au:smith'),
34 '@attr 1=1003 @attr 4=6 "smith"', 'simple author query');
36 is($QParser->target_syntax('biblioserver', 'keyword|publisher:smith'),
37 '@attr 1=1018 @attr 4=6 "smith"', 'fielded publisher query');
39 is($QParser->target_syntax('biblioserver', 'ti:"little engine that could"'),
40 '@attr 1=4 @attr 4=1 "little engine that could"', 'phrase query');
42 is($QParser->target_syntax('biblioserver', 'keyword|titlekw:smith'),
43 '@attr 1=4 @attr 2=102 @attr 9=20 @attr 4=6 "smith"',
44 'relevance-bumped query');
46 is($QParser->target_syntax('biblioserver', 'au:smith && johnson'),
47 '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=1003 @attr 4=6 "johnson"',
48 'query with boolean &&');
50 is($QParser->target_syntax('biblioserver', 'au:smith && ti:johnson'),
51 '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=4 @attr 4=6 "johnson"', 'query with boolean &&');
53 is($QParser->target_syntax('biblioserver', 'au:smith pubdate(-2008)'),
54 '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=31 @attr 4=4 @attr 2=2 "2008"',
55 'keyword search with pubdate limited to -2008');
57 is($QParser->target_syntax('biblioserver', 'au:smith pubdate(2008-)'),
58 '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=31 @attr 4=4 @attr 2=4 "2008"',
59 'keyword search with pubdate limited to 2008-');
61 is($QParser->target_syntax('biblioserver', 'au:smith pubdate(2008)'),
62 '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=31 @attr 4=4 "2008"',
63 'keyword search with pubdate limited to 2008');
65 is($QParser->target_syntax('biblioserver', 'au:smith pubdate(1980,2008)'),
66 '@and @attr 1=1003 @attr 4=6 "smith" @or @attr 1=31 @attr 4=4 "1980" @attr 1=31 @attr 4=4 "2008"',
67 'keyword search with pubdate limited to 1980, 2008');
69 is($QParser->target_syntax('biblioserver', 'au:smith #acqdate_dsc'),
70 '@or @attr 1=32 @attr 7=1 0 @attr 1=1003 @attr 4=6 "smith"',
71 'keyword search sorted by acqdate descending');
73 is($QParser->bib1_mapping_by_attr('field', 'biblioserver', {'1' => '1004'})->{'field'},
74 'personal', 'retrieve field by attr');
76 is($QParser->bib1_mapping_by_attr_string('field', 'biblioserver', '@attr 1=1004')->{'field'},
77 'personal', 'retrieve field by attrstring');
79 is ($QParser->clear_all_mappings, $QParser, 'clear all mappings returns self');
80 is ($QParser->clear_all_configuration, $QParser, 'clear all configuration returns self');
81 is (scalar(keys(%{$QParser->search_fields})), 0, "All mapping erased");
83 $QParser->add_bib1_field_map('author' => 'personal' => 'biblioserver' => { '1' => '1004' } );
84 $QParser->add_bib1_modifier_map('relevance' => 'biblioserver' => { '2' => '102' } );
85 my $desired_config = {
86 'field_mappings' => {
87 'author' => {
88 'personal' => {
89 'aliases' => [ ],
90 'bib1_mapping' => {
91 'biblioserver' => {
92 '1' => '1004'
95 'enabled' => '1',
96 'index' => 'personal',
97 'label' => 'Personal'
101 'filter_mappings' => {},
102 'modifier_mappings' => {
103 'relevance' => {
104 'bib1_mapping' => {
105 'biblioserver' => {
106 '2' => '102'
109 'enabled' => 1,
110 'label' => 'Relevance'
113 'relevance_bumps' => {}
116 SKIP: {
117 my $got_config;
118 skip 'YAML is unavailable', 2 unless can_load('modules' => { 'YAML::Any' => undef });
119 $got_config = YAML::Any::Load($QParser->serialize_mappings());
120 ok(ref $got_config, 'serialized YAML valid');
121 is_deeply($got_config, $desired_config, 'Mappings serialized correctly to YAML');
123 skip 'JSON is unavailable', 2 unless can_load('modules' => { 'JSON' => undef });
124 undef $got_config;
125 eval {
126 $got_config = JSON::from_json($QParser->serialize_mappings('json'));
128 is($@, '', 'serialized JSON valid');
129 is_deeply($got_config, $desired_config, 'Mappings serialized correctly to JSON');
132 $QParser->clear_all_mappings;
133 is($QParser->TEST_SETUP, $QParser, 'TEST_SETUP returns self');
134 is($QParser->search_class_count, 4, 'Initialized 4 search classes in test setup');
136 done_testing();