Big commit includes:
author<mako@atdot.cc> <>
Thu, 23 Aug 2007 19:45:11 +0000 (15:45 -0400)
committer<mako@atdot.cc> <>
Thu, 23 Aug 2007 19:45:11 +0000 (15:45 -0400)
 * 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.

14 files changed:
app/controllers/quickvote_controller.rb
app/models/election.rb
app/models/quick_vote.rb
app/models/selectricity_service.rb
app/views/quickvote/_candidate_list.rhtml
app/views/quickvote/create.rhtml
config/environments/test.rb
test/fixtures/candidates.yml
test/fixtures/elections.yml
test/functional/quickvote_controller_test.rb
test/unit/candidate_test.rb
test/unit/election_test.rb
test/unit/quickvote_test.rb
test/unit/selectricityservice_test.rb

index 031c0755adb2a7f38b2d8c1b9c7b632778d0c99d..e675638f2af35b619c967560fb32eae5fc3613fd 100644 (file)
@@ -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
index 7343d7204070b374eb8b383751b17e0f8ae29b7c..a574139c535abc4f48b7ba5a210da7d918e5362d 100644 (file)
@@ -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
index 494f7ac364bfb6b7dd53226bce0f138f05c10c60..bd390519331e5e2d62c82d69223e9f5397daf664 100644 (file)
@@ -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
index 196161a467ea2bcfb25f5be2f5cb3d22c6123e21..544b618d7c78fddce9fb5d91f8a3a65cf263264c 100644 (file)
@@ -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
index ddb47e53fa7f6a49b707bcecdb530e5ef0dd1a54..76de0ac59e25c40512a466deb13a0af620ddbdd6 100644 (file)
@@ -1,14 +1,14 @@
 <% %>
 <!-- the list of candidates -->
-<% if flash[:candlist] %>
+<% if flash[:candidate_names] %>
   <ul>
-  <% for cand in flash[:candlist] %>
+  <% for cand in flash[:candidate_names] %>
     <li><%=h cand.capitalize %></li>
   <% end %>
   </ul>
 <% end %>
 
-<% form_remote_tag(:update => 'candlist',
+<% form_remote_tag(:update => 'candidate_names',
                     :url => { :action => 'add_candidate' },
                    :complete => "Field.focus('ajax_newcandidate')") do %>
 <p>
index a7ccd63396e1b8c9ecdeb897188571bb16db47b6..7ccb6443120e8d7f50a1a506176b8b828b9c0b1c 100644 (file)
@@ -3,10 +3,10 @@
 
 <%= error_messages_for 'quickvote' %>
 
-<p><label for="quickvote_candidatelist">Choices</p>
+<p><label for="quickvote_candidate_names">Choices</p>
 
 <!-- the list of candidates -->
-<div id="candlist">
+<div id="candidate_names">
 <%= render :partial => 'candidate_list' %>
 </div>
 
index f0689b924bf935c9ca8cb91c578f2cba521e250c..7c2f617db2b1810fd86d3c15418b607403b991ca 100644 (file)
@@ -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
index 8794d28ae419929e241d892341c7429b84e5c74e..4a651bfcf79dbce6a825c34a302228021a2eacda 100644 (file)
@@ -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
index 8794d28ae419929e241d892341c7429b84e5c74e..814540fcd314c7ede325fa4cde8a5c8f3237ccda 100644 (file)
@@ -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
index 02c133e1508c40c28d7fdd1443f1a916d9ccfa80..ee79b3f50444e3902211b3d72fd5b5b379ecf5de 100644 (file)
@@ -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="<object>foo</object>"
-    qv.candidatelist = ["<object>foo", "bar<object>", "<foobar>"]
+    qv.candidate_names = ["<object>foo", "bar<object>", "<foobar>"]
     qv.save!
     get :index, { 'ident' => 'variable' }
     assert_response :success
index b640f0c17fe662a7bcb9d19196b4df4cc57252df..86d190ef661c4ab92147d548c6382aa7277629f7 100644 (file)
@@ -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
index 60663cfc7d477fa4e4fbf5db66138e2e065ef986..90a2d4faa7fcea1870c2457e28ed10dd7b212407 100644 (file)
@@ -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
index 99274e4b7b2bd1b6c9ec14bb7294afe112eaf96d..69a7033c3487f3b64280a42e4bdc597e27da55c6 100644 (file)
@@ -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
index c0f73c02789236df5c23e8728cc0d79aca329144..305bdc4babed47c4970cafa10cafa1eaa5a052ef 100644 (file)
@@ -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

Benjamin Mako Hill || Want to submit a patch?