Merged changes from jdong's branch into HEAD.
[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     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
28   end
29   def test_cast_mass_quickvote
30     test_create_quickvote
31     election = invoke_delegated :vote, :get_quickvote, "TestVote"
32     20.times do |t|
33       casted_vote = election.candidate_ids.sort_by {rand}
34       invoke_delegated :vote, :cast_quickvote, "TestVote", t, [casted_vote]
35     end
36     quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
37     assert_equal quickvote_votes.length, 20
38   end
39   def test_create_mass_quickvote
40     10.times do |t|
41       election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
42       assert_create_quickvote_succeeds election
43     end
44   end
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
48   end
49   def test_create_quickvote_nil
50     election =  ElectionStruct.new
51     assert_create_quickvote_fails election
52   end
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
56   end
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
60     
61   end
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
67   end
68   def test_create_quickvote_candidates_nil
69     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
70     assert_create_quickvote_fails election
71   end
72   def test_create_quickvote_insufficient_candidates
73     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple"]
74     assert_create_quickvote_fails election
75   end
76   def test_create_quickvote_candidates_whitespace
77     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => [" ", "   ", "       ", "         "]
78     assert_create_quickvote_fails election
79   end
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
83
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.
86
87     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Orange", "Orange", "Pineapple" ,  "Pineapple"]
88     assert_create_quickvote_fails election
89   end
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
93   end
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
98   end
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
102   end
103   private
104   def assert_create_quickvote_succeeds(election)
105     # Checks if a created quickvote is identical when retrieved
106     old_len=invoke_delegated(:vote,:list_quickvotes).length
107     result = invoke_delegated :vote, :create_quickvote, election
108     assert_equal result, ""
109     reflection = invoke_delegated :vote, :get_quickvote, election.name
110     assert_equal election.description, reflection.description
111     assert_equal 0, election.name.casecmp(reflection.name)
112     assert_equal election.candidate_names, reflection.candidate_names
113     assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len+1)
114   end
115   def assert_create_quickvote_fails(election)
116     # Helper function to check that creating this quickvote fails
117     old_len=invoke_delegated(:vote,:list_quickvotes).length
118     result = invoke_delegated :vote, :create_quickvote, election
119     assert_instance_of String, result
120     assert_not_equal result.length, 0
121     assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len)
122   end
123 end

Benjamin Mako Hill || Want to submit a patch?