Merged from Mako.
[selectricity] / 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
17   def test_create_quickvote
18     election = ElectionStruct.new :name => "TestVote", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
19     assert_create_quickvote_succeeds election
20   end
21
22   def test_cast_quickvote
23     test_create_quickvote
24     election = invoke_delegated :vote, :get_quickvote, "TestVote"
25     casted_vote = election.candidate_ids.sort_by {rand} #Shuffles
26     assert_cast_quickvote_succeeds "TestVote", 42, [casted_vote]
27   end
28
29   def test_cast_nil_quickvote
30     assert_cast_quickvote_fails nil, nil, nil
31     assert_cast_quickvote_fails "foo", nil, nil
32     assert_cast_quickvote_fails "foo",33, []
33     test_create_quickvote
34     assert_cast_quickvote_fails "TestVote",42,nil
35     assert_cast_quickvote_fails "TestVote",nil,[]
36   end
37
38   def test_cast_malformed_votelist
39     test_create_quickvote
40     election = invoke_delegated :vote, :get_quickvote, "TestVote"
41     assert_cast_quickvote_fails "TestVote", 11, [election.candidate_ids[0]]
42     assert_cast_quickvote_fails "TestVote", 11, [1,2]
43     assert_cast_quickvote_fails "TestVote", 11, [election.candidate_ids[0],election.candidate_ids[0], election.candidate_ids[1], election.candidate_ids[1], election.candidate_ids[2]]
44   end
45
46   def test_get_nonexistent_quickvote
47     assert_raises ArgumentError do
48       qv = invoke_delegated :vote, :get_quickvote, "asdfasdfasdf"
49     end
50   end
51
52   def test_get_voters_nonexistent_quickvote
53     assert_raises(ArgumentError) {invoke_delegated :vote, :get_quickvote_votes, "asdfasdf"}
54   end
55
56   def test_get_candidate_map_nonexistent_quickvote
57     assert_raises(ArgumentError) { invoke_delegated :vote, :get_quickvote_candidate_map, "asdfasdf"}
58   end
59
60   def test_cast_mass_quickvote
61     test_create_quickvote
62     election = invoke_delegated :vote, :get_quickvote, "TestVote"
63     20.times do |t|
64       casted_vote = election.candidate_ids.sort_by {rand}
65       assert_cast_quickvote_succeeds "TestVote", t, [casted_vote]
66     end
67   end
68
69   def test_cast_quickvote_nonexistent
70     assert_cast_quickvote_fails "ASDFJOFASF", "me", [1,2,3]
71   end
72
73   def test_cast_quickvote_nonexistent_candidates
74     test_create_quickvote
75     election = invoke_delegated :vote, :get_quickvote, "TestVote"
76     assert_cast_quickvote_fails "TestVote", 42, [123,342314,5342,1,1,2]
77   end
78
79   def test_create_mass_quickvote
80     10.times do |t|
81       election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
82       assert_create_quickvote_succeeds election
83     end
84   end
85
86   def test_create_quickvote_bad_name
87     election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
88     assert_create_quickvote_fails election
89   end
90
91   def test_create_quickvote_nil
92     election =  ElectionStruct.new
93     assert_create_quickvote_fails election
94   end
95
96   def test_create_quickvote_name_nil
97     election = ElectionStruct.new :name => "", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
98     assert_create_quickvote_fails election
99   end
100
101   def test_create_quickvote_description_nil
102     election = ElectionStruct.new :name => "foobar", :description => nil, :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
103     assert_create_quickvote_fails election
104     
105   end
106   
107   def test_create_quickvote_description_whitespace
108     election = ElectionStruct.new :name => "foobar", :description => "       ", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
109     assert_create_quickvote_fails election
110     election = ElectionStruct.new :name => "foobar", :description => "\t\t", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
111     assert_create_quickvote_fails election
112   end
113   
114   def test_create_quickvote_candidates_nil
115     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
116     assert_create_quickvote_fails election
117   end
118   def test_create_quickvote_insufficient_candidates
119     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple"]
120     assert_create_quickvote_fails election
121   end
122   def test_create_quickvote_candidates_whitespace
123     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => [" ", "   ", "       ", "         "]
124     assert_create_quickvote_fails election
125   end
126   def test_create_quickvote_dupe_candidates
127     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Apple", "Apple"]
128     assert_create_quickvote_fails election
129
130     # Previous may pass coincidentally if a uniq! then a sizecheck reveals too few unique names
131     # We don't want this to happen. Dupe canidates should fail regardless of how many are left.
132
133     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Orange", "Orange", "Pineapple" ,  "Pineapple"]
134     assert_create_quickvote_fails election
135   end
136   def test_create_quickvote_candidates_nil_mixed
137     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", nil ]
138     assert_create_quickvote_fails election
139   end
140   def test_create_quickvote_description_xmlescape
141     # Will an embedded XML element bork the table?
142     election = ElectionStruct.new :name => "foobar", :description => "test </string>", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
143     assert_create_quickvote_succeeds election
144   end
145   def test_create_quickvote_unprintable_description
146     election =  ElectionStruct.new :name => "foobar", :description => "test \x01\x02\x03\x04\x05\x06\x07\x08", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
147     assert_create_quickvote_succeeds election
148   end
149   def test_quickvote_proper_results
150     election =  ElectionStruct.new :name => "favdev", :description => "Who is your favorite developer?", :candidate_names => ["mako", "jdong", "justin"]
151     assert_create_quickvote_succeeds election
152     reflection = invoke_delegated :vote, :get_quickvote, "favdev"
153     candidates = {}
154     reflection.candidate_names.each_with_index do |name, index|
155       candidates[name] =  reflection.candidate_ids[index]
156     end
157     25.times do |t|
158       vote = [candidates["jdong"], candidates["mako"], candidates["justin"]]
159       assert_cast_quickvote_succeeds "favdev", "1:#{t}", [vote]
160     end
161     5.times do |t|
162       vote = [candidates["mako"], candidates["justin"], candidates["jdong"]]
163       assert_cast_quickvote_succeeds "favdev", "2:#{t}", [vote]
164     end
165     10.times do |t|
166       vote = [candidates["justin"], candidates["mako"], candidates["jdong"]]
167       assert_cast_quickvote_succeeds "favdev", "3:#{t}", [vote]
168     end
169     results=nil
170     assert_nothing_raised {results=invoke_delegated(:vote, :get_quickvote_results, "favdev")}
171     assert_equal results.approval_winners, [candidates["mako"]]
172     assert_equal results.borda_winners, [candidates["jdong"]]
173     assert_equal results.plurality_winners, [candidates["jdong"]]
174     assert_equal results.condorcet_winners, [candidates["jdong"]]
175     assert_equal results.ssd_winners, [candidates["jdong"]]
176   end
177   private
178
179   def assert_cast_quickvote_succeeds(shortname, id, vote)
180     assert_nothing_raised do
181       old_votes = invoke_delegated(:vote, :get_quickvote_votes, shortname)
182       invoke_delegated(:vote, :cast_quickvote, shortname, id, vote)
183
184       new_votes = invoke_delegated(:vote, :get_quickvote_votes, shortname)
185       assert_equal(old_votes.length, new_votes.length - 1)
186
187       reflection = new_votes.find_all { |v| v.voter_session_id == "XMLRPC:#{id}" }
188       assert_not_nil(reflection)
189       assert_equal(reflection.length, 1)
190       assert_equal(reflection[0].vote, vote[0])
191     end
192   end
193
194   def assert_cast_quickvote_fails(shortname, id, vote)
195     assert_raise Test::Unit::AssertionFailedError do
196       assert_cast_quickvote_succeeds(shortname, id, vote)
197      end
198   end
199
200   def assert_create_quickvote_succeeds(election)
201     # checks if a created quickvote is identical when retrieved
202     assert_nothing_raised do
203       old_len = invoke_delegated(:vote, :list_quickvotes).length
204       result = invoke_delegated(:vote, :create_quickvote, election)
205       assert_equal(result, "")
206
207       reflection = invoke_delegated(:vote, :get_quickvote, election.name)
208       assert_equal(election.description, reflection.description)
209       assert_equal(0, election.name.casecmp(reflection.name))
210       assert_equal(election.candidate_names, reflection.candidate_names)
211       assert_equal(invoke_delegated(:vote, :list_quickvotes).length, old_len+1)
212     end
213   end
214   
215   def assert_create_quickvote_fails(election)
216     assert_raise Test::Unit::AssertionFailedError do
217       assert_create_quickvote_succeeds(election)
218     end
219   end
220 end

Benjamin Mako Hill || Want to submit a patch?