fixed errors with election/class
[selectricity] / old_api_code / graph_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'graph_controller'
3 require 'selectricity_service_controller'
4
5 # Re-raise errors caught by the controller.
6 class GraphController; def rescue_action(e) raise e end; end
7
8 class GraphControllerTest < Test::Unit::TestCase
9
10   #fixtures :data
11
12   def setup
13     @controller = GraphController.new
14     @request    = ActionController::TestRequest.new
15     @response   = ActionController::TestResponse.new
16     @SS         = SelectricityService.new
17     @election = nil
18   end
19
20   # TODO Replace this with your actual tests
21   def test_show_nonexist_votes_per_interval
22     assert_raise(ActiveRecord::RecordNotFound) { get :votes_per_interval, { :id => 42 } }
23   end
24
25   def test_show_nonexist_borda_bar
26     assert_raise(ActiveRecord::RecordNotFound) { get :borda_bar, { :id => 42 } }
27   end
28
29   def test_show_nonexist_choices_positions
30     assert_raise(ActiveRecord::RecordNotFound) { get :choices_positions, { :id => 42 } }
31   end
32   
33   def test_show_empty_votes_per_interval
34     make_election
35     get :votes_per_interval, { :id => @election.id }
36     assert_response :success
37     assert_equal 'image/png', @response.headers['Content-Type']
38   end
39   
40   def test_show_empty_borda_bar
41     make_election
42     get :borda_bar, { :id => @election.id }
43     assert_response :success
44     assert_equal 'image/png', @response.headers['Content-Type']
45   end
46
47   def test_show_empty_choices_positions
48     make_election
49     get :choices_positions, { :id => @election.id }
50     assert_response :success
51     assert_equal 'image/png', @response.headers['Content-Type']
52   end
53   
54   def test_show_single_votes_per_interval
55     make_election
56     cast_vote
57     get :votes_per_interval, { :id => @election.id }
58     assert_response :success
59     assert_equal 'image/png', @response.headers['Content-Type']
60   end
61   
62   def test_show_single_borda_bar
63     make_election
64     cast_vote
65     get :borda_bar, { :id => @election.id }
66     assert_response :success
67     assert_equal 'image/png', @response.headers['Content-Type']
68   end
69
70   def test_show_single_choices_positions
71     make_election
72     cast_vote
73     get :choices_positions, { :id => @election.id }
74     assert_response :success
75     assert_equal 'image/png', @response.headers['Content-Type']
76   end
77
78   def test_show_many_votes_per_interval
79     make_election
80     20.times {cast_vote}
81     get :votes_per_interval, { :id => @election.id }
82     assert_response :success
83     assert_equal 'image/png', @response.headers['Content-Type']
84   end
85   
86   def test_show_many_borda_bar
87     make_election
88     20.times {cast_vote}
89     get :borda_bar, { :id => @election.id }
90     assert_response :success
91     assert_equal 'image/png', @response.headers['Content-Type']
92   end
93
94   def test_show_many_choices_positions
95     make_election
96     20.times {cast_vote}
97     get :choices_positions, { :id => @election.id }
98     assert_response :success
99     assert_equal 'image/png', @response.headers['Content-Type']
100   end
101
102
103   private
104   def make_election
105     e=QuickVoteStruct.new( :name => "test", :description => "This is a test",
106                           :candidate_names => ['choice1', 'choice2', 'choice3'])
107     @SS.create_quickvote e
108     @election = @SS.list_quickvotes[0]
109     assert_not_nil @election
110   end
111
112   private
113   def cast_vote
114     @SS.cast_quickvote @election.name, 42, [@election.candidate_ids.sort_by {rand}]
115   end
116   
117 end

Benjamin Mako Hill || Want to submit a patch?