From 681d8a6a8c23b28629c6bdf5d5ac307cc85f60c1 Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Mon, 21 Jun 2010 12:08:46 -0400 Subject: [PATCH] Improving choices test coverage --- app/controllers/choices_controller.rb | 2 +- app/models/choice.rb | 14 +------------- spec/models/choice_spec.rb | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 74 insertions(+), 28 deletions(-) diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index dc0fa90..50ba099 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -38,7 +38,7 @@ class ChoicesController < InheritedResources::Base show! do |format| format.xml { @choice.reload - render :xml => @choice.to_xml(:methods => [:item_data, :wins_plus_losses, :question_name])} + render :xml => @choice.to_xml(:methods => [:wins_plus_losses])} format.json { render :json => @choice.to_json(:methods => [:data])} end end diff --git a/app/models/choice.rb b/app/models/choice.rb index e04a059..7ab2fc8 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -22,14 +22,6 @@ class Choice < ActiveRecord::Base end #attr_accessor :data - def question_name - question.name - end - - def item_data - item.data - end - def lose! self.loss_count += 1 rescue (self.loss_count = 1) self.score = compute_score @@ -46,6 +38,7 @@ class Choice < ActiveRecord::Base wins + losses end + # TODO delete these and refactor loss_count and votes_count into losses and wins def losses loss_count || 0 end @@ -110,11 +103,6 @@ class Choice < ActiveRecord::Base self.save! end - def suspend! - (self.active = false) - self.save! - end - def deactivate! (self.active = false) self.save! diff --git a/spec/models/choice_spec.rb b/spec/models/choice_spec.rb index c9ecd6c..54348a4 100644 --- a/spec/models/choice_spec.rb +++ b/spec/models/choice_spec.rb @@ -4,13 +4,19 @@ describe Choice do it {should belong_to :question} it {should belong_to :item} + #uncomment after adding creator to choice model + #it {should belong_to :creator} it {should have_many :flags} + it {should have_many :votes} + it {should have_many :prompts_on_the_left} + it {should have_many :prompts_on_the_right} it {should validate_presence_of :question} + it {should validate_presence_of :creator} before(:each) do @aoi_clone = Factory.create(:email_confirmed_user) @visitor= Factory.create(:visitor, :site => @aoi_clone) - @question = Question.create(:name => 'which do you like better?', + @question = Factory.create(:aoi_question, :name => "Which do you like better?", :site => @aoi_clone, :creator => @visitor) @@ -25,13 +31,6 @@ describe Choice do Choice.create!(@valid_attributes) end - #it "should generate prompts after two choices are created" do - # proc { -# choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) -# choice2 = Choice.create!(@valid_attributes.merge(:data => '1234')) -# }.should change(@question.prompts, :count).by(2) -# end - it "should deactivate a choice" do choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) choice1.deactivate! @@ -39,12 +38,14 @@ describe Choice do end it "should update a question's counter cache on creation" do - @question.choices_count.should == 0 - @question.choices.size.should == 0 - Choice.create!(@valid_attributes.merge(:data => '1234')) - @question.reload - @question.choices_count.should == 1 - @question.choices.size.should == 1 + # not an allour ideas question + question = Factory.create(:question, :site => @aoi_clone, :creator => @visitor) + question.choices_count.should == 0 + question.choices.size.should == 0 + Choice.create!(@valid_attributes.merge(:question => question)) + question.reload + question.choices_count.should == 1 + question.choices.size.should == 1 end @@ -67,4 +68,61 @@ describe Choice do @question.reload @question.inactive_choices_count.should == prev_inactive + 1 end + it "should have a default score of 50" do + choice1 = Choice.create!(@valid_attributes) + choice1.score.should == 50 + end + it "correctly compute a score based on wins and losses" do + choice1 = Choice.create!(@valid_attributes) + choice1.votes_count = 30 + choice1.loss_count = 70 + choice1.compute_score.should be_close(30,1) + end + it "compute score and save" do + choice1 = Choice.create!(@valid_attributes) + choice1.score.should == 50 + choice1.votes_count= 30 + choice1.loss_count = 70 + choice1.compute_score! + choice1.score.should be_close(30, 1) + end + + it "determines whether a choice is admin created" do + admin_choice = @question.choices.first + admin_choice.user_created.should be_false + end + it "determines whether a choice is user created" do + new_visitor = Factory.create(:visitor, :site => @aoi_clone) + user_choice = Factory.create(:choice, :question => @question, :creator => new_visitor) + user_choice.user_created.should be_true + end + + describe "voting updates things" do + before do + @prompt = @question.choose_prompt + @winning_choice = @prompt.left_choice + @losing_choice = @prompt.right_choice + @winning_choice.win! + @losing_choice.lose! + end + + it "should update score on a win" do + @winning_choice.score.should be_close(67, 1) + end + it "should update score on a loss" do + @losing_choice.score.should be_close(33,1) + end + it "should update win count on a win" do + @winning_choice.votes_count.should == 1 + @winning_choice.wins.should == 1 + @winning_choice.loss_count.should == 0 + @winning_choice.losses.should == 0 + end + it "should update loss count on a loss" do + @losing_choice.votes_count.should == 0 + @losing_choice.wins.should == 0 + @losing_choice.loss_count.should == 1 + @losing_choice.losses.should == 1 + end + end end -- libgit2 0.21.2