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

Benjamin Mako Hill || Want to submit a patch?