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

Benjamin Mako Hill || Want to submit a patch?