Bug 23770: Unit tests

This patch introduces tests for the new to_api method introduced in
Koha::Object(s).

It uses Koha::City as a sample for simplicity. And it also uses
Koha::Illrequests to test the case in which there's no to_api_mapping
method defined on the class, to highlight that a fallback to calling
TO_JSON is done. [1]

[1] This is done under the assumption Illrequests controller code
doesn't use any kind of mapping as the rest of the API controllers do,
so there's little chance it would be added in a future.

To test:
1. Apply this patchset
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Object.t \
           t/db_dependent/Koha/Objects.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Sponsored-by: ByWater Solutions
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomas Cohen Arazi 2019-10-08 12:13:23 -03:00 committed by Martin Renvoize
parent ab4a8755c2
commit 7ce5cb0865
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
2 changed files with 79 additions and 2 deletions

View File

@ -17,7 +17,7 @@
use Modern::Perl;
use Test::More tests => 11;
use Test::More tests => 12;
use Test::Exception;
use Test::Warn;
use DateTime;
@ -213,6 +213,58 @@ subtest 'TO_JSON tests' => sub {
$schema->storage->txn_rollback;
};
subtest "to_api() tests" => sub {
plan tests => 11;
$schema->storage->txn_begin;
my $city = $builder->build_object({ class => 'Koha::Cities' });
# THE mapping
# cityid => 'city_id',
# city_country => 'country',
# city_name => 'name',
# city_state => 'state',
# city_zipcode => 'postal_code'
my $api_city = $city->to_api;
is( $api_city->{city_id}, $city->cityid, 'Attribute translated correctly' );
is( $api_city->{country}, $city->city_country, 'Attribute translated correctly' );
is( $api_city->{name}, $city->city_name, 'Attribute translated correctly' );
is( $api_city->{state}, $city->city_state, 'Attribute translated correctly' );
is( $api_city->{postal_code}, $city->city_zipcode, 'Attribute translated correctly' );
# Lets emulate an undef
my $city_class = Test::MockModule->new('Koha::City');
$city_class->mock( 'to_api_mapping',
sub {
return {
cityid => 'city_id',
city_country => 'country',
city_name => 'name',
city_state => 'state',
city_zipcode => undef
};
}
);
$api_city = $city->to_api;
is( $api_city->{city_id}, $city->cityid, 'Attribute translated correctly' );
is( $api_city->{country}, $city->city_country, 'Attribute translated correctly' );
is( $api_city->{name}, $city->city_name, 'Attribute translated correctly' );
is( $api_city->{state}, $city->city_state, 'Attribute translated correctly' );
ok( !exists $api_city->{postal_code}, 'Attribute removed' );
# Pick a class that won't have a mapping for the API
my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' );
$schema->storage->txn_rollback;
};
subtest "Test update method" => sub {
plan tests => 6;

View File

@ -19,7 +19,7 @@
use Modern::Perl;
use Test::More tests => 15;
use Test::More tests => 16;
use Test::Exception;
use Test::Warn;
@ -280,3 +280,28 @@ subtest '->search() tests' => sub {
$schema->storage->txn_rollback;
};
subtest "to_api() tests" => sub {
plan tests => 4;
$schema->storage->txn_begin;
my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
my $cities = Koha::Cities->search(
{
cityid => [ $city_1->cityid, $city_2->cityid ]
},
{ -orderby => { -desc => 'cityid' } }
);
is( $cities->count, 2, 'Count is correct' );
my $cities_api = $cities->to_api;
is( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
$schema->storage->txn_rollback;
};