App-Dochazka-REST
view release on metacpan or search on metacpan
t/model/employee.t view on Meta::CPAN
note( 'spawn an employee object for Mr. Fu' );
$emp = App::Dochazka::REST::Model::Employee->spawn(
nick => 'mrfu',
fullname => 'Mr. Fu',
email => 'mrfu@example.com',
sec_id => 1024,
);
isa_ok( $emp, 'App::Dochazka::REST::Model::Employee' );
note( 'insert Mr. Fu into employees table' );
$status = $emp->insert( $faux_context );
is( $status->level, 'OK', "Mr. Fu inserted" );
my $eid_of_mrfu = $emp->{eid};
#diag( "eid of mrfu is $eid_of_mrfu" );
is( noof( $dbix_conn, 'employees' ), 3, "Total number of employees is now 3" );
$status = list_employees_by_priv( $dbix_conn, 'all' );
test_employee_list( $status, [ 'demo', 'mrfu', 'root' ] );
note( 'load Mr. Fu by secondary ID (success)' );
$status = App::Dochazka::REST::Model::Employee->load_by_sec_id( $dbix_conn, 1024 );
is( $status->level, 'OK' );
is( $status->code, 'DISPATCH_RECORDS_FOUND', "load_by_sec_id returned OK status" );
note( 'load Mr. Fu by secondary ID (failure)' );
$status = App::Dochazka::REST::Model::Employee->load_by_sec_id( $dbix_conn, 1025 );
is( $status->level, 'NOTICE' );
is( $status->code, 'DISPATCH_NO_RECORDS_FOUND', "load_by_sec_id returned OK status" );
note( 'nick_exists and eid_exists functions' );
ok( nick_exists( $dbix_conn, 'mrfu' ) );
ok( nick_exists( $dbix_conn, 'root' ) );
ok( nick_exists( $dbix_conn, 'demo' ) );
ok( eid_exists( $dbix_conn, $eid_of_mrfu ) );
ok( eid_exists( $dbix_conn, $eid_of_root ) );
ok( ! nick_exists( $dbix_conn, 'fandango' ) );
ok( ! nick_exists( $dbix_conn, 'barbarian' ) );
ok( ! eid_exists( $dbix_conn, 1341 ) );
ok( ! eid_exists( $dbix_conn, 554 ) );
note( 'spawn another object (still Mr. Fu)' );
$status = App::Dochazka::REST::Model::Employee->load_by_eid( $dbix_conn, $eid_of_mrfu );
is( $status->code, 'DISPATCH_RECORDS_FOUND', "load_by_eid returned OK status" );
my $mrfu = $status->payload;
is( $mrfu->{eid}, $eid_of_mrfu, "EID matches that of Mr. Fu" );
is( $mrfu->{nick}, 'mrfu', "Nick should be mrfu" );
note( 'get_all_sync_employees() returns nothing' );
$status = get_all_sync_employees( $dbix_conn );
is( $status->level, 'NOTICE' );
is( $status->code, 'DISPATCH_NO_RECORDS_FOUND' );
note( 'set Mr. Fu sync property to true' );
ok( ! $mrfu->sync );
is( $mrfu->sync, 0 );
$mrfu->sync( 1 );
ok( $mrfu->sync );
is( $mrfu->sync, 1 );
note( 'update Mr. Fu database record' );
$status = $mrfu->update( $faux_context );
is( $status->level, 'OK', 'Mr. Fu database record updated' );
is( $status->code, 'DOCHAZKA_CUD_OK' );
ok( $status->payload );
is( $status->payload->sync, 1 );
note( 'get_all_sync_employees() returns Mr. Fu' );
$status = get_all_sync_employees( $dbix_conn );
is( $status->level, 'OK' );
is( $status->code, 'DISPATCH_RECORDS_FOUND' );
ok( $status->payload );
is( ref( $status->payload ), 'ARRAY' );
is( scalar( @{ $status->payload } ), 1 );
is( $status->payload->[0]->nick, 'mrfu' );
note( 'spawn Mrs. Fu object' );
$emp = App::Dochazka::REST::Model::Employee->spawn(
nick => 'mrsfu',
sec_id => 78923,
email => 'consort@futown.orient.cn',
fullname => 'Mrs. Fu',
);
isa_ok( $emp, 'App::Dochazka::REST::Model::Employee' );
note( 'insert Mrs. Fu into employees table' );
$status = $emp->insert( $faux_context );
ok( $status->ok, "Mrs. Fu inserted" );
my $eid_of_mrsfu = $emp->{eid};
isnt( $eid_of_mrsfu, $eid_of_mrfu, "Mr. and Mrs. Fu are distinct entities" );
note( 'recycle the object by assigning undef to each attribute' );
$status = $emp->reset;
foreach my $prop ( App::Dochazka::REST::Model::Employee->attrs ) {
is( $emp->get( $prop), undef );
}
note( 'attempt to load a non-existent EID' );
$status = $emp->load_by_eid( $dbix_conn, 443 );
is( $status->level, 'NOTICE' );
is( $status->code, 'DISPATCH_NO_RECORDS_FOUND', "Nick ID 443 does not exist" );
is( $status->{'count'}, 0, "Nick ID 443 does not exist" );
note( 'attempt to load a non-existent nick' );
$status = $emp->load_by_nick( $dbix_conn, 'smithfarm' );
is( $status->level, 'NOTICE' );
is( $status->code, 'DISPATCH_NO_RECORDS_FOUND' );
is( $status->{'count'}, 0 );
note( 'load Mrs. Fu' );
$status = $emp->load_by_nick( $dbix_conn, 'mrsfu' );
is( $status->code, 'DISPATCH_RECORDS_FOUND', "Nick mrsfu exists" );
$emp = $status->payload;
is( $emp->nick, 'mrsfu', "Mrs. Fu's nick is the right string" );
is( $emp->sec_id, 78923, "Mrs. Fu's secondary ID is the right string" );
note( 'update Mrs. Fu by setting fullname attribute to a new value' );
$emp->fullname( "Mrs. Fu that's Ma'am to you" );
is( $emp->fullname, "Mrs. Fu that's Ma'am to you" );
$status = $emp->update( $faux_context );
ok( $status->ok, "UPDATE status is OK" );
$status = App::Dochazka::REST::Model::Employee->load_by_nick( $dbix_conn, 'mrsfu' );
is( $status->code, 'DISPATCH_RECORDS_FOUND', "Nick mrsfu exists" );
( run in 0.705 second using v1.01-cache-2.11-cpan-63c85eba8c4 )