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

Last change on this file since 214 was 214, checked in by nanardon, 15 years ago
  • more results function, support empty ballots in voices count
  • Property svn:keywords set to Id Rev
File size: 5.4 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_nonull;
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 => 10,
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            is(@$res, 4, "count of winners");
79            is($res->[3]->{count}, 0, "last has 0 voices");
80        }
81    },
82    {
83        poll => {
84            label => 'empty ballot count',
85            empty_ballot_has_voice => 1,
86        },
87        choices => [ qw(ch1 ch2 ch3) ],
88        voting => [ qw(a b c d e f) ],
89        ballot => {
90            a => { sbal => [ 'ch1' ] },
91            b => { sbal => [ 'ch1', 'ch2' ] },
92            d => { sbal => [ 'ch1', 'ch2' ] },
93            e => {},
94            f => {},
95        },
96        test_count => 10,
97        tests => sub {
98            my ($r) = @_;
99            is($r->absolute_majority, 3, "Can get absolute_majority");
100            is($r->ballot_count, 5, "can get ballot count");
101            is($r->not_empty_ballot_count, 3, "can get ballot count");
102            is($r->empty_ballot_count, 2, "can get empty ballot count");
103            is($r->voting_count, 6, "can get voting count");
104            is($r->signing_count, 5, "can get signing count");
105            my $res = $r->results;
106            is($res->[0]->{value}, 'ch1', "winner is ch1");
107            is($res->[0]->{count}, 3, "winner has 3 voices");
108            is(@$res, 4, "count of winners");
109            is($res->[3]->{count}, 0, "last has 0 voices");
110        }
111    },
112];
113
114my $poll_test_count = 0;
115$poll_test_count += ($_->{test_count} || 0) foreach(@$test_polls);
116
117plan tests =>
118    3                          # Fixed test
119    + scalar(@$test_polls) * 4 # number of tested polls scenario
120    + $poll_test_count;        # poll test
121
122use_ok 'Vote', 'Vote';
123use_ok 'Vote::DB';
124$ENV{EPOLL_NO_COMMIT} = 1;
125isa_ok(my $vote = Vote::DB->new(Vote->config->{db}), 'Vote::DB');
126
127my $count = 0;
128foreach my $tpoll (@$test_polls) {
129    $count++;
130    diag("Testing vote #$count: " . ($tpoll->{poll}{label} || 'No name'));
131    ok(my $pollid = $vote->create_poll('test@', "Vote test $count", "password"),
132        "can create a new poll");
133
134    isa_ok(my $poll = $vote->poll($pollid), 'Vote::DB::Poll');
135
136    foreach (keys %{ $tpoll->{poll} || {}}) {
137        $poll->param($_ => $tpoll->{poll}{$_});
138    }
139
140    $poll->addupd_voting($_) foreach (@{ $tpoll->{voting} || [] });
141
142    foreach (@{ $tpoll->{choices} || [] }) {
143        $poll->add_choice($_);
144    }
145
146    ok(eq_set(
147            [ map { $poll->choice($_)->info->{label} } $poll->choices ],
148            $tpoll->{choices} || []), "choices were added");
149
150    foreach my $name (keys %{ $tpoll->{ballot} || {} }) {
151        $poll->register_ballot(
152            $name, $tpoll->{ballot}{$name}{sbal}, $tpoll->{ballot}{$name}{fsbal}, 'localhost'
153        );
154    }
155   
156    isa_ok(my $results = $vote->results($pollid), 'Vote::DB::Poll');
157
158    $tpoll->{tests}->($results);
159
160    $vote->db->rollback; # really rollback
161}
162
Note: See TracBrowser for help on using the repository browser.