1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'selectricity_service_controller'
4 class SelectricityServiceTest < Test::Unit::TestCase
6 @controller=SelectricityServiceController.new
7 @request=ActionController::TestRequest.new
8 @response = ActionController::TestResponse.new
11 def test_list_quickvotes
12 result = invoke_delegated :vote, :list_quickvotes
13 assert_instance_of Array, result
14 assert result.length > 0
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
25 def test_cast_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]
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, []
37 assert_cast_quickvote_fails "TestVote", 42,nil
38 assert_cast_quickvote_fails "TestVote", nil, []
41 def test_cast_malformed_votelist
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]]
53 def test_get_nonexistent_quickvote
54 assert_raises ArgumentError do
55 qv = invoke_delegated :vote, :get_quickvote, "asdfasdfasdf"
59 def test_get_voters_nonexistent_quickvote
60 assert_raises(ArgumentError) { invoke_delegated(:vote,
61 :get_quickvote_votes, "asdfasdf") }
64 def test_get_candidate_map_nonexistent_quickvote
65 assert_raises(ArgumentError) { invoke_delegated(:vote,
66 :get_quickvote_candidate_map,
70 def test_cast_mass_quickvote
72 election = invoke_delegated(:vote, :get_quickvote, "TestVote")
74 casted_vote = election.candidate_ids.sort_by { rand }
75 assert_cast_quickvote_succeeds "TestVote", t, [casted_vote]
79 def test_cast_quickvote_nonexistent
80 assert_cast_quickvote_fails "ASDFJOFASF", "me", [1, 2, 3]
83 def test_cast_quickvote_nonexistent_candidates
85 election = invoke_delegated :vote, :get_quickvote, "TestVote"
86 assert_cast_quickvote_fails "TestVote", 42, [123, 342314, 5342, 1, 1, 2]
89 def test_create_mass_quickvote
91 assert_create_quickvote_succeeds ElectionStruct.new({
93 :description => "Test Vote",
94 :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]})
98 def test_create_quickvote_bad_name
99 assert_create_quickvote_fails ElectionStruct.new({
100 :name => "invalid space",
101 :description => "Test Vote",
102 :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]})
106 def test_create_quickvote_nil
107 assert_create_quickvote_fails ElectionStruct.new()
110 def test_create_quickvote_name_nil
111 assert_create_quickvote_fails ElectionStruct.new({
113 :description => "Test Vote",
114 :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]})
117 def test_create_quickvote_description_nil
118 assert_create_quickvote_fails ElectionStruct.new({
121 :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]})
124 def test_create_quickvote_description_whitespace
125 assert_create_quickvote_fails ElectionStruct.new({
128 :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]})
130 assert_create_quickvote_fails ElectionStruct.new({
132 :description => "\t\t",
133 :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]})
136 def test_create_quickvote_candidates_nil
137 assert_create_quickvote_fails ElectionStruct.new({
139 :description => "valid",
140 :candidate_names => nil })
143 def test_create_quickvote_insufficient_candidates
144 assert_create_quickvote_fails ElectionStruct.new({
146 :description => "valid",
147 :candidate_names => ["Apple"] })
150 def test_create_quickvote_candidates_whitespace
151 assert_create_quickvote_fails ElectionStruct.new({
153 :description => "valid",
154 :candidate_names => [" ", " ", " ", " "]})
157 def test_create_quickvote_dupe_candidates
158 assert_create_quickvote_fails ElectionStruct.new({
160 :description => "valid",
161 :candidate_names => ["Apple", "Apple", "Apple", "Apple"]})
163 # TODO: Previous may pass coincidentally if a uniq! then a sizecheck
164 # reveals too few unique names
166 # We don't want this to happen. Dupe canidates should fail
167 # regardless of how many are left.
169 assert_create_quickvote_fails ElectionStruct.new({
171 :description => "valid",
172 :candidate_names => ["Apple", "Apple", "Orange",
173 "Orange", "Pineapple" , "Pineapple"]})
176 def test_create_quickvote_candidates_nil_mixed
177 assert_create_quickvote_fails ElectionStruct.new({
179 :description => "valid",
180 :candidate_names => ["Apple", nil ]})
184 def test_create_quickvote_description_xmlescape
185 # Will an embedded XML element bork the table?
186 assert_create_quickvote_succeeds ElectionStruct.new({
188 :description => "test </string>",
189 :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]})
192 def test_create_quickvote_unprintable_description
193 assert_create_quickvote_succeeds ElectionStruct.new ({
195 :description => "test \x01\x02\x03\x04\x05\x06\x07\x08",
196 :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]})
199 def test_quickvote_proper_results
200 assert_create_quickvote_succeeds ElectionStruct.new({
202 :description => "Who is your favorite developer?",
203 :candidate_names => ["mako", "jdong", "justin"]})
205 reflection = invoke_delegated(:vote, :get_quickvote, "favdev")
207 # build the candidate list
209 reflection.candidate_names.each_with_index do |name, index|
210 candidates[name] = reflection.candidate_ids[index]
214 vote = [candidates["jdong"], candidates["mako"], candidates["justin"]]
215 assert_cast_quickvote_succeeds "favdev", "1:#{t}", [vote]
219 vote = [candidates["mako"], candidates["justin"], candidates["jdong"]]
220 assert_cast_quickvote_succeeds "favdev", "2:#{t}", [vote]
224 vote = [candidates["justin"], candidates["mako"], candidates["jdong"]]
225 assert_cast_quickvote_succeeds "favdev", "3:#{t}", [vote]
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"]]
239 ## helper methods and non-tests used throughout this file
240 #########################################################
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)
247 new_votes = invoke_delegated(:vote, :get_quickvote_votes, shortname)
248 assert_equal(old_votes.length, new_votes.length - 1)
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])
257 def assert_cast_quickvote_fails(shortname, id, vote)
258 assert_raise Test::Unit::AssertionFailedError do
259 assert_cast_quickvote_succeeds(shortname, id, vote)
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, "")
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)
278 def assert_create_quickvote_fails(election)
279 assert_raise Test::Unit::AssertionFailedError do
280 assert_create_quickvote_succeeds(election)