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_cast_mass_quickvote
28 election = invoke_delegated :vote, :get_quickvote, "TestVote"
30 casted_vote = election.candidate_ids.sort_by {rand}
31 assert_cast_quickvote_succeeds "TestVote", t, [casted_vote]
34 def test_create_mass_quickvote
36 election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
37 assert_create_quickvote_succeeds election
40 def test_create_quickvote_bad_name
41 election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
42 assert_create_quickvote_fails election
44 def test_create_quickvote_nil
45 election = ElectionStruct.new
46 assert_create_quickvote_fails election
48 def test_create_quickvote_name_nil
49 election = ElectionStruct.new :name => "", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
50 assert_create_quickvote_fails election
52 def test_create_quickvote_description_nil
53 election = ElectionStruct.new :name => "foobar", :description => nil, :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
54 assert_create_quickvote_fails election
57 def test_create_quickvote_description_whitespace
58 election = ElectionStruct.new :name => "foobar", :description => " ", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
59 assert_create_quickvote_fails election
60 election = ElectionStruct.new :name => "foobar", :description => "\t\t", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
61 assert_create_quickvote_fails election
63 def test_create_quickvote_candidates_nil
64 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
65 assert_create_quickvote_fails election
67 def test_create_quickvote_insufficient_candidates
68 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple"]
69 assert_create_quickvote_fails election
71 def test_create_quickvote_candidates_whitespace
72 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => [" ", " ", " ", " "]
73 assert_create_quickvote_fails election
75 def test_create_quickvote_dupe_candidates
76 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Apple", "Apple"]
77 assert_create_quickvote_fails election
79 # Previous may pass coincidentally if a uniq! then a sizecheck reveals too few unique names
80 # We don't want this to happen. Dupe canidates should fail regardless of how many are left.
82 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Orange", "Orange", "Pineapple" , "Pineapple"]
83 assert_create_quickvote_fails election
85 def test_create_quickvote_candidates_nil_mixed
86 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", nil ]
87 assert_create_quickvote_fails election
89 def test_create_quickvote_description_xmlescape
90 # Will an embedded XML element bork the table?
91 election = ElectionStruct.new :name => "foobar", :description => "test </string>", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
92 assert_create_quickvote_succeeds election
94 def test_create_quickvote_unprintable_description
95 election = ElectionStruct.new :name => "foobar", :description => "test \x01\x02\x03\x04\x05\x06\x07\x08", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
96 assert_create_quickvote_succeeds election
98 def test_quickvote_proper_results
99 election = ElectionStruct.new :name => "favdev", :description => "Who is your favorite developer?", :candidate_names => ["mako", "jdong", "justin"]
100 assert_create_quickvote_succeeds election
101 reflection = invoke_delegated :vote, :get_quickvote, "favdev"
103 reflection.candidate_names.each_with_index do |name, index|
104 candidates[name] = reflection.candidate_ids[index]
107 vote = [candidates["jdong"], candidates["mako"], candidates["justin"]]
108 assert_cast_quickvote_succeeds "favdev", "1:#{t}", [vote]
111 vote = [candidates["mako"], candidates["justin"], candidates["jdong"]]
112 assert_cast_quickvote_succeeds "favdev", "2:#{t}", [vote]
115 vote = [candidates["justin"], candidates["mako"], candidates["jdong"]]
116 assert_cast_quickvote_succeeds "favdev", "3:#{t}", [vote]
118 results=invoke_delegated(:vote, :get_quickvote_results, "favdev")
119 assert_equal results.approval_winners, [candidates["mako"]]
120 assert_equal results.borda_winners, [candidates["jdong"]]
121 assert_equal results.plurality_winners, [candidates["jdong"]]
122 assert_equal results.condorcet_winners, [candidates["jdong"]]
123 assert_equal results.ssd_winners, [candidates["jdong"]]
124 assert_equal results.errors.length, 0
127 def assert_cast_quickvote_succeeds(shortname, id, vote)
128 old_votes = invoke_delegated :vote, :get_quickvote_votes, shortname
129 assert_nothing_raised do
130 invoke_delegated :vote, :cast_quickvote, shortname, id, vote
132 new_votes = invoke_delegated :vote, :get_quickvote_votes, shortname
133 assert_equal old_votes.length, new_votes.length-1
134 assert_not_nil(reflection = new_votes.find_all { |v| v.voter_session_id == "XMLRPC:#{id}" })
135 assert_equal reflection.length, 1
136 assert_equal reflection[0].vote, vote[0]
138 def assert_cast_quickvote_fails(shortname, id, vote)
139 assert_raises Test::Unit::AssertionFailedError do
140 assert_cast_quickvote_succeeds(shortname, id, vote)
143 def assert_create_quickvote_succeeds(election)
144 # Checks if a created quickvote is identical when retrieved
145 old_len=invoke_delegated(:vote,:list_quickvotes).length
147 assert_nothing_raised do
148 result = invoke_delegated :vote, :create_quickvote, election
150 assert_equal result, ""
151 reflection = invoke_delegated :vote, :get_quickvote, election.name
152 assert_equal election.description, reflection.description
153 assert_equal 0, election.name.casecmp(reflection.name)
154 assert_equal election.candidate_names, reflection.candidate_names
155 assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len+1)
157 def assert_create_quickvote_fails(election)
158 assert_raises Test::Unit::AssertionFailedError do
159 assert_create_quickvote_succeeds election