1 | use strict; |
---|
2 | use warnings; |
---|
3 | use Test::More; |
---|
4 | |
---|
5 | # $Id$ |
---|
6 | |
---|
7 | my $test_polls = [ |
---|
8 | # Structure: |
---|
9 | # { |
---|
10 | # poll => { var => $val }, |
---|
11 | # choices => [ qw() ], |
---|
12 | # voting => [ qw(mail) ], |
---|
13 | # ballot => { |
---|
14 | # # signing from voting list |
---|
15 | # signing => { sbal => [], fsbal => [] } # list of items |
---|
16 | # }, |
---|
17 | # map => { from => to }, |
---|
18 | # results => { var => $val }, # wanted results |
---|
19 | # } |
---|
20 | { # an empty poll |
---|
21 | poll => { label => 'empty poll' }, |
---|
22 | choices => [ qw(ch1 ch2) ], |
---|
23 | results => { |
---|
24 | ballot_count => 0, |
---|
25 | absolute_majority => 1, |
---|
26 | } |
---|
27 | }, |
---|
28 | ]; |
---|
29 | |
---|
30 | plan tests => |
---|
31 | 3 # Fixed test |
---|
32 | + scalar(@$test_polls) * # number of tested polls scenario |
---|
33 | 6; # per poll number of tests |
---|
34 | |
---|
35 | use_ok 'Vote', 'Vote'; |
---|
36 | use_ok 'Vote::DB'; |
---|
37 | |
---|
38 | $ENV{EPOLL_NO_COMMIT} = 1; |
---|
39 | |
---|
40 | isa_ok(my $vote = Vote::DB->new(Vote->config->{db}), 'Vote::DB'); |
---|
41 | |
---|
42 | my $count = 0; |
---|
43 | foreach my $tpoll (@$test_polls) { |
---|
44 | $count++; |
---|
45 | diag("Testing vote #$count: " . ($tpoll->{poll}{label} || 'No name')); |
---|
46 | ok(my $pollid = $vote->create_poll('test@', "Vote test $count", "password"), |
---|
47 | "can create a new poll"); |
---|
48 | |
---|
49 | isa_ok(my $poll = $vote->poll($pollid), 'Vote::DB::Poll'); |
---|
50 | |
---|
51 | foreach (keys %{ $tpoll->{poll} || {}}) { |
---|
52 | $poll->param($_ => $tpoll->{poll}{$_}); |
---|
53 | } |
---|
54 | |
---|
55 | $poll->addupd_voting($_) foreach (@{ $tpoll->{voting} || [] }); |
---|
56 | |
---|
57 | foreach (@{ $tpoll->{choices} || [] }) { |
---|
58 | $poll->add_choice($_); |
---|
59 | } |
---|
60 | |
---|
61 | ok(eq_set( |
---|
62 | [ map { $poll->choice($_)->info->{label} } $poll->choices ], |
---|
63 | $tpoll->{choices} || []), "choices were added"); |
---|
64 | |
---|
65 | foreach my $name (keys %{ $tpoll->{ballot} || {} }) { |
---|
66 | $poll->register_ballot( |
---|
67 | $name, $tpoll->{ballot}{$name}{sbal}, $tpoll->{ballot}{$name}{sbal}, "localtest" |
---|
68 | ); |
---|
69 | } |
---|
70 | |
---|
71 | isa_ok(my $results = $vote->results($pollid), 'Vote::DB::Poll'); |
---|
72 | |
---|
73 | is($results->ballot_count, $tpoll->{results}{ballot_count} || 0, |
---|
74 | "can get ballot count"); |
---|
75 | is($results->absolute_majority, $tpoll->{results}{absolute_majority}, |
---|
76 | "can get absolute majority"); |
---|
77 | |
---|
78 | my $res = $results->results_count; |
---|
79 | |
---|
80 | $vote->rollback; |
---|
81 | } |
---|