#!/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)';
--- /dev/null
+#!/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' };