From 10a4003fe323fbd7116c99202254dea5d69cdc71 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Thu, 13 Aug 2015 15:15:27 +0300 Subject: [PATCH] Add a very basic test --- Makefile.PL | 1 - app.psgi | 5 +- lib/Plack/App/Gruntmaster/HTML.pm | 2 +- t/mech.t | 104 ++++++++++++------------------ 4 files changed, 43 insertions(+), 69 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 7455a41..2584deb 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -15,7 +15,6 @@ WriteMakefile( BUILD_REQUIRES => { qw/CSS::Minifier::XS 0 File::Slurp 0 - Test::MockTime 0 Test::More 0 Test::WWW::Mechanize::PSGI 0/, }, diff --git a/app.psgi b/app.psgi index 80ed1a2..a612540 100644 --- a/app.psgi +++ b/app.psgi @@ -23,13 +23,10 @@ CSP $csp =~ s/\n/; /gr; } -my $dbinit; - sub add_database { my $app = $_[0]; sub { - dbinit $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:' unless $dbinit; - $dbinit = 1; + dbinit $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:' unless db; $app->(@_) } } diff --git a/lib/Plack/App/Gruntmaster/HTML.pm b/lib/Plack/App/Gruntmaster/HTML.pm index ea2e99e..c629383 100644 --- a/lib/Plack/App/Gruntmaster/HTML.pm +++ b/lib/Plack/App/Gruntmaster/HTML.pm @@ -49,7 +49,7 @@ sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/) } sub HTML::Element::namedlink { my ($self, $id, $name) = @_; - $name = $id unless $name =~ /[[:graph:]]/; + $name = $id unless $name && $name =~ /[[:graph:]]/; $self = $self->find('a'); $self->edit_href(sub {s/id/$id/}); $self->replace_content($name); diff --git a/t/mech.t b/t/mech.t index 990de76..10e7248 100644 --- a/t/mech.t +++ b/t/mech.t @@ -2,77 +2,55 @@ use strict; use warnings; -use Test::MockTime ':all'; -use Test::More tests => 14; +use Test::More; use Test::WWW::Mechanize::PSGI; use Gruntmaster::Data; use JSON::MaybeXS qw/decode_json/; use Log::Log4perl qw/:easy/; -set_fixed_time 25; +BEGIN { + eval { + dbinit 'dbi:Pg:dbname=gmtest'; 1; + } or plan skip_all => 'Cannot connect to test database (gmtest).'. "Error: $@"; + plan tests => 22; +} +Log::Log4perl->easy_init($OFF); my $mech = Test::WWW::Mechanize::PSGI->new(app => do 'app.psgi'); -my $result; -my $content; -Log::Log4perl->easy_init($OFF); +$mech->get_ok('/'); +$mech->title_is('Gruntmaster 6000', 'title'); -sub get { - my ($url, $expect_fail) = @_; - my $param = $url =~ /[?]/ ? '&format=json' : '?format=json'; - $expect_fail ? $mech->get("$url$param") : $mech->get_ok("$url$param"); - $result = $mech->response->code; - $content = $result == 200 ? decode_json $mech->content(decoded_by_headers => 1) : ''; -} +$mech->get_ok('/pb/'); +$mech->title_is('Problems', 'title'); + +$mech->get_ok('/pb/arc'); +$mech->title_is('Problem in archive', 'title'); + +$mech->get_ok('/ct/'); +$mech->title_is('Contests', 'title'); + +$mech->get_ok('/ct/fc'); +$mech->title_is('Finished contest'); + +$mech->get_ok('/log/'); +$mech->title_is('Job log', 'title'); + +$mech->get_ok('/log/1'); +$mech->title_is('Job 1', 'title'); + +$mech->get_ok('/us/'); +$mech->title_is('Users', 'title'); + +$mech->get_ok('/us/MGV'); +# testdata.sql does not set name for users, therefore not checking title +#$mech->title_is('Marius Gavrilescu', 'title'); + +$mech->get_ok('/src/1.c'); + +$mech->get_ok('/st/fc'); +$mech->title_is('Standings', 'title'); -our $db = Gruntmaster::Data->connect('dbi:SQLite:dbname=:memory:'); -$db->deploy; - -$db->users->create({id => 'MGV', admin => 1}); -$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'}); - -my @pbjunk = ( - name => 'Problem', - generator => 'Undef', - runner => 'File', - judge => 'Absolute', - level => 'beginner', - value => 100, - owner => 'MGV', - statement => '...', - testcnt => 1, - timeout => 1 -); - -$db->problems->create({id => 'fca', private => 0, @pbjunk}); -$db->problems->create({id => 'rca', private => 0, @pbjunk}); -$db->problems->create({id => 'pca', private => 0, @pbjunk}); -$db->problems->create({id => 'arc', private => 0, @pbjunk}); -$db->problems->create({id => 'prv', private => 1, @pbjunk}); -$db->contest_problems->create({problem => "${_}a", contest => $_}) for qw/fc rc pc/; - -sub problem_list () { [sort map {$_->{id}} @{$content->{beginner}}] } -get '/pb/'; -is_deeply problem_list, [qw/arc fca/], '/pb/ has correct problems'; -get '/pb/?contest=pc', 1; -is $result, 401, '/pb/?contest=pc returns 401'; -get '/pb/?contest=rc'; -is_deeply problem_list, [qw/rca/], '/pb/?contest=rc has correct problems'; -get '/pb/?contest=fc'; -is_deeply problem_list, [qw/fca/], '/pb/?contest=fc has correct problems'; - -get '/us/'; -my ($mgv) = @{$content->{us}}; -is $mgv->{id}, 'MGV', 'first (and only) user is MGV'; -is $mgv->{admin}, 1, 'MGV is an admin'; - -get '/ct/'; -my $pc = $content->{pending}->[0]; -my $rc = $content->{running}->[0]; -my $fc = $content->{finished}->[0]; -is $pc->{id}, 'pc', 'pc is pending'; -is $rc->{id}, 'rc', 'rc is running'; -is $fc->{id}, 'fc', 'fc is running'; +$mech->get_ok('/ed/fc'); +$mech->title_is('Editorial of Finished contest', 'title'); -- 2.30.2