From: John Dong Date: Fri, 17 Aug 2007 22:00:31 +0000 (-0400) Subject: Merge from jdong; all testcases should succeed at this point, and all deprecations... X-Git-Url: https://projects.mako.cc/source/selectricity-live/commitdiff_plain/1620856a8a47219f3a2cbb5288137b11467886fc?hp=f0a07fead89d29513f76ee42b29a0f87dbe926d6 Merge from jdong; all testcases should succeed at this point, and all deprecations should be gone --- diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 8c6f8ae..9b48030 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -4,6 +4,6 @@ class ApplicationController < ActionController::Base include AuthenticatedSystem helper :user - model :user + require_dependency "user" end diff --git a/app/controllers/quickvote_controller.rb b/app/controllers/quickvote_controller.rb index e327ed6..8b853eb 100644 --- a/app/controllers/quickvote_controller.rb +++ b/app/controllers/quickvote_controller.rb @@ -59,7 +59,7 @@ class QuickvoteController < ApplicationController # look to see that the voter has been created and has voted in # this election, and has confirmed their vote - @voter = QuickVoter.find_all(["session_id = ? and election_id = ?", + @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 @@ -92,7 +92,7 @@ class QuickvoteController < ApplicationController election = QuickVote.ident_to_quickvote(params[:ident]) # find out who the voter is for this election - @voter = QuickVoter.find_all(["session_id = ? and election_id = ?", + @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?", session.session_id, election.id])[0] if not @voter @@ -122,7 +122,7 @@ class QuickvoteController < ApplicationController end def change - voter = QuickVoter.find_all(["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 diff --git a/app/controllers/voter_controller.rb b/app/controllers/voter_controller.rb index 779e83f..e94f4a7 100644 --- a/app/controllers/voter_controller.rb +++ b/app/controllers/voter_controller.rb @@ -7,7 +7,7 @@ class VoterController < ApplicationController def index password = params[:id] password = params[:vote][:password] if params[:vote] - if @voter = FullVoter.find_all( [ "password = ?", password ] )[0] + if @voter = FullVoter.find(:all, :conditions => [ "password = ?", password ] )[0] render :action => 'fullvote' end end @@ -41,7 +41,7 @@ class VoterController < ApplicationController private def authenticate password = params[:id] - @voter = FullVoter.find_all( [ "password = ?", password ] )[0] + @voter = FullVoter.find(:all, :conditions => [ "password = ?", password ] )[0] end end diff --git a/app/models/election.rb b/app/models/election.rb index cf7bba5..3baeef0 100644 --- a/app/models/election.rb +++ b/app/models/election.rb @@ -57,7 +57,7 @@ class Election < ActiveRecord::Base end def quickvote? - type == 'QuickVote' + self.class == 'QuickVote' end def active? diff --git a/app/models/full_voter.rb b/app/models/full_voter.rb index 04071a0..980fb5c 100644 --- a/app/models/full_voter.rb +++ b/app/models/full_voter.rb @@ -5,7 +5,7 @@ class FullVoter < Voter def create_password token_generator = UniqueTokenGenerator.new( 16 ) until password and not password.empty? \ - and Voter.find_all( [ "password = ?", password ]).empty? + and Voter.find(:all, :conditions => [ "password = ?", password ]).empty? self.password = token_generator.token end end diff --git a/app/models/quick_vote.rb b/app/models/quick_vote.rb index ad72717..698dac3 100644 --- a/app/models/quick_vote.rb +++ b/app/models/quick_vote.rb @@ -12,9 +12,23 @@ class QuickVote < Election def validate if not @raw_candidates or @raw_candidates.length < 2 - errors.add(nil, "You must list at least two candidates.") + errors.add(nil, "You must list at least two candidates.") end + @raw_candidates.each do |c| + unless c.instance_of? String + errors.add(nil, "Candidates must be strings") + next + end + c.strip! + if c.length == 0 + 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! + if name =~ /[^A-Za-z0-9]/ errors.add(:name, "must only include numbers and letters.") end @@ -88,7 +102,7 @@ class QuickVote < Election if ident.match(/^\d+$/) quickvote = QuickVote.find(ident) else - quickvote = QuickVote.find_all(["name = ?", ident])[0] + quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0] end return quickvote diff --git a/app/models/selectricity_service.rb b/app/models/selectricity_service.rb index 9e276ef..6d338de 100644 --- a/app/models/selectricity_service.rb +++ b/app/models/selectricity_service.rb @@ -7,10 +7,10 @@ class SelectricityService < ActionWebService::Base if election candidates=election.candidates.collect { |c| c.id } vote_list[0].each do |vote| - raise ArgumentError.new "Invalid Candidate ID #{vote}" unless candidates.index(vote) + raise ArgumentError.new("Invalid Candidate ID #{vote}") unless candidates.index(vote) end - raise ArgumentError.new "You must rank all candidates" unless candidates.length <= vote_list[0].length - raise ArgumentError.new "Please rank each candidate only once" if vote_list[0].uniq! + raise ArgumentError.new("You must rank all candidates") unless candidates.length <= vote_list[0].length + raise ArgumentError.new("Please rank each candidate only once") if vote_list[0].uniq! voter = QuickVoter.new voter.election = election voter.ipaddress = "XMLRPC Request" @@ -22,7 +22,7 @@ class SelectricityService < ActionWebService::Base voter.vote.confirm! voter.save! else - raise ArgumentError.new "Cannot find election #{election_name}" + raise ArgumentError.new("Cannot find election #{election_name}") end end def quickvote_candidate_ids_to_names(shortname, id_list) @@ -86,14 +86,14 @@ class SelectricityService < ActionWebService::Base end def list_quickvotes() all=Array.new - QuickVote.find_all.each do |election| + QuickVote.find(:all).each do |election| all << get_quickvote(election.name) end return all end def get_quickvote(shortname) return ElectionStruct.new 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 } ) + 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 } ) end def create_quickvote(election) qv=QuickVote.new(:name => election.name, :description => election.description) diff --git a/app/models/token.rb b/app/models/token.rb index 2bab513..4ed9598 100644 --- a/app/models/token.rb +++ b/app/models/token.rb @@ -5,7 +5,7 @@ class Token < ActiveRecord::Base super token_generator = UniqueTokenGenerator.new( 16 ) - until not token.empty? and Token.find_all( [ "token = ?", token ]).empty? + until not token.empty? and Token.find(:all, :conditions => [ "token = ?", token ]).empty? self.token = token_generator.token end diff --git a/test/unit/selectricityservice_test.rb b/test/unit/selectricityservice_test.rb index 877f7e9..74b26d4 100644 --- a/test/unit/selectricityservice_test.rb +++ b/test/unit/selectricityservice_test.rb @@ -100,6 +100,34 @@ class SelectricityServiceTest < Test::Unit::TestCase election = ElectionStruct.new :name => "foobar", :description => "test \x01\x02\x03\x04\x05\x06\x07\x08", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"] assert_create_quickvote_succeeds election end + def test_quickvote_proper_results + election = ElectionStruct.new :name => "favdev", :description => "Who is your favorite developer?", :candidate_names => ["mako", "jdong", "justin"] + assert_create_quickvote_succeeds election + reflection = invoke_delegated :vote, :get_quickvote, "favdev" + candidates = {} + reflection.candidate_names.each_with_index do |name, index| + candidates[name] = reflection.candidate_ids[index] + end + 25.times do |t| + vote = [candidates["jdong"], candidates["mako"], candidates["justin"]] + invoke_delegated :vote, :cast_quickvote, "favdev", "1:#{t}", [vote] + end + 5.times do |t| + vote = [candidates["mako"], candidates["justin"], candidates["jdong"]] + invoke_delegated :vote, :cast_quickvote, "favdev", "2:#{t}", [vote] + end + 10.times do |t| + vote = [candidates["justin"], candidates["mako"], candidates["jdong"]] + invoke_delegated :vote, :cast_quickvote, "favdev", "3:#{t}", [vote] + end + results=invoke_delegated(:vote, :get_quickvote_results, "favdev") + assert_equal results.approval_winners, [candidates["mako"]] + assert_equal results.borda_winners, [candidates["jdong"]] + assert_equal results.plurality_winners, [candidates["jdong"]] + assert_equal results.condorcet_winners, [candidates["jdong"]] + assert_equal results.ssd_winners, [candidates["jdong"]] + assert_equal results.errors.length, 0 + end private def assert_create_quickvote_succeeds(election) # Checks if a created quickvote is identical when retrieved