#!/usr/bin/perl -w
use v5.14;
-use Test::More skip_all => 'These tests are badly outdated and broken';
-
-use Test::More tests => 13;
-use File::Temp qw/tempdir/;
-use Config;
-
+use Test::More;
+use Gruntmaster::App;
use Gruntmaster::Data;
+use App::Cmd::Tester;
-my $perl = $Config{perlpath} . $Config{_exe};
-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->()
+BEGIN {
+ eval {
+ Gruntmaster::Data::init 'dbi:Pg:dbname=gmtest'; 1;
+ } or plan skip_all => 'Cannot connect to test database. Create it by running make_test_db.sh before running this test. '. "Error: $@";
+ plan tests => 28;
}
-withdb { $db->deploy };
+sub fakein {
+ open my $fakein, '<', \$_[0];
+ *STDIN = $fakein;
+}
-my $pipe;
+sub ta {
+ my ($args, $out, $err) = @_;
+ my $ret = test_app 'Gruntmaster::App' => $args;
+ diag 'Error: ', $ret->error if defined $ret->error;
+ is $ret->output, "$out\n", join ' ', gm => @$args if defined $out;
+ $ret
+}
-open $pipe, "|$perl gruntmaster-contest add ct";
-print $pipe <<'';
+fakein <<EOF;
My cool contest
MGV
2014-01-01 00:00Z
2014-01-01 05:00Z
+EOF
-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';
- }
-};
+ta [qw/-c add ct/];
-is `$perl gruntmaster-contest get ct owner`, "MGV\n", 'gruntmaster-contest get';
-system $perl, 'gruntmaster-contest', 'set', 'ct', 'owner', 'nobody';
-withdb { is $db->contest('ct')->owner->id, 'nobody', 'gruntmaster-contest set' };
+subtest 'gm -c add ct' => sub {
+ plan tests => 4;
+ my $ct = contest_entry 'ct';
+ is $ct->{name}, 'My cool contest', 'contest name';
+ is $ct->{owner}, 'MGV', 'contest owner';
+ is $ct->{start}, 1388534400, 'contest start';
+ is $ct->{stop}, 1388534400 + 5 * 60 * 60, 'contest stop';
+};
-withdb { $db->contests->create({id => 'dummy', name => 'Dummy contest', owner => 'MGV', start => 0, stop => 1}) };
-my @list = sort `$perl gruntmaster-contest list`;
-chomp @list;
-my @list2 = withdb { map { $_->id } $db->contests->all };
-is_deeply \@list, [ sort @list2 ], 'gruntmaster-contest list';
+{
+ my $out = ta([qw/-c show ct/])->output;
+ like $out, qr/Name: My cool contest/, 'gm -c show ct contains Name'
+}
-system $perl, 'gruntmaster-contest', 'rm', 'dummy';
-withdb { ok !$db->contest('dummy'), 'gruntmaster-contest rm' };
+ta [qw/-c get ct owner/], 'MGV';
+ta [qw/-c set ct owner nobody/];
+ta [qw/-c get ct owner/], 'nobody';
+ta [qw/-c list/], join "\n", sort qw/fc rc pc ct/;
+ta [qw/-c rm ct/];
+ok !defined contest_entry('ct'), 'gm -c rm ct';
-open $pipe, "|$perl gruntmaster-problem add pb";
-print $pipe <<'';
+fakein <<EOF;
Test problem
-n
-ct
+y
+pc
Marius Gavrilescu
Smaranda Ciubotaru
MGV
b
-gruntmaster-problem
+gm
c
a
a
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';
- }
+EOF
+
+ta [qw/-p add pb/];
+
+subtest 'gruntmaster-problem add' => sub {
+ plan tests => 10;
+ my $pb = problem_entry 'pb';
+ ok $pb, 'problem exists';
+ is $pb->{name}, 'Test problem', 'name';
+ ok $pb->{private}, 'private';
+ is $pb->{author}, 'Marius Gavrilescu', 'author';
+ is $pb->{writer}, 'Smaranda Ciubotaru', 'statement writer';
+ is $pb->{owner}, 'MGV', 'owner';
+ is $pb->{level}, 'easy', 'level';
+ is $pb->{timeout}, 1, 'time limit';
+ is $pb->{olimit}, 100, 'output limit';
+ ok contest_has_problem('ct', 'pb'), 'is in contest';
};
-is `$perl gruntmaster-problem get pb author`, "Marius Gavrilescu\n", 'gruntmaster-problem get';
-system $perl, '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', value => 100, owner => 'MGV', statement => '...', testcnt => 1, timeout => 1}) };
-
-@list = sort `$perl gruntmaster-problem list`;
-chomp @list;
-@list2 = withdb { map { $_->id } $db->problems->all };
-is_deeply \@list, [ sort @list2 ], 'gruntmaster-problem list';
-
-system $perl, '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 `$perl gruntmaster-job get 1 format`, "CPP\n", 'gruntmaster-job get';
-system $perl, 'gruntmaster-job', 'set', 1, 'format', 'PERL';
-withdb { is $db->job(1)->format, 'PERL', 'gruntmaster-job set' };
+ta [qw/-p get pb author/], 'Marius Gavrilescu';
+ta [qw/-p set pb owner nobody/];
+ta [qw/-p get pb owner/], 'nobody';
+ta [qw/-p list/], join "\n", sort qw/arc pca rca fca prv pb/;
+ta [qw/-p rm pb/];
+ok !defined problem_entry ('pb'), 'gm -p rm pb';
+
+my $id = create_job extension => '.cpp', format => 'CPP', problem => 'arc', source => '...', owner => 'MGV';
+ok abs (time - user_entry('MGV')->{lastjob}) < 2, 'create_job - lastjob looks sane';
+ta [rerun => $id];
+is job_entry($id)->{result}, -2, "gm rerun $id";
+ta [qw/rm -j/, $id];
+
+sub terr {
+ my ($args, $err) = @_;
+ my $ret = test_app 'Gruntmaster::App' => $args;
+ like $ret->error, qr/$err/, join ' ', 'invalid:', gm => @$args;
+ $ret
+}
-system $perl, 'gruntmaster-job', 'rm', 1;
-withdb { ok !$db->job(1), 'gruntmaster-job rm' };
+terr [qw/add/], 'No table selected';
+terr [qw/-j add/], 'Don\'t know how to add to this table';
+terr [qw/-c add/], 'Wrong number of arguments';
+terr [qw/get/], 'No table selected';
+terr [qw/-c get/], 'Wrong number of arguments';
+terr [qw/list/], 'No table selected';
+terr [qw/-p rerun/], 'This command only works on jobs';
+terr [qw/-j rerun/], 'Wrong number of arguments';
+terr [qw/rm/], 'No table selected';
+terr [qw/-j rm/], 'Wrong number of arguments';
+terr [qw/set/], 'No table selected';
+terr [qw/-j set 1 owner/], 'Not enough arguments';
+terr [qw/-j set 1 owner x name/], 'The number of arguments must be odd';
+terr [qw/show/], 'No table selected';
+terr [qw/-c show/], 'Wrong number of arguments';