@@ -, +, @@ $ kshell k$ prove t/db_dependent/Koha/Object.t \ t/db_dependent/Koha/Objects.t --- t/db_dependent/Koha/Object.t | 54 ++++++++++++++++++++++++++++++++++++++++++- t/db_dependent/Koha/Objects.t | 27 +++++++++++++++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) --- a/t/db_dependent/Koha/Object.t +++ a/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; --- a/t/db_dependent/Koha/Objects.t +++ a/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; +}; --