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_equal result.length, 0
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
20 def test_cast_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]
26 def test_get_nonexistent_quickvote
27 assert_raises ArgumentError do
28 qv = invoke_delegated :vote, :get_quickvote, "asdfasdfasdf"
31 def test_cast_mass_quickvote
33 election = invoke_delegated :vote, :get_quickvote, "TestVote"
35 casted_vote = election.candidate_ids.sort_by {rand}
36 assert_cast_quickvote_succeeds "TestVote", t, [casted_vote]
39 def test_cast_quickvote_nonexistent
40 assert_cast_quickvote_fails "ASDFJOFASF", "me", [1,2,3]
42 def test_cast_quickvote_nonexistent_candidates
44 election = invoke_delegated :vote, :get_quickvote, "TestVote"
45 assert_cast_quickvote_fails "TestVote", 42, [123,342314,5342,1,1,2]
47 def test_create_mass_quickvote
49 election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
50 assert_create_quickvote_succeeds election
53 def test_create_quickvote_bad_name
54 election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
55 assert_create_quickvote_fails election
57 def test_create_quickvote_nil
58 election = ElectionStruct.new
59 assert_create_quickvote_fails election
61 def test_create_quickvote_name_nil
62 election = ElectionStruct.new :name => "", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
63 assert_create_quickvote_fails election
65 def test_create_quickvote_description_nil
66 election = ElectionStruct.new :name => "foobar", :description => nil, :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
67 assert_create_quickvote_fails election
70 def test_create_quickvote_description_whitespace
71 election = ElectionStruct.new :name => "foobar", :description => " ", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
72 assert_create_quickvote_fails election
73 election = ElectionStruct.new :name => "foobar", :description => "\t\t", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
74 assert_create_quickvote_fails election
76 def test_create_quickvote_candidates_nil
77 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
78 assert_create_quickvote_fails election
80 def test_create_quickvote_insufficient_candidates
81 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple"]
82 assert_create_quickvote_fails election
84 def test_create_quickvote_candidates_whitespace
85 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => [" ", " ", " ", " "]
86 assert_create_quickvote_fails election
88 def test_create_quickvote_dupe_candidates
89 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Apple", "Apple"]
90 assert_create_quickvote_fails election
92 # Previous may pass coincidentally if a uniq! then a sizecheck reveals too few unique names
93 # We don't want this to happen. Dupe canidates should fail regardless of how many are left.
95 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Orange", "Orange", "Pineapple" , "Pineapple"]
96 assert_create_quickvote_fails election
98 def test_create_quickvote_candidates_nil_mixed
99 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", nil ]
100 assert_create_quickvote_fails election
102 def test_create_quickvote_description_xmlescape
103 # Will an embedded XML element bork the table?
104 election = ElectionStruct.new :name => "foobar", :description => "test </string>", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
105 assert_create_quickvote_succeeds election
107 def test_create_quickvote_unprintable_description
108 election = ElectionStruct.new :name => "foobar", :description => "test \x01\x02\x03\x04\x05\x06\x07\x08", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
109 assert_create_quickvote_succeeds election
111 def test_quickvote_proper_results
112 election = ElectionStruct.new :name => "favdev", :description => "Who is your favorite developer?", :candidate_names => ["mako", "jdong", "justin"]
113 assert_create_quickvote_succeeds election
114 reflection = invoke_delegated :vote, :get_quickvote, "favdev"
116 reflection.candidate_names.each_with_index do |name, index|
117 candidates[name] = reflection.candidate_ids[index]
120 vote = [candidates["jdong"], candidates["mako"], candidates["justin"]]
121 assert_cast_quickvote_succeeds "favdev", "1:#{t}", [vote]
124 vote = [candidates["mako"], candidates["justin"], candidates["jdong"]]
125 assert_cast_quickvote_succeeds "favdev", "2:#{t}", [vote]
128 vote = [candidates["justin"], candidates["mako"], candidates["jdong"]]
129 assert_cast_quickvote_succeeds "favdev", "3:#{t}", [vote]
132 assert_nothing_raised {results=invoke_delegated(:vote, :get_quickvote_results, "favdev")}
133 assert_equal results.approval_winners, [candidates["mako"]]
134 assert_equal results.borda_winners, [candidates["jdong"]]
135 assert_equal results.plurality_winners, [candidates["jdong"]]
136 assert_equal results.condorcet_winners, [candidates["jdong"]]
137 assert_equal results.ssd_winners, [candidates["jdong"]]
140 def assert_cast_quickvote_succeeds(shortname, id, vote)
141 assert_nothing_raised do
142 old_votes = invoke_delegated :vote, :get_quickvote_votes, shortname
143 invoke_delegated :vote, :cast_quickvote, shortname, id, vote
144 new_votes = invoke_delegated :vote, :get_quickvote_votes, shortname
145 assert_equal old_votes.length, new_votes.length-1
146 assert_not_nil(reflection = new_votes.find_all { |v| v.voter_session_id == "XMLRPC:#{id}" })
147 assert_equal reflection.length, 1
148 assert_equal reflection[0].vote, vote[0]
151 def assert_cast_quickvote_fails(shortname, id, vote)
152 assert_raise Test::Unit::AssertionFailedError do
153 assert_cast_quickvote_succeeds(shortname, id, vote)
156 def assert_create_quickvote_succeeds(election)
157 # Checks if a created quickvote is identical when retrieved
158 assert_nothing_raised do
159 old_len=invoke_delegated(:vote,:list_quickvotes).length
160 result = invoke_delegated :vote, :create_quickvote, election
161 assert_equal result, ""
162 reflection = invoke_delegated :vote, :get_quickvote, election.name
163 assert_equal election.description, reflection.description
164 assert_equal 0, election.name.casecmp(reflection.name)
165 assert_equal election.candidate_names, reflection.candidate_names
166 assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len+1)
169 def assert_create_quickvote_fails(election)
170 assert_raise Test::Unit::AssertionFailedError do
171 assert_create_quickvote_succeeds election