Bug 13278: t/Search.t shouldn't depend on the DB
[koha.git] / t / Search.t
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 => 3;
21 use Test::MockModule;
22 use DBD::Mock;
23
24 # Mock the DB connexion and C4::Context
25 my $context = new Test::MockModule('C4::Context');
26 $context->mock( '_new_dbh', sub {
27         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
28           || die "Cannot create handle: $DBI::errstr\n";
29         return $dbh;
30 });
31
32 use_ok('C4::Search');
33 can_ok('C4::Search',
34     qw/_build_initial_query/);
35
36 subtest "_build_initial_query tests" => sub {
37
38     plan tests => 20;
39
40     my ($query,$query_cgi,$query_desc,$previous_operand);
41     # all params have values
42     my $params = {
43         query            => "query",
44         query_cgi        => "query_cgi",
45         query_desc       => "query_desc",
46         operator         => "operator",
47         parsed_operand   => "parsed_operand",
48         original_operand => "original_operand",
49         index            => "index",
50         index_plus       => "index_plus",
51         indexes_set      => "indexes_set",
52         previous_operand => "previous_operand"
53     };
54
55     ($query,$query_cgi,$query_desc,$previous_operand) =
56         C4::Search::_build_initial_query($params);
57     is( $query, "query operator parsed_operand",
58         "\$query built correctly");
59     is( $query_cgi, "query_cgi&op=%20operator%20&idx=index&q=original_operand",
60         "\$query_cgi built correctly");
61     is( $query_desc, "query_desc operator index_plus original_operand",
62         "\$query_desc build correctly");
63     is( $previous_operand, "previous_operand",
64         "\$query build correctly");
65
66     # no operator
67     $params = {
68         query            => "query",
69         query_cgi        => "query_cgi",
70         query_desc       => "query_desc",
71         operator         => undef,
72         parsed_operand   => "parsed_operand",
73         original_operand => "original_operand",
74         index            => "index",
75         index_plus       => "index_plus",
76         indexes_set      => "indexes_set",
77         previous_operand => "previous_operand"
78     };
79
80     ($query,$query_cgi,$query_desc,$previous_operand) =
81         C4::Search::_build_initial_query($params);
82     is( $query, "query and parsed_operand",
83         "\$query built correctly (no operator)");
84     is( $query_cgi, "query_cgi&op=%20and%20&idx=index&q=original_operand",
85         "\$query_cgi built correctly (no operator)");
86     is( $query_desc, "query_desc and index_plus original_operand",
87         "\$query_desc build correctly (no operator)");
88     is( $previous_operand, "previous_operand",
89         "\$query build correctly (no operator)");
90
91     # no previous operand
92     $params = {
93         query            => "query",
94         query_cgi        => "query_cgi",
95         query_desc       => "query_desc",
96         operator         => "operator",
97         parsed_operand   => "parsed_operand",
98         original_operand => "original_operand",
99         index            => "index",
100         index_plus       => "index_plus",
101         indexes_set      => "indexes_set",
102         previous_operand => undef
103     };
104
105     ($query,$query_cgi,$query_desc,$previous_operand) =
106         C4::Search::_build_initial_query($params);
107     is( $query, "queryparsed_operand",
108         "\$query built correctly (no previous operand)");
109     is( $query_cgi, "query_cgi&idx=index&q=original_operand",
110         "\$query_cgi built correctly (no previous operand)");
111     is( $query_desc, "query_descindex_plus original_operand",
112         "\$query_desc build correctly (no previous operand)");
113     is( $previous_operand, 1,
114         "\$query build correctly (no previous operand)");
115
116     # no index passed
117     $params = {
118         query            => "query",
119         query_cgi        => "query_cgi",
120         query_desc       => "query_desc",
121         operator         => "operator",
122         parsed_operand   => "parsed_operand",
123         original_operand => "original_operand",
124         index            => undef,
125         index_plus       => "index_plus",
126         indexes_set      => "indexes_set",
127         previous_operand => "previous_operand"
128     };
129
130     ($query,$query_cgi,$query_desc,$previous_operand) =
131         C4::Search::_build_initial_query($params);
132     is( $query, "query operator parsed_operand",
133         "\$query built correctly (no index passed)");
134     is( $query_cgi, "query_cgi&op=%20operator%20&q=original_operand",
135         "\$query_cgi built correctly (no index passed)");
136     is( $query_desc, "query_desc operator index_plus original_operand",
137         "\$query_desc build correctly (no index passed)");
138     is( $previous_operand, "previous_operand",
139         "\$query build correctly (no index passed)");
140
141     # no index_plus passed
142     $params = {
143         query            => "query",
144         query_cgi        => "query_cgi",
145         query_desc       => "query_desc",
146         operator         => "operator",
147         parsed_operand   => "parsed_operand",
148         original_operand => "original_operand",
149         index            => "index",
150         index_plus       => undef,
151         indexes_set      => "indexes_set",
152         previous_operand => "previous_operand"
153     };
154
155     ($query,$query_cgi,$query_desc,$previous_operand) =
156         C4::Search::_build_initial_query($params);
157     is( $query, "query operator parsed_operand",
158         "\$query built correctly (no index_plus passed)");
159     is( $query_cgi, "query_cgi&op=%20operator%20&idx=index&q=original_operand",
160         "\$query_cgi built correctly (no index_plus passed)");
161     is( $query_desc, "query_desc operator  original_operand",
162         "\$query_desc build correctly (no index_plus passed)");
163     is( $previous_operand, "previous_operand",
164         "\$query build correctly (no index_plus passed)");
165
166 };
167
168
169 1;