source: trunk/t/10Vote_DB.t @ 216

Last change on this file since 216 was 216, checked in by nanardon, 15 years ago
  • add more tests
  • Property svn:keywords set to Id Rev
File size: 5.7 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) * 4 # number of tested polls scenario
124    + $poll_test_count;        # poll test
125
126use_ok 'Vote', 'Vote';
127use_ok 'Vote::DB';
128$ENV{EPOLL_NO_COMMIT} = 1;
129isa_ok(my $vote = Vote::DB->new(Vote->config->{db}), 'Vote::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), 'Vote::DB::Poll');
139
140    foreach (keys %{ $tpoll->{poll} || {}}) {
141        $poll->param($_ => $tpoll->{poll}{$_});
142    }
143
144    $poll->addupd_voting($_) foreach (@{ $tpoll->{voting} || [] });
145
146    foreach (@{ $tpoll->{choices} || [] }) {
147        $poll->add_choice($_);
148    }
149
150    ok(eq_set(
151            [ map { $poll->choice($_)->info->{label} } $poll->choices ],
152            $tpoll->{choices} || []), "choices were added");
153
154    foreach my $name (keys %{ $tpoll->{ballot} || {} }) {
155        $poll->register_ballot(
156            $name, $tpoll->{ballot}{$name}{sbal}, $tpoll->{ballot}{$name}{fsbal}, 'localhost'
157        );
158    }
159   
160    isa_ok(my $results = $vote->results($pollid), 'Vote::DB::Poll');
161
162    $tpoll->{tests}->($results);
163
164    $vote->db->rollback; # really rollback
165}
166
Note: See TracBrowser for help on using the repository browser.