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 invoke_delegated :vote, :cast_quickvote, "TestVote", 42, [casted_vote]
25 quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
26 assert_equal quickvote_votes.length, 1
27 assert_equal quickvote_votes[0].vote, casted_vote
29 def test_cast_mass_quickvote
31 election = invoke_delegated :vote, :get_quickvote, "TestVote"
33 casted_vote = election.candidate_ids.sort_by {rand}
34 invoke_delegated :vote, :cast_quickvote, "TestVote", t, [casted_vote]
36 quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
37 assert_equal quickvote_votes.length, 20
39 def test_create_mass_quickvote
41 election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
42 assert_create_quickvote_succeeds election
45 def test_create_quickvote_bad_name
46 election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
47 assert_create_quickvote_fails election
49 def test_create_quickvote_nil
50 election = ElectionStruct.new
51 assert_create_quickvote_fails election
53 def test_create_quickvote_name_nil
54 election = ElectionStruct.new :name => "", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
55 assert_create_quickvote_fails election
57 def test_create_quickvote_description_nil
58 election = ElectionStruct.new :name => "foobar", :description => nil, :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
59 assert_create_quickvote_fails election
62 def test_create_quickvote_description_whitespace
63 election = ElectionStruct.new :name => "foobar", :description => " ", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
64 assert_create_quickvote_fails election
65 election = ElectionStruct.new :name => "foobar", :description => "\t\t", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
66 assert_create_quickvote_fails election
68 def test_create_quickvote_candidates_nil
69 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
70 assert_create_quickvote_fails election
72 def test_create_quickvote_insufficient_candidates
73 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple"]
74 assert_create_quickvote_fails election
76 def test_create_quickvote_candidates_whitespace
77 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => [" ", " ", " ", " "]
78 assert_create_quickvote_fails election
80 def test_create_quickvote_dupe_candidates
81 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Apple", "Apple"]
82 assert_create_quickvote_fails election
84 # Previous may pass coincidentally if a uniq! then a sizecheck reveals too few unique names
85 # We don't want this to happen. Dupe canidates should fail regardless of how many are left.
87 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Orange", "Orange", "Pineapple" , "Pineapple"]
88 assert_create_quickvote_fails election
90 def test_create_quickvote_candidates_nil_mixed
91 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", nil ]
92 assert_create_quickvote_fails election
94 def test_create_quickvote_description_xmlescape
95 # Will an embedded XML element bork the table?
96 election = ElectionStruct.new :name => "foobar", :description => "test </string>", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
97 assert_create_quickvote_succeeds election
99 def test_create_quickvote_unprintable_description
100 election = ElectionStruct.new :name => "foobar", :description => "test \x01\x02\x03\x04\x05\x06\x07\x08", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
101 assert_create_quickvote_succeeds election
103 def test_quickvote_proper_results
104 election = ElectionStruct.new :name => "favdev", :description => "Who is your favorite developer?", :candidate_names => ["mako", "jdong", "justin"]
105 assert_create_quickvote_succeeds election
106 reflection = invoke_delegated :vote, :get_quickvote, "favdev"
108 reflection.candidate_names.each_with_index do |name, index|
109 candidates[name] = reflection.candidate_ids[index]
112 vote = [candidates["jdong"], candidates["mako"], candidates["justin"]]
113 invoke_delegated :vote, :cast_quickvote, "favdev", "1:#{t}", [vote]
116 vote = [candidates["mako"], candidates["justin"], candidates["jdong"]]
117 invoke_delegated :vote, :cast_quickvote, "favdev", "2:#{t}", [vote]
120 vote = [candidates["justin"], candidates["mako"], candidates["jdong"]]
121 invoke_delegated :vote, :cast_quickvote, "favdev", "3:#{t}", [vote]
123 results=invoke_delegated(:vote, :get_quickvote_results, "favdev")
124 assert_equal results.approval_winners, [candidates["mako"]]
125 assert_equal results.borda_winners, [candidates["jdong"]]
126 assert_equal results.plurality_winners, [candidates["jdong"]]
127 assert_equal results.condorcet_winners, [candidates["jdong"]]
128 assert_equal results.ssd_winners, [candidates["jdong"]]
129 assert_equal results.errors.length, 0
132 def assert_create_quickvote_succeeds(election)
133 # Checks if a created quickvote is identical when retrieved
134 old_len=invoke_delegated(:vote,:list_quickvotes).length
135 result = invoke_delegated :vote, :create_quickvote, election
136 assert_equal result, ""
137 reflection = invoke_delegated :vote, :get_quickvote, election.name
138 assert_equal election.description, reflection.description
139 assert_equal 0, election.name.casecmp(reflection.name)
140 assert_equal election.candidate_names, reflection.candidate_names
141 assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len+1)
143 def assert_create_quickvote_fails(election)
144 # Helper function to check that creating this quickvote fails
145 old_len=invoke_delegated(:vote,:list_quickvotes).length
146 result = invoke_delegated :vote, :create_quickvote, election
147 assert_instance_of String, result
148 assert_not_equal result.length, 0
149 assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len)