Added more testcases:
[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     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)
22   end
23   def test_get_quickvote
24     test_create_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
31     assert result.id != 0
32     assert_nil result.candidate_ids.uniq!
33     assert result.candidate_ids.length == result.candidate_names.length
34   end
35   def test_cast_quickvote
36     test_create_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
43   end
44   def test_cast_mass_quickvote
45     test_create_quickvote
46     election = invoke_delegated :vote, :get_quickvote, "TestVote"
47     20.times do |t|
48       casted_vote = election.candidate_ids.sort_by {rand}
49       invoke_delegated :vote, :cast_quickvote, "TestVote", t, [casted_vote]
50     end
51     quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
52     assert_equal quickvote_votes.length, 20
53   end
54   def test_create_mass_quickvote
55     10.times do |t|
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)
61     end
62   end
63   def test_create_quickvote_bad_name
64     election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
65     result = invoke_delegated :vote, :create_quickvote, election
66     assert_instance_of String, result
67     assert_not_equal result.length, 0
68     assert_equal(invoke_delegated(:vote,:list_quickvotes).length, 0)
69   end
70 end

Benjamin Mako Hill || Want to submit a patch?