Merged from Mako.
author<jlsharps@mit.edu> <>
Mon, 20 Aug 2007 17:38:35 +0000 (13:38 -0400)
committer<jlsharps@mit.edu> <>
Mon, 20 Aug 2007 17:38:35 +0000 (13:38 -0400)
app/apis/selectricity_api.rb
app/controllers/election_controller.rb
app/controllers/quickvote_controller.rb
app/controllers/site_controller.rb
app/controllers/voter_controller.rb
app/models/selectricity_service.rb
app/views/quickvote/results.rhtml
lib/rubyvote/condorcet.rb
lib/rubyvote/election.rb
test/unit/selectricityservice_test.rb

index b1210c6e861eb9b02c1d8a7693ee5885241d5986..9c95369d9a798917ee68babb9d2f578c0a6b3b04 100644 (file)
@@ -7,6 +7,7 @@ class ElectionStruct < ActionWebService::Struct
 end
 class VoteInfo < ActionWebService::Struct
   member :voter_id, :int
+  member :voter_session_id, :string
   member :voter_ipaddress, :string
   member :vote_time, :int
   member :vote, [:int]
@@ -18,12 +19,10 @@ class VoteResultStruct < ActionWebService::Struct
   member :condorcet_winners, [:int]
   member :ssd_winners, [:int]
   member :borda_winners, [:int]
-  member :errors, [:string]
 end
 class CandidateMap < ActionWebService::Struct
   member :candidate_ids, [:int]
   member :candidate_names, [:string]
-  member :errors, [:string]
 end
 class SelectricityAPI < ActionWebService::API::Base
   api_method :cast_quickvote, :expects => [:string, :int, [[:int]]], :returns => [:string]
index df0aa7c53631ab968b7d2f6bf963172645ae4f3f..d3332948b4398a197ca8280d69ec8db351ea7be2 100644 (file)
@@ -1,5 +1,8 @@
 class ElectionController < ApplicationController
-  model :raw_voter_list, :voter, :vote, :candidate
+  require_dependency "raw_voter_list"
+  require_dependency "voter"
+  require_dependency "vote"
+  require_dependency "candidate"
   layout 'main'
 
   #before_filter :login_required
index 8b853ebd1a70274a5562a9d04c46ab4101153a25..41d0dc3616050ef909e39ebbcaa2eef7363da12f 100644 (file)
@@ -1,9 +1,9 @@
 class QuickvoteController < ApplicationController
   layout 'main'
-  model :quick_voter
-  model :quick_vote
-  model :vote
-  model :election
+  require_dependency "quick_voter"
+  require_dependency "quick_vote"
+  require_dependency "vote"
+  require_dependency "election"
 
   #############################################################
   # the following methods pertain to creating quickvotes
index a33a1fa16707403589ce4caebd0bb36161bb30d9..80fdfc406c3247b16651ae5c6b0bada8c2d35625 100644 (file)
@@ -1,6 +1,8 @@
 class SiteController < ApplicationController
   layout 'main'
-  model :user, :election, :account
+  require_dependency "user"
+  require_dependency "election"
+  require_dependency "account"
 
   def index
     @quickvotes = QuickVote.find(:all).sort {|a,b| b.enddate <=> a.enddate}[0..1]
index e94f4a76f0faed8e1994d30356c41fef851b0387..f4e0e7e7d782e9d67c13518d15758f616b8c9764 100644 (file)
@@ -1,8 +1,8 @@
 class VoterController < ApplicationController
   layout 'main'
-  model :voter
-  model :vote
-  model :election
+  require_dependency "voter"
+  require_dependency "vote"
+  require_dependency "election"
 
   def index
     password = params[:id]
index 6d338dee9905d11edd407aebdd2e9ad72f1f2d16..196161a467ea2bcfb25f5be2f5cb3d22c6123e21 100644 (file)
@@ -28,7 +28,7 @@ class SelectricityService < ActionWebService::Base
   def quickvote_candidate_ids_to_names(shortname, id_list)
     qv=QuickVote.ident_to_quickvote(shortname)
     candidates={}
-    return [] unless qv
+    raise ArgumentError.new("Quickvote by name #{shortname} doesn't exist") unless qv
     qv.results
     qv.candidates.each {|c| candidates[c.id] = c}
     results=[]
@@ -46,10 +46,8 @@ class SelectricityService < ActionWebService::Base
     #TODO: Validate shortname
     qv=QuickVote.ident_to_quickvote(shortname)
     result=VoteResultStruct.new
-    result.errors=[]
     unless qv
-      result.errors << "No quickvote with name #{shortname} found!"
-      return result
+      raise ArgumentError.new("No quickvote with name #{shortname} found!")
     end
     qv.results
     result.plurality_winners=qv.plurality_result.winners
@@ -62,10 +60,8 @@ class SelectricityService < ActionWebService::Base
   def get_quickvote_candidate_map(shortname)
     qv=QuickVote.ident_to_quickvote(shortname)
     result=CandidateMap.new
-    result.errors=[]
     unless qv
-      result.errors << "No quickvote with name #{shortname} found!"
-      return result
+      raise ArgumentError.new("No quickvote with name #{shortname} found!")
     end
     candidates={}
     qv.candidates.each {|c| candidates[c.id] = c.name}
@@ -77,10 +73,10 @@ class SelectricityService < ActionWebService::Base
     qv=QuickVote.ident_to_quickvote(shortname)
     votes=Array.new
     unless qv
-      return result
+      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)
+      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 )
     end
     return votes
   end
@@ -92,7 +88,7 @@ class SelectricityService < ActionWebService::Base
     return all
   end
   def get_quickvote(shortname)
-    return ElectionStruct.new unless election=QuickVote.ident_to_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 } )
   end
   def create_quickvote(election)
@@ -101,7 +97,7 @@ class SelectricityService < ActionWebService::Base
     if qv.save
       return ""
     else
-      return "Saving quickvote FAILED:"+qv.errors.inspect
+      raise ArgumentError.new("Saving quickvote FAILED:"+qv.errors.inspect)
     end
   end
 
index c49d68201a48e10363c14811cad171a6abe6dd39..bc33d072f4379774a58335b0a26d67507bad47de 100644 (file)
@@ -179,7 +179,7 @@ by several other names.</p>
 <%= render :partial => 'victories_ties' %>
 <%= render :partial => 'pref_table' %>
 
-<%= image_tag( graph_url( :action => 'votes_per_day', :id => @election ) ) %><br />
-<%= image_tag( graph_url( :action => 'votes_per_interval', :id => @election ))%><br />
+<%=image_tag( graph_url( :action => 'votes_per_interval', :id => @election ))%>
+<br />
 <%= image_tag( graph_url( :action => 'borda_bar', :id => @election ) ) %><br />
 <%= image_tag( graph_url( :action => 'choices_positions', :id => @election ) ) %>
index 7fda91b48073a0308c8040ddba03ac13d8822929..d0210efe19cfbf4fd09f9feb9dc82e4678583b74 100644 (file)
@@ -33,8 +33,6 @@
 
 class CondorcetVote < ElectionVote
   
-  attr_accessor :results
-
   def initialize(votes=nil)
     unless defined?(@candidates)
       @candidates = Array.new
@@ -46,7 +44,6 @@ class CondorcetVote < ElectionVote
       end
     end
     super(votes)
-    @results = Array.new
   end
 
   def tally_vote(vote=nil)
@@ -86,53 +83,24 @@ class CondorcetVote < ElectionVote
     end
   end
 
-  def results
-    if @results.size < 2 && (not @candidates.empty?)
-      tabulate
-    end
-    @results
-  end
-
-  def result
-    find_only_winner unless @winner
-    @winner
-  end
-
   protected
 
   def verify_vote(vote=nil)
     vote.instance_of?( Array ) and
       vote == vote.uniq
   end
-
-  def tabulate
-    find_only_winner unless @winner
-    until @candidates.empty? 
-      aResult = resultFactory( self )
-      @results << aResult.winners
-      filter_out(aResult)
-    end
-  end
-
-  def find_only_winner
-    @winner = resultFactory( self )
-    @results << @winner.winners
-    filter_out(@winner)
-  end
-
 end
 
 class PureCondorcetVote < CondorcetVote
-  def resultFactory(init)
-    PureCondorcetResult.new(init)
+  def result
+    PureCondorcetResult.new(self)
   end
 end
 
 class CloneproofSSDVote < CondorcetVote
-  def resultFactory(init)
-    CloneproofSSDResult.new(init)
+  def result
+    CloneproofSSDResult.new(self)
   end
-
 end
 
 
@@ -170,8 +138,8 @@ class CondorcetResult < ElectionResult
           ties << [candidate, challenger] 
         end
       end
-    end
-     
+    end  
+    
     victors.each do |list|
       if victories.has_key?(list[0])
         victories[list[0]][list[1]] = list[2]       
@@ -183,6 +151,14 @@ class CondorcetResult < ElectionResult
     
     return victories, ties    
   end
+
+  def ranked_candidates
+    if not defined?(@ranked_candidates)
+      @ranked_candidates = build_ranked_candidates()
+    end
+
+    @ranked_candidates
+  end
         
   protected
   def defeats(candidates=nil, votes=nil)
@@ -203,6 +179,27 @@ class CondorcetResult < ElectionResult
 
     defeats
   end
+  
+  def build_ranked_candidates
+    # build a lis of ranked candidates by dropping the winner and
+    # cursing
+
+    ranked_candidates = []
+
+    resultobj = self.dup
+    candidates = self.election.candidates
+
+    until candidates.empty? 
+      ranked_candidates << resultobj.winners
+      
+      new_voteobj = resultobj.election.dup
+      candidates = new_voteobj.candidates
+      new_voteobj.candidates.delete_if {|x| resultobj.winners.include?(x)}
+      resultobj = new_voteobj.result
+    end
+
+    ranked_candidates
+  end
 
 end
 
index 5a5824341a57c1f4c9f6aef7ec14ed41f40dc693..3655a034e7575d7be3541ab49e1be12a80fb0c17 100644 (file)
@@ -66,10 +66,6 @@ class ElectionVote
     self.verify_vote(vote)
   end
 
-  def filter_out(winner)
-    @candidates.delete_if {|x| winner.winners.include?(x)}
-  end
-
 end
 
 class PluralityVote < ElectionVote
@@ -118,6 +114,7 @@ end
 
 class ElectionResult
   attr_reader :winners
+  attr_reader :election
 
   def initialize(voteobj=nil)
     unless voteobj and voteobj.kind_of?( ElectionVote )
@@ -135,7 +132,7 @@ class ElectionResult
   def winner?
     @winners.length > 0 and not @winners[0].nil?
   end
-
+  
 end
 
 class PluralityResult < ElectionResult
index 74b26d45913d25ff52aec6224879d2814a272299..c0f73c02789236df5c23e8728cc0d79aca329144 100644 (file)
@@ -13,58 +13,104 @@ class SelectricityServiceTest < Test::Unit::TestCase
     assert_instance_of Array, result
     assert_equal result.length, 0
   end
+
   def test_create_quickvote
     election = ElectionStruct.new :name => "TestVote", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_succeeds election
   end
+
   def test_cast_quickvote
     test_create_quickvote
     election = invoke_delegated :vote, :get_quickvote, "TestVote"
     casted_vote = election.candidate_ids.sort_by {rand} #Shuffles
-    invoke_delegated :vote, :cast_quickvote, "TestVote", 42, [casted_vote]
-    quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
-    assert_equal quickvote_votes.length, 1
-    assert_equal quickvote_votes[0].vote, casted_vote
+    assert_cast_quickvote_succeeds "TestVote", 42, [casted_vote]
+  end
+
+  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, []
+    test_create_quickvote
+    assert_cast_quickvote_fails "TestVote",42,nil
+    assert_cast_quickvote_fails "TestVote",nil,[]
+  end
+
+  def test_cast_malformed_votelist
+    test_create_quickvote
+    election = invoke_delegated :vote, :get_quickvote, "TestVote"
+    assert_cast_quickvote_fails "TestVote", 11, [election.candidate_ids[0]]
+    assert_cast_quickvote_fails "TestVote", 11, [1,2]
+    assert_cast_quickvote_fails "TestVote", 11, [election.candidate_ids[0],election.candidate_ids[0], election.candidate_ids[1], election.candidate_ids[1], election.candidate_ids[2]]
+  end
+
+  def test_get_nonexistent_quickvote
+    assert_raises ArgumentError do
+      qv = invoke_delegated :vote, :get_quickvote, "asdfasdfasdf"
+    end
+  end
+
+  def test_get_voters_nonexistent_quickvote
+    assert_raises(ArgumentError) {invoke_delegated :vote, :get_quickvote_votes, "asdfasdf"}
+  end
+
+  def test_get_candidate_map_nonexistent_quickvote
+    assert_raises(ArgumentError) { invoke_delegated :vote, :get_quickvote_candidate_map, "asdfasdf"}
   end
+
   def test_cast_mass_quickvote
     test_create_quickvote
     election = invoke_delegated :vote, :get_quickvote, "TestVote"
     20.times do |t|
       casted_vote = election.candidate_ids.sort_by {rand}
-      invoke_delegated :vote, :cast_quickvote, "TestVote", t, [casted_vote]
+      assert_cast_quickvote_succeeds "TestVote", t, [casted_vote]
     end
-    quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
-    assert_equal quickvote_votes.length, 20
   end
+
+  def test_cast_quickvote_nonexistent
+    assert_cast_quickvote_fails "ASDFJOFASF", "me", [1,2,3]
+  end
+
+  def test_cast_quickvote_nonexistent_candidates
+    test_create_quickvote
+    election = invoke_delegated :vote, :get_quickvote, "TestVote"
+    assert_cast_quickvote_fails "TestVote", 42, [123,342314,5342,1,1,2]
+  end
+
   def test_create_mass_quickvote
     10.times do |t|
       election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
       assert_create_quickvote_succeeds election
     end
   end
+
   def test_create_quickvote_bad_name
     election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
   end
+
   def test_create_quickvote_nil
     election =  ElectionStruct.new
     assert_create_quickvote_fails election
   end
+
   def test_create_quickvote_name_nil
     election = ElectionStruct.new :name => "", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
   end
+
   def test_create_quickvote_description_nil
     election = ElectionStruct.new :name => "foobar", :description => nil, :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
     
   end
+  
   def test_create_quickvote_description_whitespace
     election = ElectionStruct.new :name => "foobar", :description => "       ", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
     election = ElectionStruct.new :name => "foobar", :description => "\t\t", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
   end
+  
   def test_create_quickvote_candidates_nil
     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
     assert_create_quickvote_fails election
@@ -110,42 +156,65 @@ class SelectricityServiceTest < Test::Unit::TestCase
     end
     25.times do |t|
       vote = [candidates["jdong"], candidates["mako"], candidates["justin"]]
-      invoke_delegated :vote, :cast_quickvote, "favdev", "1:#{t}", [vote]
+      assert_cast_quickvote_succeeds "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]
+      assert_cast_quickvote_succeeds "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]
+      assert_cast_quickvote_succeeds "favdev", "3:#{t}", [vote]
     end
-    results=invoke_delegated(:vote, :get_quickvote_results, "favdev")
+    results=nil
+    assert_nothing_raised {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_cast_quickvote_succeeds(shortname, id, vote)
+    assert_nothing_raised do
+      old_votes = invoke_delegated(:vote, :get_quickvote_votes, shortname)
+      invoke_delegated(:vote, :cast_quickvote, shortname, id, vote)
+
+      new_votes = invoke_delegated(:vote, :get_quickvote_votes, shortname)
+      assert_equal(old_votes.length, new_votes.length - 1)
+
+      reflection = new_votes.find_all { |v| v.voter_session_id == "XMLRPC:#{id}" }
+      assert_not_nil(reflection)
+      assert_equal(reflection.length, 1)
+      assert_equal(reflection[0].vote, vote[0])
+    end
+  end
+
+  def assert_cast_quickvote_fails(shortname, id, vote)
+    assert_raise Test::Unit::AssertionFailedError do
+      assert_cast_quickvote_succeeds(shortname, id, vote)
+     end
+  end
+
   def assert_create_quickvote_succeeds(election)
-    # Checks if a created quickvote is identical when retrieved
-    old_len=invoke_delegated(:vote,:list_quickvotes).length
-    result = invoke_delegated :vote, :create_quickvote, election
-    assert_equal result, ""
-    reflection = invoke_delegated :vote, :get_quickvote, election.name
-    assert_equal election.description, reflection.description
-    assert_equal 0, election.name.casecmp(reflection.name)
-    assert_equal election.candidate_names, reflection.candidate_names
-    assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len+1)
+    # checks if a created quickvote is identical when retrieved
+    assert_nothing_raised do
+      old_len = invoke_delegated(:vote, :list_quickvotes).length
+      result = invoke_delegated(:vote, :create_quickvote, election)
+      assert_equal(result, "")
+
+      reflection = invoke_delegated(:vote, :get_quickvote, election.name)
+      assert_equal(election.description, reflection.description)
+      assert_equal(0, election.name.casecmp(reflection.name))
+      assert_equal(election.candidate_names, reflection.candidate_names)
+      assert_equal(invoke_delegated(:vote, :list_quickvotes).length, old_len+1)
+    end
   end
+  
   def assert_create_quickvote_fails(election)
-    # Helper function to check that creating this quickvote fails
-    old_len=invoke_delegated(:vote,:list_quickvotes).length
-    result = invoke_delegated :vote, :create_quickvote, election
-    assert_instance_of String, result
-    assert_not_equal result.length, 0
-    assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len)
+    assert_raise Test::Unit::AssertionFailedError do
+      assert_create_quickvote_succeeds(election)
+    end
   end
 end

Benjamin Mako Hill || Want to submit a patch?