From 851a6dca185fa43a5b07a4274119c70b0171cd1f Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 8 Oct 2019 12:13:23 -0300 Subject: [PATCH] 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 Signed-off-by: Tomas Cohen Arazi Sponsored-by: ByWater Solutions Signed-off-by: Josef Moravec --- t/db_dependent/Koha/Object.t | 54 ++++++++++++++++++++++++++++++++++++++++++- t/db_dependent/Koha/Objects.t | 27 +++++++++++++++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index fb57218afa..7570b62afe 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -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; diff --git a/t/db_dependent/Koha/Objects.t b/t/db_dependent/Koha/Objects.t index db7711e0db..8cd7c437ac 100644 --- a/t/db_dependent/Koha/Objects.t +++ b/t/db_dependent/Koha/Objects.t @@ -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; +}; -- 2.11.0