App-Dochazka-REST

 view release on metacpan or  search on metacpan

t/model/schedule.t  view on Meta::CPAN


#use App::CELL::Test::LogToFile;
use App::CELL qw( $meta $site );
use Data::Dumper;
use App::Dochazka::Common qw( $today $yesterday $tomorrow );
use App::Dochazka::REST::ConnBank qw( $dbix_conn );
use App::Dochazka::REST::Model::Employee;
use App::Dochazka::REST::Model::Schedule qw( sid_exists );
use App::Dochazka::REST::Model::Schedhistory;
use App::Dochazka::REST::Model::Schedintvls;
use App::Dochazka::REST::Model::Shared qw( noof );
use App::Dochazka::REST::Test;
use Test::JSON;
use Test::More; 
use Test::Warnings;

note('initialize');
initialize_regression_test();

my $today_ts = $today . " 00:00:00";

note('spawn and insert employee object');
is( noof( $dbix_conn, "employees" ), 2 );

my $emp = App::Dochazka::REST::Model::Employee->spawn(
    nick => 'mrsched',
    remark => 'SCHEDULE TESTING OBJECT',
);
my $status = $emp->insert( $faux_context );
ok( $status->ok, "Schedule testing employee object inserted" );
ok( $emp->eid > 0, "Schedule testing employee object has an EID" );

my $schedule = test_schedule_model( [
    "[$tomorrow 12:30, $tomorrow 16:30)",
    "[$tomorrow 08:00, $tomorrow 12:00)",
    "[$today 12:30, $today 16:30)",
    "[$today 08:00, $today 12:00)",
    "[$yesterday 12:30, $yesterday 16:30)",
    "[$yesterday 08:00, $yesterday 12:00)",
] );

note('Attempt to change the "schedule" field to a bogus string');
my $saved_sched_obj = $schedule->clone;
$schedule->schedule( 'BOGUS STRING' );
is( $schedule->schedule, 'BOGUS STRING' );
$status = $schedule->update( $faux_context );
is( $status->level, 'OK' );
my $new_sched_obj = App::Dochazka::REST::Model::Schedule->spawn( $status->payload );
ok( $schedule->compare( $saved_sched_obj ) );
ok( $schedule->compare_disabled( $saved_sched_obj ) );

note('Attempt to change the "sid" field');
$saved_sched_obj = $schedule->clone;
#diag( Dumper $saved_sched_obj );
#BAIL_OUT(0);
$schedule->sid( 99943 );
is( $schedule->{sid}, 99943 );
$status = $schedule->update( $faux_context );
is( $status->level, 'OK' );
is( $status->{'DBI_return_value'}, '0E0' );
# but the value in the database is unchanged - the 'sid' and 'schedule' fields are never updated
$status = App::Dochazka::REST::Model::Schedule->load_by_sid( $dbix_conn, $saved_sched_obj->sid );
is( $status->level, 'OK' );
is( $status->payload->{sid}, $saved_sched_obj->sid ); # no real change
$schedule = $status->payload;
note('(in other words, nothing changed)');

note('Make a bogus schedintvls object and attempt to delete it');
my $bogus_intvls = App::Dochazka::REST::Model::Schedintvls->spawn;
$status = $bogus_intvls->delete( $dbix_conn );
is( $status->level, 'WARN', "Could not delete bogus intervals" );

note('Attempt to re-insert the same schedule');
my $sid_copy = $schedule->sid;        # store a local copy of the SID
my $sched_copy = $schedule->schedule; # store a local copy of the schedule (JSON)
$schedule->reset;		      # reset object to factory settings
$schedule->{schedule} = $sched_copy;  # set up object to "re-insert" the same schedule
is( $schedule->{sid}, undef, "SID is undefined at this point" );
$status = $schedule->insert( $faux_context );
if( $status->level ne 'OK' ) {
    diag( Dumper $status );
    diag( "Bailing out at MARK 01" );
    BAIL_OUT(0);
}
ok( $status->ok );
is( $schedule->{sid}, $sid_copy );    # SID is unchanged

note('attempt to insert the same schedule string in a completely new schedule object');
is( noof( $dbix_conn, 'schedules' ), 2, "schedules row count is 2" );
my $schedule2 = App::Dochazka::REST::Model::Schedule->spawn(
    schedule => $sched_copy,
    remark => 'DUPLICATE',
);
is_valid_json( $schedule2->schedule, "String is valid JSON" );
$status = $schedule2->insert( $faux_context );
ok( $schedule2->sid > 0, "SID was assigned" );
ok( $status->ok, "Schedule insert OK" );
is( $schedule2->sid, $sid_copy, "But SID is the same as before" );
is( noof( $dbix_conn, 'schedules' ), 2, "schedules row count is still 2" );

#note('tests for get_schedule_json function');
#my $json = get_schedule_json( $sid_copy );
#is( ref( $json ), 'ARRAY' );
#is( get_schedule_json( 994), undef, "Non-existent SID" );

note('Now that we finally have the schedule safely in the database, we can assign it to the employee (Mr. Sched) by inserting a record in the schedhistory table');
my $schedhistory = App::Dochazka::REST::Model::Schedhistory->spawn(
    eid => $emp->{eid},
    sid => $schedule->{sid},
    effective => $today,
    remark => 'TESTING',
);
isa_ok( $schedhistory, 'App::Dochazka::REST::Model::Schedhistory', "schedhistory object is an object" );

note('test schedhistory accessors');
is( $schedhistory->eid, $emp->{eid} );
is( $schedhistory->sid, $schedule->{sid} );
is( $schedhistory->effective, $today );
is( $schedhistory->remark, 'TESTING' );
is( $schedhistory->scode, undef, "scode property not populated yet" );



( run in 0.707 second using v1.01-cache-2.11-cpan-524268b4103 )