X-Git-Url: https://projects.mako.cc/source/selectricity-live/blobdiff_plain/823d530ba46c1fb3518c4deef0081738503837eb..b436d3fd0e673b4f5bcc01f0e7cce39ff0e40f82:/test/unit/quickvote_test.rb?ds=sidebyside diff --git a/test/unit/quickvote_test.rb b/test/unit/quickvote_test.rb index 99274e4..83a0cdb 100644 --- a/test/unit/quickvote_test.rb +++ b/test/unit/quickvote_test.rb @@ -1,12 +1,108 @@ require File.dirname(__FILE__) + '/../test_helper' class QuickVoteTest < Test::Unit::TestCase - fixtures :quickvote - - def correct_enddate - true - end - - - -end \ No newline at end of file + fixtures :elections, :candidates + + def setup + # create the quickvote + @qv = QuickVote.find(1) + + # create candidates + @candidates = ['mako', 'mika', 'bettamax'] + @candidates.each do |name| + @qv.candidates << Candidate.new({:name => name}) + end + @qv.save + end + + def test_add_remove_candidates + # make sure the names are right + assert_equal 3, @qv.candidates.length + assert_equal @candidates.sort, + @qv.candidates.collect {|c| c.name}.sort + + # add one candidate + @qv.candidates << Candidate.new({:name => 'erik'}) + assert @qv.save + assert @qv.reload + assert_equal 4, @qv.candidates.length + assert_equal (@candidates + ['erik']).sort, + @qv.candidates.collect {|c| c.name}.sort + + # drop erik and try again + @qv.candidates = \ + @qv.candidates.select {|c| c.name != 'erik'} + assert @qv.save + assert @qv.reload + assert_equal 3, @qv.candidates.length + assert_equal @candidates.sort, + @qv.candidates.collect {|c| c.name}.sort + end + + def test_create_update_quickvote + assert_kind_of QuickVote, @qv + assert_equal 1, @qv.id + assert_equal 'acetarium', @qv.name + assert_equal 'who is the winner?', @qv.description + assert_equal 'ssd', @qv.election_method + assert_equal '2007-08-22 12:00:00', @qv.startdate_before_type_cast + assert_equal '2010-09-21 11:59:59', @qv.enddate_before_type_cast + + # should be set by default default results + assert_equal false, @qv.early_results + assert_equal false, @qv.kiosk + assert_equal true, @qv.authenticated + assert_equal false, @qv.embeddable + assert_equal true, @qv.verifiable + + # update and save + @qv.name = 'foobar' + assert @qv.save, + @qv.errors.full_messages.join("; ") + + @qv.reload + assert_equal 'foobar', @qv.name + end + + def test_create_invalid_enddate + @qv.enddate = '2003-09-21 11:59:59 ' + assert_equal @qv.save, false, "created vote with invalid enddate" + assert @qv.reload + end + + def test_add_voters + 50.times do + voter = QuickVoter.new + voter.election = @qv + voter.session_id = UniqueTokenGenerator.new(20).token + + voter.vote = Vote.new + assert voter.save + voter.vote.set_defaults! + assert_nil voter.vote.confirm! + assert voter.reload + end + assert @qv.save + assert_equal 50, @qv.voters.length + assert_equal 50, @qv.voters.collect {|v| v.vote}.length + assert_equal 50, @qv.voters.select {|v| v.vote.confirmed?}.length + end + + def test_build_results + end + + def test_bernie_qv + assert qvb = QuickVote.find(954) + assert_kind_of QuickVote, qvb + assert_equal 17, qvb.candidates.length + end + + def test_for_reload_errors + qvb = QuickVote.find(954) + qvb.save + qvb.reload + + assert_equal (4666..4682).to_a, qvb.candidates.collect {|c| c.id}.sort + end + +end