Bug 19410: Move build_query_params_from_api into a helper
[koha.git] / t / Koha / REST / Plugin / Query.t
blobe7b980b695f8431954d048a340d105994bec0c7b
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 # Dummy app for testing the plugin
21 use Mojolicious::Lite;
22 use Try::Tiny;
24 app->log->level('error');
26 plugin 'Koha::REST::Plugin::Query';
28 get '/empty' => sub {
29 my $c = shift;
30 $c->render( json => undef, status => 200 );
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'
41 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
42 $c->render(
43 json => {
44 filtered_params => $filtered_params,
45 reserved_params => $reserved_params
47 status => 200
51 get '/query_full' => sub {
52 my $c = shift;
53 my $input = {
54 _match => 'exact',
55 _order_by => 'blah',
56 _page => 2,
57 _per_page => 3,
58 firstname => 'Manuel',
59 surname => 'Cohen Arazi'
61 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
62 $c->render(
63 json => {
64 filtered_params => $filtered_params,
65 reserved_params => $reserved_params
67 status => 200
71 get '/dbic_merge_sorting' => sub {
72 my $c = shift;
73 my $attributes = { a => 'a', b => 'b' };
74 $attributes = $c->dbic_merge_sorting(
76 attributes => $attributes,
77 params => { _match => 'exact', _order_by => [ 'uno', '-dos', '+tres', ' cuatro' ] }
80 $c->render( json => $attributes, status => 200 );
83 get '/build_query' => sub {
84 my $c = shift;
85 my ( $filtered_params, $reserved_params ) =
86 $c->extract_reserved_params( $c->req->params->to_hash );
87 my $query;
88 try {
89 $query = $c->build_query_params( $filtered_params, $reserved_params );
90 $c->render( json => { query => $query }, status => 200 );
92 catch {
93 $c->render(
94 json => { exception_msg => $_->message, exception_type => ref($_) },
95 status => 400
100 # The tests
102 use Test::More tests => 3;
103 use Test::Mojo;
105 subtest 'extract_reserved_params() tests' => sub {
107 plan tests => 8;
109 my $t = Test::Mojo->new;
111 $t->get_ok('/query')->status_is(200)
112 ->json_is( '/filtered_params' =>
113 { firstname => 'Manuel', surname => 'Cohen Arazi' } )
114 ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
116 $t->get_ok('/query_full')->status_is(200)
117 ->json_is(
118 '/filtered_params' => {
119 firstname => 'Manuel',
120 surname => 'Cohen Arazi'
122 ->json_is(
123 '/reserved_params' => {
124 _page => 2,
125 _per_page => 3,
126 _match => 'exact',
127 _order_by => 'blah'
128 } );
132 subtest 'dbic_merge_sorting() tests' => sub {
134 plan tests => 5;
136 my $t = Test::Mojo->new;
138 $t->get_ok('/dbic_merge_sorting')->status_is(200)
139 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
140 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
141 '/order_by' => [
142 'uno',
143 { -desc => 'dos' },
144 { -asc => 'tres' },
145 { -asc => 'cuatro' }
150 subtest '_build_query_params_from_api' => sub {
152 plan tests => 16;
154 my $t = Test::Mojo->new;
156 # _match => contains
157 $t->get_ok('/build_query?_match=contains&title=Ender&author=Orson')
158 ->status_is(200)
159 ->json_is( '/query' =>
160 { author => { like => '%Orson%' }, title => { like => '%Ender%' } } );
162 # _match => starts_with
163 $t->get_ok('/build_query?_match=starts_with&title=Ender&author=Orson')
164 ->status_is(200)
165 ->json_is( '/query' =>
166 { author => { like => 'Orson%' }, title => { like => 'Ender%' } } );
168 # _match => ends_with
169 $t->get_ok('/build_query?_match=ends_with&title=Ender&author=Orson')
170 ->status_is(200)
171 ->json_is( '/query' =>
172 { author => { like => '%Orson' }, title => { like => '%Ender' } } );
174 # _match => exact
175 $t->get_ok('/build_query?_match=exact&title=Ender&author=Orson')
176 ->status_is(200)
177 ->json_is( '/query' => { author => 'Orson', title => 'Ender' } );
179 # _match => blah
180 $t->get_ok('/build_query?_match=blah&title=Ender&author=Orson')
181 ->status_is(400)
182 ->json_is( '/exception_msg' => 'Invalid value for _match param (blah)' )
183 ->json_is( '/exception_type' => 'Koha::Exceptions::WrongParameter' );