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

Benjamin Mako Hill || Want to submit a patch?