Set up test database automatically when testing
[gruntmaster-data.git] / t / Gruntmaster-Data.t
CommitLineData
4623c9f2 1#!/usr/bin/perl
1ce4e27c 2use v5.14;
4623c9f2 3use warnings;
bbf8209c 4
4623c9f2
MG
5use Gruntmaster::Data;
6use Test::Deep;
7use Test::More;
bbf8209c 8
4623c9f2
MG
9BEGIN {
10 eval {
11 Gruntmaster::Data::init 'dbi:Pg:dbname=gmtest'; 1;
1ea1adc8 12 } or plan skip_all => 'Cannot connect to test database. Create it by running createdb gmtest before running this test. '. "Error: $@";
4623c9f2
MG
13 plan tests => 37;
14}
15
1ea1adc8
MG
16note 'Setting up test database';
17$ENV{PGOPTIONS} = '-c client_min_messages=WARNING';
18system 'psql', 'gmtest', '-qf', 'db.sql';
19system 'psql', 'gmtest', '-qf', 'testdata.sql';
20
4623c9f2
MG
21note 'Running update_status';
22update_status;
23
24my $x = user_list->{us};
25is @$x, 2, 'user_list has two elements';
26is_deeply $x->[0], {id => 'nobody', admin => 0, name => undef, town => undef, university => undef, country => undef, level => undef, lastjob => undef, contests => 1, solved => 2, attempted => 0}, 'user_list first element is correct';
27is $x->[1]{admin}, 1, 'user_list second user is admin';
28
29$x = user_entry 'nobody';
30cmp_bag $x->{problems}, [
31 {problem => 'arc', problem_name => 'Problem in archive', solved => bool 1},
32 {problem => 'fca', problem_name => 'FC problem A', solved => bool 1},
33], 'user_entry problems';
34
35is_deeply $x->{contests}, [
36 {contest => 'fc', contest_name => 'Finished contest', rank => 2, score => 40},
37], 'user_entry contests';
38
39sub pbids { [map { $_->{id} } @{$x->{beginner}}] }
40
41$x = problem_list;
42cmp_bag pbids, [qw/arc fca/], 'problem_list';
43
44$x = problem_list private => 1;
45cmp_bag pbids, [qw/arc fca rca pca prv/], 'problem_list private => 1';
46
47$x = problem_list contest => 'rc';
48cmp_bag pbids, [qw/rca/], q/problem_list contest => 'rc'/;
49
50$x = problem_list contest => 'rc', solution => 1;
51ok exists $x->{beginner}[0]{solution}, q/problem_list contest => 'rc', solution => 1 has solution/;
52
53$x = problem_list owner => 'nobody';
54cmp_bag pbids, [], q/problem_list owner => 'nobody'/;
55
56$x = problem_entry 'arc';
57cmp_bag $x->{limits}, [{format => 'C', timeout => 0.1}, {format => 'CPP', timeout => 0.1}], 'problem_entry limits';
58is $x->{solution}, 'Sample Text', 'problem_entry has solution';
59
60$x = problem_entry 'rca', 'rc';
61ok !exists $x->{solution}, 'problem_entry during contest does not have solution';
62ok exists $x->{contest_start}, 'problem_entry during contest has contest_start ';
63
64$x = contest_list;
65is $x->{finished}[0]{id}, 'fc', 'contest_list fc is finished';
66is $x->{running}[0]{id}, 'rc', 'contest_list rc is running';
67is $x->{pending}[0]{id}, 'pc', 'contest_list pc is pending';
68
69$x = contest_entry 'fc';
70cmp_deeply $x, {id => 'fc', name => 'Finished contest', start => ignore, stop => ignore, owner => 'MGV', owner_name => undef, finished => bool (1), started => bool (1), description => undef}, 'contest_entry fc';
71
72$x = contest_full 'fc';
73ok exists $x->{editorial}, 'contest_full fc has editorial';
74
75ok contest_has_problem('rc', 'rca'), 'contest rc has problem rca';
76ok contest_has_problem('rc', 'arc'), 'contest rc does not have problem arc';
77
78sub jobids { [ map { $_->{id} } @{$x->{log}} ] }
79
80$x = job_list;
81cmp_bag jobids, [1..5], 'job_list';
82is $x->{current_page}, 1, 'current page is 1';
83is $x->{last_page}, 1, 'last page is 1';
84ok !exists $x->{previous_page}, 'there is no previous page';
85ok !exists $x->{next_page}, 'there is no next page';
86
87$x = job_list private => 1;
88cmp_bag jobids, [1..7], 'job_list private => 1';
89
90$x = job_list contest => 'fc';
91cmp_bag jobids, [1..3], 'job_list contest => fc';
92
93$x = job_list owner => 'MGV';
94cmp_bag jobids, [1], 'job_ids owner => MGV';
95
96$x = job_list problem => 'fca';
97cmp_bag jobids, [1..4], 'job_ids problem => fca';
98
99$x = job_list problem => 'fca', result => 1;
100cmp_bag jobids, [2], 'job_ids problem => fca, result => 1';
101
102$x = job_entry 1;
103is $x->{size}, 21, 'job_entry size';
104ok !exists $x->{source}, 'job_entry does not have source';
105is_deeply $x->{results}, [], 'job_entry results';
106
107$x = job_entry 7;
108ok !defined $x->{result}, 'job_entry 7 has NULL result';
109
110$x = job_full 1;
111ok exists $x->{source}, 'job_full has source';
112
113$x = standings 'fc';
114is_deeply $x, {
115 problems => [[fca => 'FC problem A']],
116 st => [
117 {rank => 1, user => 'MGV', user_name => undef, score => 50, scores => [50]},
118 {rank => 2, user => 'nobody', user_name => undef, score => 40, scores => [40]},
119 ]
120}, 'standings fc';
This page took 0.018971 seconds and 4 git commands to generate.