Bug 5327 shifting database dependent modules and scripts to t/db_dependent
[koha-ffzg.git] / t / db_dependent / lib / KohaTest / Search / SimpleSearch.pm
1 package KohaTest::Search::SimpleSearch;
2 use base qw( KohaTest::Search );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::Search;
10 use C4::Biblio;
11
12 =head2 STARTUP METHODS
13
14 These get run once, before the main test methods in this module
15
16 =head3 insert_test_data
17
18 =cut
19
20 sub insert_test_data : Test( startup => 71 ) {
21     my $self = shift;
22     
23     # get original 'Finn Test' count
24     my $query = 'Finn Test';
25     my ( $error, $results ) = SimpleSearch( $query );
26     $self->{'orig_finn_test_hits'} = scalar(@$results);
27
28     # I'm going to add a bunch of biblios so that I can search for them.
29     $self->add_biblios( count     => 10,
30                         add_items => 1 );
31
32 }
33
34 =head2 STARTUP METHODS
35
36 standard test methods
37
38 =head3 basic_test
39
40 basic usage.
41
42 =cut
43
44 sub basic_test : Test( 2 ) {
45     my $self = shift;
46
47     my $query = 'test';
48
49     my ( $error, $results ) = SimpleSearch( $query );
50     ok( ! defined $error, 'no error found during search' );
51     like( $results->[0], qr/$query/i, 'the result seems to match the query' )
52       or diag( Data::Dumper->Dump( [ $results ], [ 'results' ] ) );
53     
54 }
55
56 =head3 basic_test_with_server
57
58 Test the usage where we specify no limits, but we do specify a server.
59
60 =cut
61
62 sub basic_test_with_server : Test( 2 ) {
63     my $self = shift;
64
65     my $query = 'test';
66
67     my ( $error, $results ) = SimpleSearch( $query, undef, undef, [ 'biblioserver' ] );
68     ok( ! defined $error, 'no error found during search' );
69     like( $results->[0], qr/$query/i, 'the result seems to match the query' )
70       or diag( Data::Dumper->Dump( [ $results ], [ 'results' ] ) );
71     
72 }
73
74
75 =head3 basic_test_no_results
76
77 Make sure we get back an empty listref when there are no results.
78
79 =cut
80
81 sub basic_test_no_results : Test( 3 ) {
82     my $self = shift;
83
84     my $query = 'This string is almost guaranteed to not match anything.';
85
86     my ( $error, $results ) = SimpleSearch( $query );
87     ok( ! defined $error, 'no error found during search' );
88     isa_ok( $results, 'ARRAY' );
89     is( scalar( @$results ), 0, 'an empty list was returned.' )
90       or diag( Data::Dumper->Dump( [ $results ], [ 'results' ] ) );
91 }
92
93 =head3 limits
94
95 check that the SimpleTest method limits the number of results returned.
96
97 =cut
98
99 sub limits : Test( 8 ) {
100     my $self = shift;
101
102     my $query = 'Finn Test';
103
104     {
105         my ( $error, $results ) = SimpleSearch( $query );
106         ok( ! defined $error, 'no error found during search' );
107         my $expected_hits = 10 + $self->{'orig_finn_test_hits'};
108         is( scalar @$results, $expected_hits, "found all $expected_hits results." )
109           or diag( Data::Dumper->Dump( [ $results ], [ 'results' ] ) );
110     }
111     
112     my $offset = 4;
113     {
114         my ( $error, $results ) = SimpleSearch( $query, $offset );
115         ok( ! defined $error, 'no error found during search' );
116         my $expected_hits = 6 + $self->{'orig_finn_test_hits'};
117         is( scalar @$results, $expected_hits, "found $expected_hits results." )
118           or diag( Data::Dumper->Dump( [ $results ], [ 'results' ] ) );
119     }
120
121     my $max_results = 2;
122     {
123         my ( $error, $results ) = SimpleSearch( $query, $offset, $max_results );
124         ok( ! defined $error, 'no error found during search' );
125         is( scalar @$results, $max_results, "found $max_results results." )
126           or diag( Data::Dumper->Dump( [ $results ], [ 'results' ] ) );
127     }
128     
129     {
130         my ( $error, $results ) = SimpleSearch( $query, 0, $max_results );
131         ok( ! defined $error, 'no error found during search' );
132         is( scalar @$results, $max_results, "found $max_results results." )
133           or diag( Data::Dumper->Dump( [ $results ], [ 'results' ] ) );
134     }
135     
136        
137 }
138
139
140 1;