Removed execution flag from non-exe files
[Fumo.git] / t / 10-queue-manip.t
blobeac865c6fd67a29dbc2501d9bb26641a06caf962
1 use Test::More tests => 31;
2 use Test::Exception;
4 use strict;
5 use warnings;
7 use DBI;
9 BEGIN {
10     use_ok 'Fumo::Queue';
11     use_ok 'Fumo::Queue::Run';
14 SKIP: {
15     skip('FUMO_TEST_DSN environment variable not set', 17)
16         unless $ENV{FUMO_TEST_DSN};
18     # just bail if we can't connect
19     my $dbh = DBI->connect($ENV{FUMO_TEST_DSN})
20         or BAIL_OUT(DBI->errstr);
22     # create the high level queue
23     my $queue = Fumo::Queue->new($dbh);
24     isa_ok($queue, 'Fumo::Queue');
26     # remove all runs
27     lives_ok { $queue->empty() } 'Delete all runs from the queue';
28     cmp_ok $queue->count(), '==', 0, 'Queue count is 0';
30     # create a new run
31     my $run = Fumo::Queue::Run->new({
32         project  => 'Project1',
33         branch   => 'Branch1',
34         revision => 'Revision1'
35     });
36     isa_ok($run, 'Fumo::Queue::Run');
38     # add to the queue
39     lives_ok { $queue->put_run($run) } 'Adding new run to queue';
41     # so now there should be one run
42     cmp_ok $queue->count(), '==', 1, 'Queue count is 1';
44     $run = undef;
46     # try and get it back again
47     lives_ok { $run = $queue->get_run('Project1', 'Branch1', 'Revision1') }
48         'Getting an existing branch lives';
49     isa_ok($run, 'Fumo::Queue::Run');
51     # does the run contain what it should?
52     {
53         my %hopefor = (
54             project  => 'Project1',
55             branch   => 'Branch1',
56             revision => 'Revision1',
57             weight   => 0,
58         );
60         for my $field (keys %hopefor) {
61             no strict 'refs';
63             cmp_ok($run->$field, 'eq', $hopefor{$field},
64                    "'$field' is corrent for run ($hopefor{$field})");
65         }
66     }
68     # try updating the run
69     {
70         $run->weight(110);
72         # add to the queue
73         lives_ok { $queue->put_run($run) } 'Adding new run to queue lives';
75         # this will do another DB lookup
76         my $copy_of_run = $queue->get_run('Project1', 'Branch1', 'Revision1');
77         cmp_ok $copy_of_run->weight(), '==', 110,
78             'New weight is 110';
79     }
81     # try and add the same run (it will actually do an update)
82     {
83         lives_ok { $queue->put_run($run) }
84             'Adding identical run to queue lives';
85     }
87     # delete the run
88     {
89         lives_ok { $queue->delete($run) } 'Deleting a run lives'; 
90         cmp_ok $queue->count(), '==', 0, 'Queue count is 0 again';       
91     }
93     # loop all runs
94     {
95         for (1 .. 5) {
96             lives_ok {
97                 $queue->put_run(Fumo::Queue::Run->new({
98                     project  => "project$_",
99                     branch   => "branch1$_",
100                     revision => "revision$_"
101                 }))
102             } "Adding new run ($_) to queue";
104             # count the entries
105             my $ctr = 0;
106             while (my $incr = $queue->all_runs()) {
107                 $ctr++;
108             }
109             cmp_ok $ctr, '==', $_, "all_runs returns $_ results";
110         }
111     }
113     # delete the runs
114     {
115         lives_ok { $queue->delete($_) while $_ = $queue->all_runs() }
116             'Deleting all runs with all_runs() lives ok';
117         cmp_ok $queue->count(), '==', 0, 'Queue count is 0 again';       
118     }