Commit 47320adc1a67ceb4460ab99e98b889933f6bab3b
1 parent
299dd86d
Exists in
master
and in
1 other branch
Adding necessary tests to question model
Showing
1 changed file
with
53 additions
and
2 deletions
Show diff stats
spec/models/question_spec.rb
... | ... | @@ -6,7 +6,11 @@ describe Question do |
6 | 6 | it {should belong_to :site} |
7 | 7 | it {should have_many :choices} |
8 | 8 | it {should have_many :prompts} |
9 | + it {should have_many :votes} | |
10 | + it {should have_many :densities} | |
11 | + it {should have_many :appearances} | |
9 | 12 | it {should validate_presence_of :site} |
13 | + it {should validate_presence_of :creator} | |
10 | 14 | |
11 | 15 | before(:each) do |
12 | 16 | @aoi_clone = Factory.create(:user, :email => "pius@alum.mit.edu", :password => "password", :password_confirmation => "password", :id => 8) |
... | ... | @@ -16,8 +20,6 @@ describe Question do |
16 | 20 | |
17 | 21 | } |
18 | 22 | |
19 | - # @item1 = Factory.create(:item, :data => "foo", :id => 1, :creator_id => 8) | |
20 | - # @item2 = Factory.create(:item, :data => "bar", :id => 2, :creator_id => 8) | |
21 | 23 | end |
22 | 24 | |
23 | 25 | it "should create a new instance given valid attributes" do |
... | ... | @@ -37,6 +39,55 @@ describe Question do |
37 | 39 | q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) |
38 | 40 | q.prompts(true).size.should == 2 |
39 | 41 | end |
42 | + | |
43 | + it "should choose an active prompt randomly" do | |
44 | + q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) | |
45 | + prompt = q.picked_prompt | |
46 | + prompt.active?.should == true | |
47 | + end | |
48 | + | |
49 | + it "should choose an active prompt using catchup algorithm" do | |
50 | + q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) | |
51 | + prompt = q.catchup_choose_prompt | |
52 | + prompt.active?.should == true | |
53 | + end | |
54 | + | |
55 | + context "catchup algorithm" do | |
56 | + before(:all) do | |
57 | + user = Factory.create(:user) | |
58 | + @catchup_q = Factory.create(:aoi_question, :site => user, :creator => user.default_visitor) | |
59 | + | |
60 | + @catchup_q.it_should_autoactivate_ideas = true | |
61 | + @catchup_q.save! | |
62 | + | |
63 | + 100.times.each do |num| | |
64 | + user.create_choice("visitor identifier", @catchup_q, {:data => num, :local_identifier => "exmaple"}) | |
65 | + end | |
66 | + end | |
67 | + it "should choose an active prompt using catchup algorithm on a large number of choices" do | |
68 | + @catchup_q.reload | |
69 | + # Sanity check, 2 extra choices are autocreated when empty question created | |
70 | + @catchup_q.choices.size.should == 102 | |
71 | + | |
72 | + #the catchup algorithm depends on all prompts being generated automatically | |
73 | + @catchup_q.prompts.size.should == 102 **2 - 102 | |
74 | + | |
75 | + prompt = @catchup_q.catchup_choose_prompt | |
76 | + prompt.active?.should == true | |
77 | + end | |
78 | + | |
79 | + it "should have a normalized vector of weights to support the catchup algorithm" do | |
80 | + weights = @catchup_q.catchup_prompts_weights | |
81 | + sum = 0 | |
82 | + weights.each{|k,v| sum+=v} | |
83 | + | |
84 | + (sum - 1.0).abs.should < 0.000001 | |
85 | + end | |
86 | + | |
87 | + | |
88 | + | |
89 | + end | |
90 | + | |
40 | 91 | |
41 | 92 | #q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) |
42 | 93 | end | ... | ... |