From 905cf65f6b3e8acc894c268c3bf729e7b0c76292 Mon Sep 17 00:00:00 2001 From: Date: Thu, 23 Aug 2007 15:45:11 -0400 Subject: [PATCH] Big commit includes: * A bunch of cleanup of the unit tests. * Addition of fixtures for elections and candidates. * Cleanup of the selectricitservice model. * Additional tweaks for testing including addition of ruby-debug for testing. --- app/controllers/quickvote_controller.rb | 48 +++++++++++++------- app/models/election.rb | 2 +- app/models/quick_vote.rb | 45 ++++++++++-------- app/models/selectricity_service.rb | 42 +++++++++++------ app/views/quickvote/_candidate_list.rhtml | 6 +-- app/views/quickvote/create.rhtml | 4 +- config/environments/test.rb | 7 ++- test/fixtures/candidates.yml | 34 ++++++++++++-- test/fixtures/elections.yml | 25 ++++++++-- test/functional/quickvote_controller_test.rb | 12 ++--- test/unit/candidate_test.rb | 6 +-- test/unit/election_test.rb | 7 +-- test/unit/quickvote_test.rb | 42 +++++++++++++---- test/unit/selectricityservice_test.rb | 15 +++--- 14 files changed, 202 insertions(+), 93 deletions(-) diff --git a/app/controllers/quickvote_controller.rb b/app/controllers/quickvote_controller.rb index 031c075..e675638 100644 --- a/app/controllers/quickvote_controller.rb +++ b/app/controllers/quickvote_controller.rb @@ -13,7 +13,7 @@ class QuickvoteController < ApplicationController if params[:quickvote] @quickvote = QuickVote.new(params[:quickvote]) # store the candidate grabbed through ajax and stored in flash - @quickvote.candidatelist = flash[:candlist] + @quickvote.candidate_names = flash[:candidate_names] @quickvote.description=@quickvote.description # try to save, if it fails, show the page again (the flash should # still be intact @@ -21,14 +21,14 @@ class QuickvoteController < ApplicationController @quickvote = @quickvote.reload render :action => 'success' else - flash.keep(:candlist) + flash.keep(:candidate_names) end else # if we don't have a quickvote param, it means that the person # here has not been hitting this page and we can clear any - # candlist in the flash - flash.delete(:candlist) if flash.has_key?(:candlist) + # candidate_names list in the flash + flash.delete(:candidate_names) if flash.has_key?(:candidate_names) @quickvote = QuickVote.new end end @@ -36,13 +36,16 @@ class QuickvoteController < ApplicationController def add_candidate candidate_name = params[:ajax][:newcandidate] unless candidate_name.strip.empty? - if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array) - flash[:candlist] << candidate_name unless flash[:candlist].index(candidate_name) + if flash.has_key?(:candidate_names) \ + and flash[:candidate_names].instance_of?(Array) + unless flash[:candidate_names].index(candidate_name) + flash[:candidate_names] << candidate_name + end else - flash[:candlist] = [ candidate_name ] + flash[:candidate_names] = [ candidate_name ] end end - flash.keep(:candlist) + flash.keep(:candidate_names) render_partial 'candidate_list' end @@ -57,8 +60,9 @@ class QuickvoteController < ApplicationController if @election # look to see that the voter has been created and has voted in # this election, and has confirmed their vote - @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?", - session.session_id, @election.id])[0] + @voter = QuickVoter.find(:all, + :conditions => ["session_id = ? and election_id = ?", + session.session_id, @election.id])[0] # if the voter has not voted we destroy them if @voter and not @voter.voted? @@ -90,8 +94,9 @@ class QuickvoteController < ApplicationController election = QuickVote.ident_to_quickvote(params[:ident]) # find out who the voter is for this election - @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?", - session.session_id, election.id])[0] + @voter = QuickVoter.find(:all, + :conditions => ["session_id = ? and election_id = ?", + session.session_id, election.id])[0] if not @voter # we have not seen this voter before. something is wrong, try @@ -120,7 +125,8 @@ class QuickvoteController < ApplicationController end def change - voter = QuickVoter.find(:all, :conditions => ["session_id = ?", session.session_id])[0] + voter = QuickVoter.find(:all, :conditions => ["session_id = ?", + session.session_id])[0] voter.destroy redirect_to quickvote_url( :ident => params[:ident] ) end @@ -138,19 +144,27 @@ class QuickvoteController < ApplicationController def mapvoters @map = GMap.new("map_div_id") @map.control_init(:large_map => true, :map_type => true) - center=nil + center = nil + QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter| next unless voter.ipaddress + location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress) next unless location.lng and location.lat + unless center - center=[location.lat,location.lng] - @map.center_zoom_init(center,4) + center = [location.lat, location.lng] + @map.center_zoom_init(center, 4) end - marker = GMarker.new([location.lat,location.lng], :title => "Voter", :info_window => (voter.ipaddress or "unknown")+" "+voter.vote.votestring) + + marker = GMarker.new([location.lat,location.lng], + :title => "Voter", + :info_window => (voter.ipaddress or "unknown") \ + + " " + voter.vote.votestring) @map.overlay_init(marker) end end + ############################################################### # the following method pertains to displaying the results of a # quickvote diff --git a/app/models/election.rb b/app/models/election.rb index 7343d72..a574139 100644 --- a/app/models/election.rb +++ b/app/models/election.rb @@ -57,7 +57,7 @@ class Election < ActiveRecord::Base def start_blockers reasons = [] - + debugger if self.candidates.length <= 1 reasons << "You must have at least two candidates." end diff --git a/app/models/quick_vote.rb b/app/models/quick_vote.rb index 494f7ac..bd39051 100644 --- a/app/models/quick_vote.rb +++ b/app/models/quick_vote.rb @@ -1,8 +1,10 @@ class QuickVote < Election + before_validation :build_candidate_names after_validation :create_candidates validates_uniqueness_of :name validates_presence_of :name - attr_accessor :raw_candidates + + attr_accessor :candidate_names attr_accessor :reviewed def initialize(params={}) @@ -14,16 +16,12 @@ class QuickVote < Election Time.now + 30.days - 1.second end - def enddate - super(30) - end - def validate - if not @raw_candidates or @raw_candidates.length < 2 + if @candidate_names.length < 2 errors.add(nil, "You must list at least two candidates.") end - - @raw_candidates.each do |c| + + @candidate_names.each do |c| unless c.instance_of? String errors.add(nil, "Candidates must be strings") next @@ -33,9 +31,11 @@ class QuickVote < Election errors.add(nil, "Candidate name must not be empty") next end - end if @raw_candidates - - errors.add(nil, "Candidates must all be unique") if @raw_candidates and @raw_candidates.uniq! + end if @candidate_names + + if @candidate_names and @candidate_names.uniq! + errors.add(nil, "Candidates must all be unique") + end if name =~ /[^A-Za-z0-9]/ errors.add(:name, "must only include numbers and letters.") @@ -47,15 +47,10 @@ class QuickVote < Election if name =~ /^(create|index|confirm|change|results)$/ errors.add(:name, " is a reserved word.") end - + if enddate < startdate - errors.add(nil, "QuickVotes can't end before they start!") + errors.add(nil, "QuickVotes can't end before they start") end - - end - - def candidatelist=(candlist) - @raw_candidates = candlist end def name @@ -66,9 +61,21 @@ class QuickVote < Election reviewed.to_i == 1 end + def build_candidate_names + @candidate_names ||= [] + if @candidate_names.empty? and not candidates.empty? + @candidate_names = candidates.collect {|c| c.name} + end + end + def create_candidates return unless errors.empty? - @raw_candidates.each do |name| + + # delete the candidates + candidates.each {|c| c.destroy} + + # create the new list based on the names + @candidate_names.each do |name| candidate = Candidate.new({:name => name}) self.candidates << candidate end diff --git a/app/models/selectricity_service.rb b/app/models/selectricity_service.rb index 196161a..544b618 100644 --- a/app/models/selectricity_service.rb +++ b/app/models/selectricity_service.rb @@ -69,31 +69,47 @@ class SelectricityService < ActionWebService::Base result.candidate_names=candidates.values result end + def get_quickvote_votes(shortname) - qv=QuickVote.ident_to_quickvote(shortname) - votes=Array.new + qv = QuickVote.ident_to_quickvote(shortname) + unless qv raise ArgumentError.new("Cannot find QuickVote #{shortname}") end - qv.votes.each do |vote| - votes << VoteInfo.new(:voter_id => vote.voter.id, :voter_ipaddress => vote.voter.ipaddress, :vote_time => vote.time.to_i, :vote => vote.votes, :voter_session_id => vote.voter.session_id ) + + qv.votes.collect do |vote| + VoteInfo.new(:voter_id => vote.voter.id, + :voter_ipaddress => vote.voter.ipaddress, + :vote_time => vote.time.to_i, + :vote => vote.votes, + :voter_session_id => vote.voter.session_id) end - return votes end + def list_quickvotes() - all=Array.new - QuickVote.find(:all).each do |election| - all << get_quickvote(election.name) + QuickVote.find(:all).collect do |election| + get_quickvote(election.name) end - return all end + def get_quickvote(shortname) - raise ArgumentError.new("Cannot find QuickVote named #{shortname}") unless election=QuickVote.ident_to_quickvote(shortname) - return ElectionStruct.new(:id => election.id, :name => election.name, :description => election.description, :candidate_ids => election.candidates.collect {|c| c.id }, :candidate_names => election.candidates.collect {|c| c.name } ) + unless election = QuickVote.ident_to_quickvote(shortname) + raise ArgumentError.new("Cannot find QuickVote named #{shortname}") + end + + ElectionStruct.new( + :id => election.id, + :name => election.name, + :description => election.description, + :candidate_ids => election.candidates.collect {|c| c.id }, + :candidate_names => election.candidates.collect {|c| c.name } ) end + def create_quickvote(election) - qv=QuickVote.new(:name => election.name, :description => election.description) - qv.candidatelist=election.candidate_names + qv = QuickVote.new(:name => election.name, + :description => election.description) + qv.candidate_names = election.candidate_names + if qv.save return "" else diff --git a/app/views/quickvote/_candidate_list.rhtml b/app/views/quickvote/_candidate_list.rhtml index ddb47e5..76de0ac 100644 --- a/app/views/quickvote/_candidate_list.rhtml +++ b/app/views/quickvote/_candidate_list.rhtml @@ -1,14 +1,14 @@ <% %> -<% if flash[:candlist] %> +<% if flash[:candidate_names] %> <% end %> -<% form_remote_tag(:update => 'candlist', +<% form_remote_tag(:update => 'candidate_names', :url => { :action => 'add_candidate' }, :complete => "Field.focus('ajax_newcandidate')") do %>

diff --git a/app/views/quickvote/create.rhtml b/app/views/quickvote/create.rhtml index a7ccd63..7ccb644 100644 --- a/app/views/quickvote/create.rhtml +++ b/app/views/quickvote/create.rhtml @@ -3,10 +3,10 @@ <%= error_messages_for 'quickvote' %> -

+

-
+
<%= render :partial => 'candidate_list' %>
diff --git a/config/environments/test.rb b/config/environments/test.rb index f0689b9..7c2f617 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -16,4 +16,9 @@ config.action_controller.perform_caching = false # Tell ActionMailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. -config.action_mailer.delivery_method = :test \ No newline at end of file +config.action_mailer.delivery_method = :test + +# start the debugger +require 'ruby-debug' +SCRIPT_LINES__ = {} +Debugger.start diff --git a/test/fixtures/candidates.yml b/test/fixtures/candidates.yml index 8794d28..4a651bf 100644 --- a/test/fixtures/candidates.yml +++ b/test/fixtures/candidates.yml @@ -1,5 +1,31 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -first: - id: 1 -another: - id: 2 + +mika_normal: + id: 1 + name: mika + election_id: 1 + +mako_normal: + id: 2 + name: mako + election_id: 1 + +bettamax_normal: + id: 3 + name: bettamax + election_id: 1 + +mika_badend: + id: 4 + name: mika + election_id: 2 + +mako_badend: + id: 5 + name: mako + election_id: 2 + +bettamax_badend: + id: 6 + name: bettamax + election_id: 2 diff --git a/test/fixtures/elections.yml b/test/fixtures/elections.yml index 8794d28..814540f 100644 --- a/test/fixtures/elections.yml +++ b/test/fixtures/elections.yml @@ -1,5 +1,22 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -first: - id: 1 -another: - id: 2 +quickvote_normal: + id: 1 + name: acetarium + description: who is the winner? + anonymous: 1 + startdate: 2007-08-22 12:00:00 + enddate: 2007-09-21 11:59:59 + active: 1 + type: QuickVote + election_method: ssd + +quickvote_invalid_endtime: + id: 2 + name: acetarium2 + description: who is the winner? + anonymous: 1 + startdate: 2007-08-22 12:00:00 + enddate: 2003-09-21 11:59:59 + active: 1 + type: QuickVote + election_method: ssd diff --git a/test/functional/quickvote_controller_test.rb b/test/functional/quickvote_controller_test.rb index 02c133e..ee79b3f 100644 --- a/test/functional/quickvote_controller_test.rb +++ b/test/functional/quickvote_controller_test.rb @@ -28,7 +28,7 @@ class QuickvoteControllerTest < Test::Unit::TestCase end def test_create_quickvote - post(:create, {'commit' =>"Create Quickvote", 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}}, nil, {:candlist=>["foo", "bar", "foobar"]}) + post(:create, {'commit' =>"Create Quickvote", 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}}, nil, {:candidate_names=>["foo", "bar", "foobar"]}) assert_template "quickvote/success" get :index, { 'ident' => "variable"} assert_response :success @@ -42,19 +42,19 @@ class QuickvoteControllerTest < Test::Unit::TestCase end def test_create_quickvote_badname - post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "foobar"]}) + post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => ["foo", "bar", "foobar"]}) assert_template "quickvote/create" end def test_create_quickvote_dupe_candidate - post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "bar", "foobar"]}) + post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => ["foo", "bar", "bar", "foobar"]}) assert_template "quickvote/create" end def test_create_quickvote_nil_candidate - post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => nil}) + post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => nil}) assert_template "quickvote/create" - post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => []}) + post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => []}) assert_template "quickvote/create" end @@ -119,7 +119,7 @@ class QuickvoteControllerTest < Test::Unit::TestCase test_create_quickvote qv=QuickVote.ident_to_quickvote('variable') qv.description="foo" - qv.candidatelist = ["foo", "bar", ""] + qv.candidate_names = ["foo", "bar", ""] qv.save! get :index, { 'ident' => 'variable' } assert_response :success diff --git a/test/unit/candidate_test.rb b/test/unit/candidate_test.rb index b640f0c..86d190e 100644 --- a/test/unit/candidate_test.rb +++ b/test/unit/candidate_test.rb @@ -1,10 +1,8 @@ require File.dirname(__FILE__) + '/../test_helper' class CandidateTest < Test::Unit::TestCase - fixtures :candidates - # Replace this with your real tests. - def test_truth - assert_kind_of Candidate, candidates(:first) + def test_default + assert true end end diff --git a/test/unit/election_test.rb b/test/unit/election_test.rb index 60663cf..90a2d4f 100644 --- a/test/unit/election_test.rb +++ b/test/unit/election_test.rb @@ -1,10 +1,7 @@ require File.dirname(__FILE__) + '/../test_helper' class ElectionTest < Test::Unit::TestCase - fixtures :elections - - # Replace this with your real tests. - def test_truth - assert_kind_of Election, elections(:first) + def test_default + assert true end end diff --git a/test/unit/quickvote_test.rb b/test/unit/quickvote_test.rb index 99274e4..69a7033 100644 --- a/test/unit/quickvote_test.rb +++ b/test/unit/quickvote_test.rb @@ -1,12 +1,38 @@ require File.dirname(__FILE__) + '/../test_helper' class QuickVoteTest < Test::Unit::TestCase - fixtures :quickvote - - def correct_enddate - true + fixtures :elections, :candidates + + def setup + @quickvote_normal = QuickVote.find(1) + end + + def test_create_update_delete_quickvote_from_fixture + assert_kind_of QuickVote, @quickvote_normal + assert_equal 1, @quickvote_normal.id + assert_equal 'acetarium', @quickvote_normal.name + assert_equal 'who is the winner?', @quickvote_normal.description + #assert_equal QuickVote, @quickvote_normal.type + assert_equal 'ssd', @quickvote_normal.election_method + assert_equal '2007-08-22 12:00:00', @quickvote_normal.startdate_before_type_cast + assert_equal '2007-09-21 11:59:59', @quickvote_normal.enddate_before_type_cast + + # make sure that the each of the three andidates + (1..3).each do |i| + assert @quickvote_normal.candidates.include?(Candidate.find(i)) + end + + # update and save + @quickvote_normal.name = 'foobar' + assert @quickvote_normal.save, + @quickvote_normal.errors.full_messages.join("; ") + + @quickvote_normal.reload + assert_equal 'foobar', @quickvote_normal.name + end + + def test_create_invalid_enddate + qv = QuickVote.find(2) + assert_equal qv.save, false, "created vote with invalid enddate" end - - - -end \ No newline at end of file +end diff --git a/test/unit/selectricityservice_test.rb b/test/unit/selectricityservice_test.rb index c0f73c0..305bdc4 100644 --- a/test/unit/selectricityservice_test.rb +++ b/test/unit/selectricityservice_test.rb @@ -9,13 +9,16 @@ class SelectricityServiceTest < Test::Unit::TestCase end def test_list_quickvotes - result= invoke_delegated :vote, :list_quickvotes + result = invoke_delegated :vote, :list_quickvotes assert_instance_of Array, result - assert_equal result.length, 0 + assert result.length > 0 end def test_create_quickvote - election = ElectionStruct.new :name => "TestVote", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"] + election = ElectionStruct.new( + { :name => "TestVote", + :description => "Test Vote", + :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"] }) assert_create_quickvote_succeeds election end @@ -29,10 +32,10 @@ class SelectricityServiceTest < Test::Unit::TestCase def test_cast_nil_quickvote assert_cast_quickvote_fails nil, nil, nil assert_cast_quickvote_fails "foo", nil, nil - assert_cast_quickvote_fails "foo",33, [] + assert_cast_quickvote_fails "foo", 33, [] test_create_quickvote - assert_cast_quickvote_fails "TestVote",42,nil - assert_cast_quickvote_fails "TestVote",nil,[] + assert_cast_quickvote_fails "TestVote", 42,nil + assert_cast_quickvote_fails "TestVote", nil, [] end def test_cast_malformed_votelist -- 2.39.5