Line 0
Link Here
|
0 |
- |
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 |
# Dummy app for testing the plugin |
21 |
use Mojolicious::Lite; |
22 |
|
23 |
app->log->level('error'); |
24 |
|
25 |
plugin 'Koha::REST::Plugin::Query'; |
26 |
|
27 |
|
28 |
get '/empty' => sub { |
29 |
my $c = shift; |
30 |
$c->render( json => undef, status => 200 ); |
31 |
}; |
32 |
|
33 |
get '/query' => sub { |
34 |
my $c = shift; |
35 |
my $input = { |
36 |
page => 2, |
37 |
per_page => 3, |
38 |
firstname => 'Manuel', |
39 |
surname => 'Cohen Arazi' |
40 |
}; |
41 |
my ( $filter, $attributes ) = $c->generate_dbic_query($input); |
42 |
$c->render( json => { filter => $filter, attributes => $attributes }, status => 200 ); |
43 |
}; |
44 |
|
45 |
get '/query_exact' => sub { |
46 |
my $c = shift; |
47 |
my $input = { |
48 |
page => 2, |
49 |
per_page => 3, |
50 |
firstname => 'Manuel', |
51 |
surname => 'Cohen Arazi', |
52 |
_exact_match => 1 |
53 |
}; |
54 |
my ( $filter, $attributes ) = $c->generate_dbic_query($input); |
55 |
$c->render( json => { filter => $filter, attributes => $attributes }, status => 200 ); |
56 |
}; |
57 |
|
58 |
# The tests |
59 |
|
60 |
use Test::More tests => 1; |
61 |
use Test::Mojo; |
62 |
|
63 |
subtest 'generate_dbic_query() tests' => sub { |
64 |
|
65 |
plan tests => 8; |
66 |
|
67 |
my $t = Test::Mojo->new; |
68 |
|
69 |
$t->get_ok('/query') |
70 |
->status_is( 200 ) |
71 |
->json_is( '/attributes' => { page => 2, rows => 3 } ) |
72 |
->json_is( '/filter' => { firstname => { like => 'Manuel%' }, surname => { like => 'Cohen Arazi%' } } ); |
73 |
|
74 |
$t->get_ok('/query_exact') |
75 |
->status_is( 200 ) |
76 |
->json_is( '/attributes' => { page => 2, rows => 3 } ) |
77 |
->json_is( '/filter' => { firstname => 'Manuel' , surname => 'Cohen Arazi' } ); |
78 |
|
79 |
# use Data::Printer colored => 1; |
80 |
# p($t->tx->res); |
81 |
}; |