From e519dcf68a33843c15f7d0e3795a28899007cfe5 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 29 Aug 2017 17:00:50 -0300 Subject: [PATCH] Bug 19196: Unit tests This patch adds unit tests for the new pagination Mojo plugin. Sponsored-by: ByWater Solutions Sponsored-by: Camden County Signed-off-by: Kyle M Hall Signed-off-by: Lari Taskula --- t/Koha/REST/Plugin/Pagination.t | 110 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 t/Koha/REST/Plugin/Pagination.t diff --git a/t/Koha/REST/Plugin/Pagination.t b/t/Koha/REST/Plugin/Pagination.t new file mode 100644 index 0000000..04d51e1 --- /dev/null +++ b/t/Koha/REST/Plugin/Pagination.t @@ -0,0 +1,110 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +# Dummy app for testing the plugin +use Mojolicious::Lite; + +app->log->level('error'); + +plugin 'Koha::REST::Plugin::Pagination'; + + +get '/empty' => sub { + my $c = shift; + $c->render( json => { ok => 1 }, status => 200 ); +}; + +get '/pagination_headers' => sub { + my $c = shift; + $c->add_pagination_headers({ total => 10, params => { page => 2, per_page => 3, firstname => 'Jonathan' } }); + $c->render( json => { ok => 1 }, status => 200 ); +}; + +get '/pagination_headers_first_page' => sub { + my $c = shift; + $c->add_pagination_headers({ total => 10, params => { page => 1, per_page => 3, firstname => 'Jonathan' } }); + $c->render( json => { ok => 1 }, status => 200 ); +}; + +get '/pagination_headers_last_page' => sub { + my $c = shift; + $c->add_pagination_headers({ total => 10, params => { page => 4, per_page => 3, firstname => 'Jonathan' } }); + $c->render( json => { ok => 1 }, status => 200 ); +}; + +# The tests + +use Test::More tests => 1; +use Test::Mojo; + +subtest 'add_pagination_headers() tests' => sub { + + plan tests => 45; + + my $t = Test::Mojo->new; + + $t->get_ok('/empty') + ->status_is( 200 ) + ->header_is( 'X-Total-Count' => undef, 'X-Total-Count is undefined' ) + ->header_is( 'Link' => undef, 'Link is undefined' ); + + $t->get_ok('/pagination_headers') + ->status_is( 200 ) + ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) + ->header_like( 'Link' => qr/; rel="prev",/ ) + ->header_like( 'Link' => qr/; rel="prev",/ ) + ->header_like( 'Link' => qr/; rel="prev",/ ) + ->header_like( 'Link' => qr/; rel="next",/ ) + ->header_like( 'Link' => qr/; rel="next",/ ) + ->header_like( 'Link' => qr/; rel="next",/ ) + ->header_like( 'Link' => qr/; rel="first",/ ) + ->header_like( 'Link' => qr/; rel="first",/ ) + ->header_like( 'Link' => qr/; rel="first",/ ) + ->header_like( 'Link' => qr/; rel="last"/ ) + ->header_like( 'Link' => qr/; rel="last"/ ) + ->header_like( 'Link' => qr/; rel="last"/ ); + + $t->get_ok('/pagination_headers_first_page') + ->status_is( 200 ) + ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) + ->header_unlike( 'Link' => qr/; rel="prev",/ ) + ->header_like( 'Link' => qr/; rel="next",/ ) + ->header_like( 'Link' => qr/; rel="next",/ ) + ->header_like( 'Link' => qr/; rel="next",/ ) + ->header_like( 'Link' => qr/; rel="first",/ ) + ->header_like( 'Link' => qr/; rel="first",/ ) + ->header_like( 'Link' => qr/; rel="first",/ ) + ->header_like( 'Link' => qr/; rel="last"/ ) + ->header_like( 'Link' => qr/; rel="last"/ ) + ->header_like( 'Link' => qr/; rel="last"/ ); + + $t->get_ok('/pagination_headers_last_page') + ->status_is( 200 ) + ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) + ->header_like( 'Link' => qr/; rel="prev",/ ) + ->header_like( 'Link' => qr/; rel="prev",/ ) + ->header_like( 'Link' => qr/; rel="prev",/ ) + ->header_unlike( 'Link' => qr/; rel="next",/ ) + ->header_like( 'Link' => qr/; rel="first",/ ) + ->header_like( 'Link' => qr/; rel="first",/ ) + ->header_like( 'Link' => qr/; rel="first",/ ) + ->header_like( 'Link' => qr/; rel="last"/ ) + ->header_like( 'Link' => qr/; rel="last"/ ) + ->header_like( 'Link' => qr/; rel="last"/ ); +}; -- 2.7.4