Bugzilla – Attachment 94398 Details for
Bug 23770
Add Koha::Object(s)->to_api method
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 23770: Unit tests
Bug-23770-Unit-tests.patch (text/plain), 4.99 KB, created by
Marcel de Rooy
on 2019-10-18 09:00:04 UTC
(
hide
)
Description:
Bug 23770: Unit tests
Filename:
MIME Type:
Creator:
Marcel de Rooy
Created:
2019-10-18 09:00:04 UTC
Size:
4.99 KB
patch
obsolete
>From 4f487d69e58f30de0fb58516504d34dde36e0989 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 8 Oct 2019 12:13:23 -0300 >Subject: [PATCH] Bug 23770: Unit tests >Content-Type: text/plain; charset=utf-8 > >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 <tomascohen@theke.io> >Sponsored-by: ByWater Solutions > >Signed-off-by: Josef Moravec <josef.moravec@gmail.com> > >Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >--- > 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
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 23770
:
93831
|
93881
|
93882
|
93883
|
93888
|
93890
|
93891
|
93892
|
93893
|
93894
|
93895
|
94396
|
94397
| 94398 |
94399
|
94400