X-Git-Url: https://projects.mako.cc/source/selectricity-live/blobdiff_plain/905cf65f6b3e8acc894c268c3bf729e7b0c76292..f55c18a56705cbe340a1234d9083ad4e1055f0d4:/test/unit/quickvote_test.rb diff --git a/test/unit/quickvote_test.rb b/test/unit/quickvote_test.rb index 69a7033..83a0cdb 100644 --- a/test/unit/quickvote_test.rb +++ b/test/unit/quickvote_test.rb @@ -4,35 +4,105 @@ class QuickVoteTest < Test::Unit::TestCase 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)) + # 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 - @quickvote_normal.name = 'foobar' - assert @quickvote_normal.save, - @quickvote_normal.errors.full_messages.join("; ") + @qv.name = 'foobar' + assert @qv.save, + @qv.errors.full_messages.join("; ") - @quickvote_normal.reload - assert_equal 'foobar', @quickvote_normal.name + @qv.reload + assert_equal 'foobar', @qv.name end def test_create_invalid_enddate - qv = QuickVote.find(2) - assert_equal qv.save, false, "created vote with 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