Add assert_cast_quickvote_succeeds/fails helper, and changed the fails helpers to...
[selectricity-live] / test / unit / selectricityservice_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'selectricity_service_controller'
3
4 class SelectricityServiceTest < Test::Unit::TestCase
5   def setup
6     @controller=SelectricityServiceController.new
7     @request=ActionController::TestRequest.new
8     @response   = ActionController::TestResponse.new
9   end
10
11   def test_list_quickvotes
12     result= invoke_delegated :vote, :list_quickvotes
13     assert_instance_of Array, result
14     assert_equal result.length, 0
15   end
16   def test_create_quickvote
17     election = ElectionStruct.new :name => "TestVote", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
18     assert_create_quickvote_succeeds election
19   end
20   def test_cast_quickvote
21     test_create_quickvote
22     election = invoke_delegated :vote, :get_quickvote, "TestVote"
23     casted_vote = election.candidate_ids.sort_by {rand} #Shuffles
24     assert_cast_quickvote_succeeds "TestVote", 42, [casted_vote]
25   end
26   def test_cast_mass_quickvote
27     test_create_quickvote
28     election = invoke_delegated :vote, :get_quickvote, "TestVote"
29     20.times do |t|
30       casted_vote = election.candidate_ids.sort_by {rand}
31       assert_cast_quickvote_succeeds "TestVote", t, [casted_vote]
32     end
33   end
34   def test_create_mass_quickvote
35     10.times do |t|
36       election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
37       assert_create_quickvote_succeeds election
38     end
39   end
40   def test_create_quickvote_bad_name
41     election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
42     assert_create_quickvote_fails election
43   end
44   def test_create_quickvote_nil
45     election =  ElectionStruct.new
46     assert_create_quickvote_fails election
47   end
48   def test_create_quickvote_name_nil
49     election = ElectionStruct.new :name => "", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
50     assert_create_quickvote_fails election
51   end
52   def test_create_quickvote_description_nil
53     election = ElectionStruct.new :name => "foobar", :description => nil, :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
54     assert_create_quickvote_fails election
55     
56   end
57   def test_create_quickvote_description_whitespace
58     election = ElectionStruct.new :name => "foobar", :description => "       ", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
59     assert_create_quickvote_fails election
60     election = ElectionStruct.new :name => "foobar", :description => "\t\t", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
61     assert_create_quickvote_fails election
62   end
63   def test_create_quickvote_candidates_nil
64     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
65     assert_create_quickvote_fails election
66   end
67   def test_create_quickvote_insufficient_candidates
68     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple"]
69     assert_create_quickvote_fails election
70   end
71   def test_create_quickvote_candidates_whitespace
72     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => [" ", "   ", "       ", "         "]
73     assert_create_quickvote_fails election
74   end
75   def test_create_quickvote_dupe_candidates
76     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Apple", "Apple"]
77     assert_create_quickvote_fails election
78
79     # Previous may pass coincidentally if a uniq! then a sizecheck reveals too few unique names
80     # We don't want this to happen. Dupe canidates should fail regardless of how many are left.
81
82     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Orange", "Orange", "Pineapple" ,  "Pineapple"]
83     assert_create_quickvote_fails election
84   end
85   def test_create_quickvote_candidates_nil_mixed
86     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", nil ]
87     assert_create_quickvote_fails election
88   end
89   def test_create_quickvote_description_xmlescape
90     # Will an embedded XML element bork the table?
91     election = ElectionStruct.new :name => "foobar", :description => "test </string>", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
92     assert_create_quickvote_succeeds election
93   end
94   def test_create_quickvote_unprintable_description
95     election =  ElectionStruct.new :name => "foobar", :description => "test \x01\x02\x03\x04\x05\x06\x07\x08", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
96     assert_create_quickvote_succeeds election
97   end
98   def test_quickvote_proper_results
99     election =  ElectionStruct.new :name => "favdev", :description => "Who is your favorite developer?", :candidate_names => ["mako", "jdong", "justin"]
100     assert_create_quickvote_succeeds election
101     reflection = invoke_delegated :vote, :get_quickvote, "favdev"
102     candidates = {}
103     reflection.candidate_names.each_with_index do |name, index|
104       candidates[name] =  reflection.candidate_ids[index]
105     end
106     25.times do |t|
107       vote = [candidates["jdong"], candidates["mako"], candidates["justin"]]
108       assert_cast_quickvote_succeeds "favdev", "1:#{t}", [vote]
109     end
110     5.times do |t|
111       vote = [candidates["mako"], candidates["justin"], candidates["jdong"]]
112       assert_cast_quickvote_succeeds "favdev", "2:#{t}", [vote]
113     end
114     10.times do |t|
115       vote = [candidates["justin"], candidates["mako"], candidates["jdong"]]
116       assert_cast_quickvote_succeeds "favdev", "3:#{t}", [vote]
117     end
118     results=invoke_delegated(:vote, :get_quickvote_results, "favdev")
119     assert_equal results.approval_winners, [candidates["mako"]]
120     assert_equal results.borda_winners, [candidates["jdong"]]
121     assert_equal results.plurality_winners, [candidates["jdong"]]
122     assert_equal results.condorcet_winners, [candidates["jdong"]]
123     assert_equal results.ssd_winners, [candidates["jdong"]]
124     assert_equal results.errors.length, 0
125   end
126   private
127   def assert_cast_quickvote_succeeds(shortname, id, vote)
128     old_votes = invoke_delegated :vote, :get_quickvote_votes, shortname
129     assert_nothing_raised do
130       invoke_delegated :vote, :cast_quickvote, shortname, id, vote
131     end
132     new_votes = invoke_delegated :vote, :get_quickvote_votes, shortname
133     assert_equal old_votes.length, new_votes.length-1
134     assert_not_nil(reflection = new_votes.find_all { |v| v.voter_session_id == "XMLRPC:#{id}" })
135     assert_equal reflection.length, 1
136     assert_equal reflection[0].vote, vote[0]
137   end
138   def assert_cast_quickvote_fails(shortname, id, vote)
139     assert_raises Test::Unit::AssertionFailedError do 
140       assert_cast_quickvote_succeeds(shortname, id, vote)
141     end
142   end
143   def assert_create_quickvote_succeeds(election)
144     # Checks if a created quickvote is identical when retrieved
145     old_len=invoke_delegated(:vote,:list_quickvotes).length
146     result=""
147     assert_nothing_raised do
148       result = invoke_delegated :vote, :create_quickvote, election
149     end
150     assert_equal result, ""
151     reflection = invoke_delegated :vote, :get_quickvote, election.name
152     assert_equal election.description, reflection.description
153     assert_equal 0, election.name.casecmp(reflection.name)
154     assert_equal election.candidate_names, reflection.candidate_names
155     assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len+1)
156   end
157   def assert_create_quickvote_fails(election)
158     assert_raises Test::Unit::AssertionFailedError do
159       assert_create_quickvote_succeeds election
160     end
161   end
162 end

Benjamin Mako Hill || Want to submit a patch?