* Add a few testcases for checking for nonexistent things
[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_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_get_nonexistent_quickvote
27     assert_raises ArgumentError do
28       qv = invoke_delegated :vote, :get_quickvote, "asdfasdfasdf"
29     end
30   end
31   def test_cast_mass_quickvote
32     test_create_quickvote
33     election = invoke_delegated :vote, :get_quickvote, "TestVote"
34     20.times do |t|
35       casted_vote = election.candidate_ids.sort_by {rand}
36       assert_cast_quickvote_succeeds "TestVote", t, [casted_vote]
37     end
38   end
39   def test_cast_quickvote_nonexistent
40     assert_cast_quickvote_fails "ASDFJOFASF", "me", [1,2,3]
41   end
42   def test_cast_quickvote_nonexistent_candidates
43     test_create_quickvote
44     election = invoke_delegated :vote, :get_quickvote, "TestVote"
45     assert_cast_quickvote_fails "TestVote", 42, [123,342314,5342,1,1,2]
46   end
47   def test_create_mass_quickvote
48     10.times do |t|
49       election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
50       assert_create_quickvote_succeeds election
51     end
52   end
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
56   end
57   def test_create_quickvote_nil
58     election =  ElectionStruct.new
59     assert_create_quickvote_fails election
60   end
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
64   end
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
68     
69   end
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
75   end
76   def test_create_quickvote_candidates_nil
77     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
78     assert_create_quickvote_fails election
79   end
80   def test_create_quickvote_insufficient_candidates
81     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple"]
82     assert_create_quickvote_fails election
83   end
84   def test_create_quickvote_candidates_whitespace
85     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => [" ", "   ", "       ", "         "]
86     assert_create_quickvote_fails election
87   end
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
91
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.
94
95     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Orange", "Orange", "Pineapple" ,  "Pineapple"]
96     assert_create_quickvote_fails election
97   end
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
101   end
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
106   end
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
110   end
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"
115     candidates = {}
116     reflection.candidate_names.each_with_index do |name, index|
117       candidates[name] =  reflection.candidate_ids[index]
118     end
119     25.times do |t|
120       vote = [candidates["jdong"], candidates["mako"], candidates["justin"]]
121       assert_cast_quickvote_succeeds "favdev", "1:#{t}", [vote]
122     end
123     5.times do |t|
124       vote = [candidates["mako"], candidates["justin"], candidates["jdong"]]
125       assert_cast_quickvote_succeeds "favdev", "2:#{t}", [vote]
126     end
127     10.times do |t|
128       vote = [candidates["justin"], candidates["mako"], candidates["jdong"]]
129       assert_cast_quickvote_succeeds "favdev", "3:#{t}", [vote]
130     end
131     results=nil
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"]]
138   end
139   private
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]
149     end
150   end
151   def assert_cast_quickvote_fails(shortname, id, vote)
152     assert_raise Test::Unit::AssertionFailedError do 
153       assert_cast_quickvote_succeeds(shortname, id, vote)
154     end
155   end
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)
167     end
168   end
169   def assert_create_quickvote_fails(election)
170     assert_raise Test::Unit::AssertionFailedError do
171       assert_create_quickvote_succeeds election
172     end
173   end
174 end

Benjamin Mako Hill || Want to submit a patch?