View | Details | Raw Unified | Return to bug 23770
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Object.t (-1 / +53 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 11;
20
use Test::More tests => 12;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
use DateTime;
23
use DateTime;
Lines 213-218 subtest 'TO_JSON tests' => sub { Link Here
213
    $schema->storage->txn_rollback;
213
    $schema->storage->txn_rollback;
214
};
214
};
215
215
216
subtest "to_api() tests" => sub {
217
218
    plan tests => 11;
219
220
    $schema->storage->txn_begin;
221
222
    my $city = $builder->build_object({ class => 'Koha::Cities' });
223
224
    # THE mapping
225
    # cityid       => 'city_id',
226
    # city_country => 'country',
227
    # city_name    => 'name',
228
    # city_state   => 'state',
229
    # city_zipcode => 'postal_code'
230
231
    my $api_city = $city->to_api;
232
233
    is( $api_city->{city_id},     $city->cityid,       'Attribute translated correctly' );
234
    is( $api_city->{country},     $city->city_country, 'Attribute translated correctly' );
235
    is( $api_city->{name},        $city->city_name,    'Attribute translated correctly' );
236
    is( $api_city->{state},       $city->city_state,   'Attribute translated correctly' );
237
    is( $api_city->{postal_code}, $city->city_zipcode, 'Attribute translated correctly' );
238
239
    # Lets emulate an undef
240
    my $city_class = Test::MockModule->new('Koha::City');
241
    $city_class->mock( 'to_api_mapping',
242
        sub {
243
            return {
244
                cityid       => 'city_id',
245
                city_country => 'country',
246
                city_name    => 'name',
247
                city_state   => 'state',
248
                city_zipcode => undef
249
            };
250
        }
251
    );
252
253
    $api_city = $city->to_api;
254
255
    is( $api_city->{city_id},     $city->cityid,       'Attribute translated correctly' );
256
    is( $api_city->{country},     $city->city_country, 'Attribute translated correctly' );
257
    is( $api_city->{name},        $city->city_name,    'Attribute translated correctly' );
258
    is( $api_city->{state},       $city->city_state,   'Attribute translated correctly' );
259
    ok( !exists $api_city->{postal_code}, 'Attribute removed' );
260
261
    # Pick a class that won't have a mapping for the API
262
    my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
263
    is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' );
264
265
    $schema->storage->txn_rollback;
266
};
267
216
subtest "Test update method" => sub {
268
subtest "Test update method" => sub {
217
    plan tests => 6;
269
    plan tests => 6;
218
270
(-)a/t/db_dependent/Koha/Objects.t (-2 / +26 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 15;
22
use Test::More tests => 16;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
use Test::Warn;
25
25
Lines 280-282 subtest '->search() tests' => sub { Link Here
280
280
281
    $schema->storage->txn_rollback;
281
    $schema->storage->txn_rollback;
282
};
282
};
283
- 
283
284
subtest "to_api() tests" => sub {
285
286
    plan tests => 4;
287
288
    $schema->storage->txn_begin;
289
290
    my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
291
    my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
292
293
    my $cities = Koha::Cities->search(
294
        {
295
            cityid => [ $city_1->cityid, $city_2->cityid ]
296
        },
297
        { -orderby => { -desc => 'cityid' } }
298
    );
299
300
    is( $cities->count, 2, 'Count is correct' );
301
    my $cities_api = $cities->to_api;
302
    is( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
303
    is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
304
    is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
305
306
    $schema->storage->txn_rollback;
307
};

Return to bug 23770