App-Dochazka-REST
view release on metacpan or search on metacpan
t/model/employee.t view on Meta::CPAN
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" );
my $emp2 = $status->payload;
is_deeply( $emp, $emp2 );
note( "pathologically change Mrs. Fu's nick to null" );
my $saved_nick = $emp->nick;
$emp->{'nick'} = undef;
$status = $emp->update( $faux_context );
ok( $status->not_ok );
is( $status->level, 'ERR' );
is( $status->code, 'DOCHAZKA_DBI_ERR' );
$emp->nick( $saved_nick );
note( "attempt to change Mr. Fu's supervisor to Mr. Fu - i.e. he would supervise himself" );
$mrfu->supervisor( $eid_of_mrfu );
$status = $mrfu->update( $faux_context );
is( $status->level, 'ERR' );
is( $status->code, 'DOCHAZKA_DBI_ERR' );
note( "Load Mr. Fu employee object" );
$status = App::Dochazka::REST::Model::Employee->load_by_nick( $dbix_conn, 'mrfu' );
is( $status->code, 'DISPATCH_RECORDS_FOUND', "Nick mrsfu exists" );
$mrfu = $status->payload;
note( "Mr. Fu has no reports" );
$status = $mrfu->has_reports( $dbix_conn );
is( $status->level, 'OK' );
is( $status->code, 'DISPATCH_EMPLOYEE_HAS_REPORTS_EID' );
is( $status->payload, 0 );
note( "Mrs. Fu has no reports" );
$status = $mrfu->has_reports( $dbix_conn );
is( $status->level, 'OK' );
is( $status->code, 'DISPATCH_EMPLOYEE_HAS_REPORTS_EID' );
is( $status->payload, 0 );
note( "Change Mr. Fu's supervisor to Mrs. Fu (EID $eid_of_mrsfu)" );
$mrfu->supervisor( $eid_of_mrsfu );
$status = $mrfu->update( $faux_context );
ok( $status->ok, "UPDATE status is OK" );
is( $mrfu->supervisor, $eid_of_mrsfu );
note( "List Mrs. Fu's team" );
$status = $emp->team_nicks( $dbix_conn );
is( $status->level, 'OK' );
is( $status->code, 'DISPATCH_LIST_EMPLOYEE_NICKS_TEAM' );
is_deeply( $status->payload, [ 'mrfu' ] );
note( "Mr. Fu still has no reports" );
$status = $mrfu->has_reports( $dbix_conn );
is( $status->level, 'OK' );
is( $status->code, 'DISPATCH_EMPLOYEE_HAS_REPORTS_EID' );
is( $status->payload, 0 );
note( "Mrs. Fu now has 1 report" );
$status = $emp->has_reports( $dbix_conn );
is( $status->level, 'OK' );
is( $status->code, 'DISPATCH_EMPLOYEE_HAS_REPORTS_EID' );
is( $status->payload, 1 );
note( "attempt to change Mrs. Fu's EID" );
my $saved_eid = $emp->eid;
ok( $saved_eid > 1 );
my $bogus_eid = 34342;
$emp->eid( $bogus_eid );
ok( $emp->eid != $saved_eid );
ok( $saved_eid != $emp->eid );
is( $emp->eid, $bogus_eid );
$status = $emp->update( $faux_context );
( run in 1.303 second using v1.01-cache-2.11-cpan-d7f47b0818f )