From 47320adc1a67ceb4460ab99e98b889933f6bab3b Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Wed, 21 Apr 2010 19:21:17 -0400 Subject: [PATCH] Adding necessary tests to question model --- spec/models/question_spec.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 0db269e..537e7d9 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -6,7 +6,11 @@ describe Question do it {should belong_to :site} it {should have_many :choices} it {should have_many :prompts} + it {should have_many :votes} + it {should have_many :densities} + it {should have_many :appearances} it {should validate_presence_of :site} + it {should validate_presence_of :creator} before(:each) do @aoi_clone = Factory.create(:user, :email => "pius@alum.mit.edu", :password => "password", :password_confirmation => "password", :id => 8) @@ -16,8 +20,6 @@ describe Question do } - # @item1 = Factory.create(:item, :data => "foo", :id => 1, :creator_id => 8) - # @item2 = Factory.create(:item, :data => "bar", :id => 2, :creator_id => 8) end it "should create a new instance given valid attributes" do @@ -37,6 +39,55 @@ describe Question do q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) q.prompts(true).size.should == 2 end + + it "should choose an active prompt randomly" do + q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) + prompt = q.picked_prompt + prompt.active?.should == true + end + + it "should choose an active prompt using catchup algorithm" do + q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) + prompt = q.catchup_choose_prompt + prompt.active?.should == true + end + + context "catchup algorithm" do + before(:all) do + user = Factory.create(:user) + @catchup_q = Factory.create(:aoi_question, :site => user, :creator => user.default_visitor) + + @catchup_q.it_should_autoactivate_ideas = true + @catchup_q.save! + + 100.times.each do |num| + user.create_choice("visitor identifier", @catchup_q, {:data => num, :local_identifier => "exmaple"}) + end + end + it "should choose an active prompt using catchup algorithm on a large number of choices" do + @catchup_q.reload + # Sanity check, 2 extra choices are autocreated when empty question created + @catchup_q.choices.size.should == 102 + + #the catchup algorithm depends on all prompts being generated automatically + @catchup_q.prompts.size.should == 102 **2 - 102 + + prompt = @catchup_q.catchup_choose_prompt + prompt.active?.should == true + end + + it "should have a normalized vector of weights to support the catchup algorithm" do + weights = @catchup_q.catchup_prompts_weights + sum = 0 + weights.each{|k,v| sum+=v} + + (sum - 1.0).abs.should < 0.000001 + end + + + + end + #q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) end -- libgit2 0.21.2