Bug 22107: Make tests use a simpler class, with no FK
[koha.git] / t / db_dependent / Koha / REST / Plugin / Objects.t
blob8c75d024a48d4b8a6422cf50b565e1395cec7d47
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;
19 use Koha::Cities;
21 # Dummy app for testing the plugin
22 use Mojolicious::Lite;
24 app->log->level('error');
26 plugin 'Koha::REST::Plugin::Objects';
27 plugin 'Koha::REST::Plugin::Query';
28 plugin 'Koha::REST::Plugin::Pagination';
30 get '/cities' => sub {
31 my $c = shift;
32 $c->validation->output($c->req->params->to_hash);
33 my $cities = $c->objects->search(Koha::Cities->new);
34 $c->render( status => 200, json => $cities );
37 get '/cities_to_model' => sub {
38 my $c = shift;
39 $c->validation->output($c->req->params->to_hash);
40 my $cities_set = Koha::Cities->new;
41 my $cities = $c->objects->search( $cities_set, \&to_model );
42 $c->render( status => 200, json => $cities );
45 get '/cities_to_model_to_api' => sub {
46 my $c = shift;
47 $c->validation->output($c->req->params->to_hash);
48 my $cities_set = Koha::Cities->new;
49 my $cities = $c->objects->search( $cities_set, \&to_model, \&to_api );
50 $c->render( status => 200, json => $cities );
53 sub to_model {
54 my $params = shift;
56 if ( exists $params->{nombre} ) {
57 $params->{city_name} = delete $params->{nombre};
60 return $params;
63 sub to_api {
64 my $params = shift;
66 if ( exists $params->{city_name} ) {
67 $params->{nombre} = delete $params->{city_name};
70 return $params;
73 # The tests
74 use Test::More tests => 1;
75 use Test::Mojo;
77 use t::lib::TestBuilder;
78 use Koha::Database;
80 my $schema = Koha::Database->new()->schema();
83 my $builder = t::lib::TestBuilder->new;
85 subtest 'objects.search helper' => sub {
87 plan tests => 90;
89 my $t = Test::Mojo->new;
91 $schema->storage->txn_begin;
93 # Remove existing cities to have more control on the search restuls
94 Koha::Cities->delete;
96 # Create two sample patrons that match the query
97 $builder->build_object({
98 class => 'Koha::Cities',
99 value => {
100 city_name => 'Manuel'
103 $builder->build_object({
104 class => 'Koha::Cities',
105 value => {
106 city_name => 'Manuela'
110 $t->get_ok('/cities?city_name=manuel&_per_page=1&_page=1')
111 ->status_is(200)
112 ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
113 ->json_has('/0')
114 ->json_hasnt('/1')
115 ->json_is('/0/city_name' => 'Manuel');
117 $builder->build_object({
118 class => 'Koha::Cities',
119 value => {
120 city_name => 'Emanuel'
124 # _match=starts_with
125 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=starts_with')
126 ->status_is(200)
127 ->json_has('/0')
128 ->json_has('/1')
129 ->json_hasnt('/2')
130 ->json_is('/0/city_name' => 'Manuel')
131 ->json_is('/1/city_name' => 'Manuela');
133 # _match=ends_with
134 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=ends_with')
135 ->status_is(200)
136 ->json_has('/0')
137 ->json_has('/1')
138 ->json_hasnt('/2')
139 ->json_is('/0/city_name' => 'Manuel')
140 ->json_is('/1/city_name' => 'Emanuel');
142 # _match=exact
143 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=exact')
144 ->status_is(200)
145 ->json_has('/0')
146 ->json_hasnt('/1')
147 ->json_is('/0/city_name' => 'Manuel');
149 # _match=contains
150 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=contains')
151 ->status_is(200)
152 ->json_has('/0')
153 ->json_has('/1')
154 ->json_has('/2')
155 ->json_hasnt('/3')
156 ->json_is('/0/city_name' => 'Manuel')
157 ->json_is('/1/city_name' => 'Manuela')
158 ->json_is('/2/city_name' => 'Emanuel');
160 ## _to_model tests
161 # _match=starts_with
162 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
163 ->status_is(200)
164 ->json_has('/0')
165 ->json_has('/1')
166 ->json_hasnt('/2')
167 ->json_is('/0/city_name' => 'Manuel')
168 ->json_is('/1/city_name' => 'Manuela');
170 # _match=ends_with
171 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
172 ->status_is(200)
173 ->json_has('/0')
174 ->json_has('/1')
175 ->json_hasnt('/2')
176 ->json_is('/0/city_name' => 'Manuel')
177 ->json_is('/1/city_name' => 'Emanuel');
179 # _match=exact
180 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact')
181 ->status_is(200)
182 ->json_has('/0')
183 ->json_hasnt('/1')
184 ->json_is('/0/city_name' => 'Manuel');
186 # _match=contains
187 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains')
188 ->status_is(200)
189 ->json_has('/0')
190 ->json_has('/1')
191 ->json_has('/2')
192 ->json_hasnt('/3')
193 ->json_is('/0/city_name' => 'Manuel')
194 ->json_is('/1/city_name' => 'Manuela')
195 ->json_is('/2/city_name' => 'Emanuel');
197 ## _to_model && _to_api tests
198 # _match=starts_with
199 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
200 ->status_is(200)
201 ->json_has('/0')
202 ->json_has('/1')
203 ->json_hasnt('/2')
204 ->json_is('/0/nombre' => 'Manuel')
205 ->json_is('/1/nombre' => 'Manuela');
207 # _match=ends_with
208 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
209 ->status_is(200)
210 ->json_has('/0')
211 ->json_has('/1')
212 ->json_hasnt('/2')
213 ->json_is('/0/nombre' => 'Manuel')
214 ->json_is('/1/nombre' => 'Emanuel');
216 # _match=exact
217 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact')
218 ->status_is(200)
219 ->json_has('/0')
220 ->json_hasnt('/1')
221 ->json_is('/0/nombre' => 'Manuel');
223 # _match=contains
224 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains')
225 ->status_is(200)
226 ->json_has('/0')
227 ->json_has('/1')
228 ->json_has('/2')
229 ->json_hasnt('/3')
230 ->json_is('/0/nombre' => 'Manuel')
231 ->json_is('/1/nombre' => 'Manuela')
232 ->json_is('/2/nombre' => 'Emanuel');
234 $schema->storage->txn_rollback;