From: Marius Gavrilescu Date: Fri, 16 May 2014 12:30:02 +0000 (+0300) Subject: Add tests X-Git-Tag: 5999.000_003~5 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=acb202c6ea57f93d21ded0a8acf7138069257081;p=gruntmaster-data.git Add tests --- diff --git a/Makefile.PL b/Makefile.PL index f94f5f1..cccd7c7 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -21,6 +21,9 @@ WriteMakefile( Sub::Name 0 Term::ANSIColor 0/, }, + BUILD_REQUIRES => { + qw/DBD::SQLite 0/, + } META_MERGE => { dynamic_config => 0, resources => { diff --git a/t/Gruntmaster-Data.t b/t/Gruntmaster-Data.t index b3b8672..b8f28d7 100644 --- a/t/Gruntmaster-Data.t +++ b/t/Gruntmaster-Data.t @@ -1,6 +1,38 @@ #!/usr/bin/perl -w use v5.14; -use Test::More tests => 1; +use Test::More tests => 13; BEGIN { use_ok('Gruntmaster::Data') }; + +my $db = Gruntmaster::Data->connect('dbi:SQLite:dbname=:memory:'); +$db->deploy; + +$db->users->create({id => 'MGV'}); +$db->contests->create({id => 'fc', start => 10, stop => 20, name => 'Finished contest', owner => 'MGV'}); +$db->contests->create({id => 'rc', start => 20, stop => 30, name => 'Running contest', owner => 'MGV'}); +$db->contests->create({id => 'pc', start => 30, stop => 40, name => 'Pending contest', owner => 'MGV'}); + +ok $db->contest('pc')->is_pending(25), 'is_pending'; +ok !$db->contest('rc')->is_pending(25), '!is_pending'; +ok $db->contest('fc')->is_finished(25), 'is_finished'; +ok !$db->contest('rc')->is_finished(25), '!is_finished'; +ok $db->contest('rc')->is_running(25), 'is_running'; + +$db->problems->create({id => 'pb', name => 'Problem', generator => 'Undef', runner => 'File', judge => 'Absolute', level => 'beginner', owner => 'MGV', statement => '...', testcnt => 1, timeout => 1, private => 0}); + +ok !$db->problem('pb')->is_private(25), '!is_private'; +$db->problem('pb')->update({private => 1}); +ok $db->problem('pb')->is_private(25), 'is_private (explicit)'; +$db->problem('pb')->update({private => 0}); + +$db->contest_problems->create({contest => 'pc', problem => 'pb'}); +ok $db->problem('pb')->is_private(25), 'is_private (implicit)'; +ok $db->problem('pb')->is_in_archive(25), 'is_in_archive'; + +$db->contest_problems->create({contest => 'rc', problem => 'pb'}); +ok $db->problem('pb')->is_private(25), 'is_private (also implicit)'; +ok !$db->problem('pb')->is_in_archive(25), '!is_in_archive'; + +$db->contest_problems->find('rc', 'pb')->delete; +ok $db->problem('pb')->is_in_archive(25), 'is_in_archive (again)'; diff --git a/t/tools.t b/t/tools.t new file mode 100644 index 0000000..2735593 --- /dev/null +++ b/t/tools.t @@ -0,0 +1,120 @@ +#!/usr/bin/perl -w +use v5.14; + +use Test::More tests => 13; +use File::Temp qw/tempdir/; + +use Gruntmaster::Data; + +my $dir = tempdir CLEANUP => 1; +$ENV{GRUNTMASTER_DSN} = "dbi:SQLite:dbname=$dir/testdb"; + +our $db; + +sub withdb (&) { + local $db = Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN}); + shift->() +} + +withdb { $db->deploy }; + +my $pipe; + +open $pipe, '|./gruntmaster-contest add ct'; +print $pipe <<''; +My cool contest +MGV +2014-01-01 00:00Z +2014-01-01 05:00Z + +close $pipe; + +withdb { + subtest 'gruntmaster-contest add' => sub { + plan tests => 5; + my $ct = $db->contest('ct'); + ok $ct, 'contest exists'; + is $ct->name, 'My cool contest', 'contest name'; + is $ct->owner->id, 'MGV', 'contest owner'; + is $ct->start, 1388534400, 'contest start'; + is $ct->stop, 1388534400 + 5 * 60 * 60, 'contest stop'; + } +}; + +is `./gruntmaster-contest get ct owner`, "MGV\n", 'gruntmaster-contest get'; +system './gruntmaster-contest', 'set', 'ct', 'owner', 'nobody'; +withdb { is $db->contest('ct')->owner->id, 'nobody', 'gruntmaster-contest set' }; + +withdb { $db->contests->create({id => 'dummy', name => 'Dummy contest', owner => 'MGV', start => 0, stop => 1}) }; +my @list = sort `./gruntmaster-contest list`; +chomp @list; +my @list2 = withdb { map { $_->id } $db->contests->all }; +is_deeply \@list, [ sort @list2 ], 'gruntmaster-contest list'; + +system './gruntmaster-contest', 'rm', 'dummy'; +withdb { ok !$db->contest('dummy'), 'gruntmaster-contest rm' }; + +open $pipe, '|./gruntmaster-problem add pb'; +print $pipe <<''; +Test problem +n +ct +Marius Gavrilescu +Smaranda Ciubotaru +MGV +b +gruntmaster-problem +c +a +a +3 +1 +100 +Ok +Ok +Ok + +close $pipe; + +withdb { + subtest 'gruntmaster-problem add' => sub { + plan tests => 13; + my $pb = $db->problem('pb'); + ok $pb, 'problem exists'; + is $pb->name, 'Test problem', 'name'; + is $pb->author, 'Marius Gavrilescu', 'author'; + is $pb->writer, 'Smaranda Ciubotaru', 'statement writer'; + is $pb->owner->id, 'MGV', 'owner'; + is $pb->level, 'easy', 'level'; + is $pb->generator, 'Undef', 'generator'; + is $pb->runner, 'File', 'runner'; + is $pb->judge, 'Absolute', 'judge'; + is $pb->testcnt, 3, 'test count'; + is $pb->timeout, 1, 'time limit'; + is $pb->olimit, 100, 'output limit'; + ok $db->contest_problems->find('ct', 'pb'), 'is in contest'; + } +}; + +is `./gruntmaster-problem get pb author`, "Marius Gavrilescu\n", 'gruntmaster-problem get'; +system './gruntmaster-problem set pb owner nobody'; +withdb { is $db->problem('pb')->owner->id, 'nobody', 'gruntmaster-problem set' }; + +withdb { $db->problems->create({id => 'dummy', name => 'Dummy', generator => 'Undef', runner => 'File', judge => 'Absolute', level => 'beginner', owner => 'MGV', statement => '...', testcnt => 1, timeout => 1}) }; + +@list = sort `./gruntmaster-problem list`; +chomp @list; +@list2 = withdb { map { $_->id } $db->problems->all }; +is_deeply \@list, [ sort @list2 ], 'gruntmaster-problem list'; + +system './gruntmaster-problem', 'rm', 'dummy'; +withdb { ok !$db->problem('dummy'), 'gruntmaster-problem rm' }; + +withdb { $db->jobs->create({id => 1, date => 1, extension => '.ext', format => 'CPP', problem => 'pb', source => '...', owner => 'MGV'}) }; + +is `./gruntmaster-job get 1 format`, "CPP\n", 'gruntmaster-job get'; +system './gruntmaster-job', 'set', 1, 'format', 'PERL'; +withdb { is $db->job(1)->format, 'PERL', 'gruntmaster-job set' }; + +system './gruntmaster-job', 'rm', 1; +withdb { ok !$db->job(1), 'gruntmaster-job rm' };