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 result = invoke_delegated :vote, :create_quickvote, election
19 assert_instance_of String, result
20 assert_equal "", result
21 assert_equal(invoke_delegated(:vote,:list_quickvotes).length, 1)
23 def test_get_quickvote
25 result = invoke_delegated :vote, :get_quickvote, "TestVote"
26 assert_instance_of ElectionStruct, result
27 assert_equal 0, result.name.casecmp("TestVote")
28 assert_equal result.description, "Test Vote"
29 assert_equal result.candidate_names.sort, ["Apple", "Orange", "Banana", "Pineapple"].sort
30 assert_not_nil result.id
32 assert_nil result.candidate_ids.uniq!
33 assert result.candidate_ids.length == result.candidate_names.length
35 def test_cast_quickvote
37 election = invoke_delegated :vote, :get_quickvote, "TestVote"
38 casted_vote = election.candidate_ids.sort_by {rand} #Shuffles
39 invoke_delegated :vote, :cast_quickvote, "TestVote", 42, [casted_vote]
40 quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
41 assert_equal quickvote_votes.length, 1
42 assert_equal quickvote_votes[0].vote, casted_vote
44 def test_cast_mass_quickvote
46 election = invoke_delegated :vote, :get_quickvote, "TestVote"
48 casted_vote = election.candidate_ids.sort_by {rand}
49 invoke_delegated :vote, :cast_quickvote, "TestVote", t, [casted_vote]
51 quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
52 assert_equal quickvote_votes.length, 20
54 def test_create_mass_quickvote
56 election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
57 result = invoke_delegated :vote, :create_quickvote, election
58 assert_instance_of String, result
59 assert_equal "", result
60 assert_equal(invoke_delegated(:vote,:list_quickvotes).length, t+1)
63 def test_create_quickvote_bad_name
64 election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
65 assert_create_quickvote_fails election
67 def test_create_quickvote_nil
68 election = ElectionStruct.new
69 assert_create_quickvote_fails election
71 def test_create_quickvote_name_nil
72 election = ElectionStruct.new :name => "", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
73 assert_create_quickvote_fails election
75 def test_create_quickvote_description_nil
76 election = ElectionStruct.new :name => "foobar", :description => nil, :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
77 assert_create_quickvote_fails election
80 def test_create_quickvote_description_whitespace
81 election = ElectionStruct.new :name => "foobar", :description => " ", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
82 assert_create_quickvote_fails election
83 election = ElectionStruct.new :name => "foobar", :description => "\t\t", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
84 assert_create_quickvote_fails election
86 def test_create_quickvote_candidates_nil
87 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
88 assert_create_quickvote_fails election
90 def test_create_quickvote_insufficient_candidates
91 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple"]
92 assert_create_quickvote_fails election
94 def test_create_quickvote_candidates_whitespace
95 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => [" ", " ", " ", " "]
96 assert_create_quickvote_fails election
98 def test_create_quickvote_dupe_candidates
99 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Apple", "Apple"]
100 assert_create_quickvote_fails election
102 # Previous may pass coincidentally if a uniq! then a sizecheck reveals too few unique names
103 # We don't want this to happen. Dupe canidates should fail regardless of how many are left.
105 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Orange", "Orange", "Pineapple" , "Pineapple"]
106 assert_create_quickvote_fails election
108 def test_create_quickvote_candidates_nil_mixed
109 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", nil ]
110 assert_create_quickvote_fails election
112 def test_create_quickvote_description_xmlescape
113 # Will an embedded XML element bork the table?
114 election = ElectionStruct.new :name => "foobar", :description => "test </string>", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
115 result = invoke_delegated :vote, :create_quickvote, election
116 assert_equal result, ""
117 reflection = invoke_delegated :vote, :get_quickvote, election.name
118 assert_equal election.description, reflection.description
119 assert_not_equal reflection.description.length, 0
122 def assert_create_quickvote_fails(election)
123 old_len=invoke_delegated(:vote,:list_quickvotes).length
124 result = invoke_delegated :vote, :create_quickvote, election
125 assert_instance_of String, result
126 assert_not_equal result.length, 0
127 assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len)