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

(-)a/t/db_dependent/Koha/Object.t (-3 / +122 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 12;
20
use Test::More tests => 16;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
use DateTime;
23
use DateTime;
Lines 260-266 subtest "to_api() tests" => sub { Link Here
260
260
261
    # Pick a class that won't have a mapping for the API
261
    # Pick a class that won't have a mapping for the API
262
    my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
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' );
263
    is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no overloaded to_api_mapping method, return TO_JSON' );
264
265
    $schema->storage->txn_rollback;
266
};
267
268
subtest "to_api_mapping() tests" => sub {
269
270
    plan tests => 1;
271
272
    $schema->storage->txn_begin;
273
274
    my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
275
    is_deeply( $illrequest->to_api_mapping, {}, 'If no to_api_mapping present, return empty hashref' );
276
277
    $schema->storage->txn_rollback;
278
};
279
280
subtest "from_api_mapping() tests" => sub {
281
282
    plan tests => 3;
283
284
    $schema->storage->txn_begin;
285
286
    my $city = $builder->build_object({ class => 'Koha::Cities' });
287
288
    # Lets emulate an undef
289
    my $city_class = Test::MockModule->new('Koha::City');
290
    $city_class->mock( 'to_api_mapping',
291
        sub {
292
            return {
293
                cityid       => 'city_id',
294
                city_country => 'country',
295
                city_zipcode => undef
296
            };
297
        }
298
    );
299
300
    is_deeply(
301
        $city->from_api_mapping,
302
        {
303
            city_id => 'cityid',
304
            country => 'city_country'
305
        },
306
        'Mapping returns correctly, undef ommited'
307
    );
308
309
    $city_class->unmock( 'to_api_mapping');
310
    $city_class->mock( 'to_api_mapping',
311
        sub {
312
            return {
313
                cityid       => 'city_id',
314
                city_country => 'country',
315
                city_zipcode => 'postal_code'
316
            };
317
        }
318
    );
319
320
    is_deeply(
321
        $city->from_api_mapping,
322
        {
323
            city_id => 'cityid',
324
            country => 'city_country'
325
        },
326
        'Reverse mapping is cached'
327
    );
328
329
    # Get a fresh object
330
    $city = $builder->build_object({ class => 'Koha::Cities' });
331
    is_deeply(
332
        $city->from_api_mapping,
333
        {
334
            city_id     => 'cityid',
335
            country     => 'city_country',
336
            postal_code => 'city_zipcode'
337
        },
338
        'Fresh mapping loaded'
339
    );
340
341
    $schema->storage->txn_rollback;
342
};
343
344
subtest 'set_from_api() tests' => sub {
345
346
    plan tests => 4;
347
348
    $schema->storage->txn_begin;
349
350
    my $city = $builder->build_object({ class => 'Koha::Cities' });
351
    my $city_unblessed = $city->unblessed;
352
    my $attrs = {
353
        name        => 'Cordoba',
354
        country     => 'Argentina',
355
        postal_code => '5000'
356
    };
357
    $city->set_from_api($attrs);
358
359
    is( $city->city_state, $city_unblessed->{city_state}, 'Untouched attributes are preserved' );
360
    is( $city->city_name, $attrs->{name}, 'city_name updated correctly' );
361
    is( $city->city_country, $attrs->{country}, 'city_country updated correctly' );
362
    is( $city->city_zipcode, $attrs->{postal_code}, 'city_zipcode updated correctly' );
363
364
    $schema->storage->txn_rollback;
365
};
366
367
subtest 'new_from_api() tests' => sub {
368
369
    plan tests => 4;
370
371
    $schema->storage->txn_begin;
372
373
    my $attrs = {
374
        name        => 'Cordoba',
375
        country     => 'Argentina',
376
        postal_code => '5000'
377
    };
378
    my $city = Koha::City->new_from_api($attrs);
379
380
    is( ref($city), 'Koha::City', 'Object type is correct' );
381
    is( $city->city_name,    $attrs->{name}, 'city_name updated correctly' );
382
    is( $city->city_country, $attrs->{country}, 'city_country updated correctly' );
383
    is( $city->city_zipcode, $attrs->{postal_code}, 'city_zipcode updated correctly' );
264
384
265
    $schema->storage->txn_rollback;
385
    $schema->storage->txn_rollback;
266
};
386
};
267
- 

Return to bug 23893