source: trunk/t/10Epoll_DB.t @ 308

Last change on this file since 308 was 269, checked in by nanardon, 14 years ago
  • allow to delete ballot after poll (then keeping only results in DB)
  • Property svn:keywords set to Id Rev
File size: 5.9 KB
Line 
1use strict;
2use warnings;
3use Test::More;
4
5# $Id$
6
7# Tpoll Structure:
8#    {
9#        poll => { var => $val },
10#        choices => [ qw() ],
11#        voting => [ qw(mail) ],
12#        ballot => {
13#        # signing from voting list
14#            signing => { sbal => [], fsbal => [] } # list of items
15#        },
16#        map => { from => to },
17#        results => { var => $val }, # wanted results
18#    }
19my $test_polls = [
20    { # an empty poll
21        poll => { label => 'empty poll' },
22        choices => [ qw(ch1 ch2) ],
23        test_count => 4,
24        tests => sub {
25            my ($r) = @_;
26            is($r->absolute_majority, 1, "Can get absolute_majority");
27            is($r->ballot_count, 0, "can get ballot count");
28            is($r->voting_count, 0, "can get voting count");
29            is($r->signing_count, 0, "can get signing count");
30        }
31    },
32    {
33        poll => { label => 'some vals' },
34        choices => [ qw(ch1 ch2) ],
35        voting => [ qw(a b c d e f) ],
36        ballot => {
37            a => { sbal => [ 'ch1' ] },
38            b => { sbal => [ 'ch1', 'ch10' ] },
39            d => { sbal => [ 'ch1', 'ch2' ] },
40            f => {},
41        },
42        test_count => 7,
43        tests => sub {
44            my ($r) = @_;
45            is($r->absolute_majority, 2, "Can get absolute_majority");
46            is($r->ballot_count, 4, "can get ballot count");
47            is($r->ballot_count_nonull, 3, "can get ballot count");
48            is($r->voting_count, 6, "can get voting count");
49            is($r->signing_count, 4, "can get signing count");
50            my $res = $r->results;
51            is($res->[0]->{value}, 'ch1', "winner is ch1");
52            is($res->[0]->{count}, 3, "winner has 3 voices");
53        }
54    },
55    {
56        poll => { label => 'some vals with mapped values' },
57        choices => [ qw(ch1 ch2 ch3) ],
58        voting => [ qw(a b c d e f) ],
59        ballot => {
60            a => { sbal => [ 'ch1' ] },
61            b => { sbal => [ 'ch1', 'ch10' ] },
62            d => { sbal => [ 'ch1', 'ch2', 'ch4' ] },
63            f => {},
64        },
65        test_count => 14,
66        tests => sub {
67            my ($r) = @_;
68            $r->map_value('ch10', 'ch1');
69            is($r->absolute_majority, 2, "Can get absolute_majority");
70            is($r->ballot_count, 4, "can get ballot count");
71            is($r->not_empty_ballot_count, 3, "can get ballot count");
72            is($r->empty_ballot_count, 1, "can get empty ballot count");
73            is($r->voting_count, 6, "can get voting count");
74            is($r->signing_count, 4, "can get signing count");
75            my $res = $r->results;
76            is($res->[0]->{value}, 'ch1', "winner is ch1");
77            is($res->[0]->{count}, 3, "winner has 3 voices");
78            ok($res->[0]->{abs_maj}, "winner has absolute majority");
79            ok($res->[0]->{elected}, "winner is set as elected");
80            is(@$res, 4, "count of winners");
81            is($res->[3]->{count}, 0, "last has 0 voices");
82            ok(!$res->[3]->{abs_maj}, "last has not absolute majority");
83            ok(!$res->[3]->{elected}, "last is not set as elected");
84        }
85    },
86    {
87        poll => {
88            label => 'empty ballot count',
89            empty_ballot_has_voice => 1,
90        },
91        choices => [ qw(ch1 ch2 ch3) ],
92        voting => [ qw(a b c d e f) ],
93        ballot => {
94            a => { sbal => [ 'ch1' ] },
95            b => { sbal => [ 'ch1', 'ch2' ] },
96            d => { sbal => [ 'ch1', 'ch2' ] },
97            e => {},
98            f => {},
99        },
100        test_count => 10,
101        tests => sub {
102            my ($r) = @_;
103            is($r->absolute_majority, 3, "Can get absolute_majority");
104            is($r->ballot_count, 5, "can get ballot count");
105            is($r->not_empty_ballot_count, 3, "can get ballot count");
106            is($r->empty_ballot_count, 2, "can get empty ballot count");
107            is($r->voting_count, 6, "can get voting count");
108            is($r->signing_count, 5, "can get signing count");
109            my $res = $r->results;
110            is($res->[0]->{value}, 'ch1', "winner is ch1");
111            is($res->[0]->{count}, 3, "winner has 3 voices");
112            is(@$res, 4, "count of winners");
113            is($res->[3]->{count}, 0, "last has 0 voices");
114        }
115    },
116];
117
118my $poll_test_count = 0;
119$poll_test_count += ($_->{test_count} || 0) foreach(@$test_polls);
120
121plan tests =>
122    3                          # Fixed test
123    + scalar(@$test_polls) * 7 # number of tested polls scenario
124    + $poll_test_count * 2;    # poll test
125
126use_ok 'Epoll', 'Epoll';
127use_ok 'Epoll::DB';
128$ENV{EPOLL_NO_COMMIT} = 1;
129isa_ok(my $vote = Epoll::DB->new(Epoll->config->{db}), 'Epoll::DB');
130
131my $count = 0;
132foreach my $tpoll (@$test_polls) {
133    $count++;
134    diag("Testing vote #$count: " . ($tpoll->{poll}{label} || 'No name'));
135    ok(my $pollid = $vote->create_poll('test@', "Vote test $count", "password"),
136        "can create a new poll");
137
138    isa_ok(my $poll = $vote->poll($pollid), 'Epoll::DB::Poll');
139
140    ok(my $puid = $poll->uid, "can get poll uid");
141    is($vote->poll_id_from_uid($puid), $pollid, "can get id from uid");
142
143    foreach (keys %{ $tpoll->{poll} || {}}) {
144        $poll->param($_ => $tpoll->{poll}{$_});
145    }
146
147    $poll->addupd_voting($_) foreach (@{ $tpoll->{voting} || [] });
148
149    foreach (@{ $tpoll->{choices} || [] }) {
150        $poll->add_choice($_);
151    }
152
153    ok(eq_set(
154            [ map { $poll->choice($_)->info->{label} } $poll->choices ],
155            $tpoll->{choices} || []), "choices were added");
156
157    foreach my $name (keys %{ $tpoll->{ballot} || {} }) {
158        $poll->register_ballot(
159            $name, $tpoll->{ballot}{$name}{sbal}, 'localhost'
160        );
161    }
162   
163    isa_ok(my $results = $vote->results($pollid), 'Epoll::DB::Poll');
164
165    $tpoll->{tests}->($results);
166
167    ok($results->delete_ballots, "Can switch to static results");
168
169    $tpoll->{tests}->($results);
170
171    $vote->db->rollback; # really rollback
172}
173
Note: See TracBrowser for help on using the repository browser.