Commit 681d8a6a8c23b28629c6bdf5d5ac307cc85f60c1
1 parent
2093d6f2
Exists in
master
and in
1 other branch
Improving choices test coverage
Showing
3 changed files
with
74 additions
and
28 deletions
Show diff stats
app/controllers/choices_controller.rb
@@ -38,7 +38,7 @@ class ChoicesController < InheritedResources::Base | @@ -38,7 +38,7 @@ class ChoicesController < InheritedResources::Base | ||
38 | show! do |format| | 38 | show! do |format| |
39 | format.xml { | 39 | format.xml { |
40 | @choice.reload | 40 | @choice.reload |
41 | - render :xml => @choice.to_xml(:methods => [:item_data, :wins_plus_losses, :question_name])} | 41 | + render :xml => @choice.to_xml(:methods => [:wins_plus_losses])} |
42 | format.json { render :json => @choice.to_json(:methods => [:data])} | 42 | format.json { render :json => @choice.to_json(:methods => [:data])} |
43 | end | 43 | end |
44 | end | 44 | end |
app/models/choice.rb
@@ -22,14 +22,6 @@ class Choice < ActiveRecord::Base | @@ -22,14 +22,6 @@ class Choice < ActiveRecord::Base | ||
22 | end | 22 | end |
23 | #attr_accessor :data | 23 | #attr_accessor :data |
24 | 24 | ||
25 | - def question_name | ||
26 | - question.name | ||
27 | - end | ||
28 | - | ||
29 | - def item_data | ||
30 | - item.data | ||
31 | - end | ||
32 | - | ||
33 | def lose! | 25 | def lose! |
34 | self.loss_count += 1 rescue (self.loss_count = 1) | 26 | self.loss_count += 1 rescue (self.loss_count = 1) |
35 | self.score = compute_score | 27 | self.score = compute_score |
@@ -46,6 +38,7 @@ class Choice < ActiveRecord::Base | @@ -46,6 +38,7 @@ class Choice < ActiveRecord::Base | ||
46 | wins + losses | 38 | wins + losses |
47 | end | 39 | end |
48 | 40 | ||
41 | + # TODO delete these and refactor loss_count and votes_count into losses and wins | ||
49 | def losses | 42 | def losses |
50 | loss_count || 0 | 43 | loss_count || 0 |
51 | end | 44 | end |
@@ -110,11 +103,6 @@ class Choice < ActiveRecord::Base | @@ -110,11 +103,6 @@ class Choice < ActiveRecord::Base | ||
110 | self.save! | 103 | self.save! |
111 | end | 104 | end |
112 | 105 | ||
113 | - def suspend! | ||
114 | - (self.active = false) | ||
115 | - self.save! | ||
116 | - end | ||
117 | - | ||
118 | def deactivate! | 106 | def deactivate! |
119 | (self.active = false) | 107 | (self.active = false) |
120 | self.save! | 108 | self.save! |
spec/models/choice_spec.rb
@@ -4,13 +4,19 @@ describe Choice do | @@ -4,13 +4,19 @@ describe Choice do | ||
4 | 4 | ||
5 | it {should belong_to :question} | 5 | it {should belong_to :question} |
6 | it {should belong_to :item} | 6 | it {should belong_to :item} |
7 | + #uncomment after adding creator to choice model | ||
8 | + #it {should belong_to :creator} | ||
7 | it {should have_many :flags} | 9 | it {should have_many :flags} |
10 | + it {should have_many :votes} | ||
11 | + it {should have_many :prompts_on_the_left} | ||
12 | + it {should have_many :prompts_on_the_right} | ||
8 | it {should validate_presence_of :question} | 13 | it {should validate_presence_of :question} |
14 | + it {should validate_presence_of :creator} | ||
9 | 15 | ||
10 | before(:each) do | 16 | before(:each) do |
11 | @aoi_clone = Factory.create(:email_confirmed_user) | 17 | @aoi_clone = Factory.create(:email_confirmed_user) |
12 | @visitor= Factory.create(:visitor, :site => @aoi_clone) | 18 | @visitor= Factory.create(:visitor, :site => @aoi_clone) |
13 | - @question = Question.create(:name => 'which do you like better?', | 19 | + @question = Factory.create(:aoi_question, :name => "Which do you like better?", |
14 | :site => @aoi_clone, | 20 | :site => @aoi_clone, |
15 | :creator => @visitor) | 21 | :creator => @visitor) |
16 | 22 | ||
@@ -25,13 +31,6 @@ describe Choice do | @@ -25,13 +31,6 @@ describe Choice do | ||
25 | Choice.create!(@valid_attributes) | 31 | Choice.create!(@valid_attributes) |
26 | end | 32 | end |
27 | 33 | ||
28 | - #it "should generate prompts after two choices are created" do | ||
29 | - # proc { | ||
30 | -# choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) | ||
31 | -# choice2 = Choice.create!(@valid_attributes.merge(:data => '1234')) | ||
32 | -# }.should change(@question.prompts, :count).by(2) | ||
33 | -# end | ||
34 | - | ||
35 | it "should deactivate a choice" do | 34 | it "should deactivate a choice" do |
36 | choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) | 35 | choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) |
37 | choice1.deactivate! | 36 | choice1.deactivate! |
@@ -39,12 +38,14 @@ describe Choice do | @@ -39,12 +38,14 @@ describe Choice do | ||
39 | end | 38 | end |
40 | 39 | ||
41 | it "should update a question's counter cache on creation" do | 40 | it "should update a question's counter cache on creation" do |
42 | - @question.choices_count.should == 0 | ||
43 | - @question.choices.size.should == 0 | ||
44 | - Choice.create!(@valid_attributes.merge(:data => '1234')) | ||
45 | - @question.reload | ||
46 | - @question.choices_count.should == 1 | ||
47 | - @question.choices.size.should == 1 | 41 | + # not an allour ideas question |
42 | + question = Factory.create(:question, :site => @aoi_clone, :creator => @visitor) | ||
43 | + question.choices_count.should == 0 | ||
44 | + question.choices.size.should == 0 | ||
45 | + Choice.create!(@valid_attributes.merge(:question => question)) | ||
46 | + question.reload | ||
47 | + question.choices_count.should == 1 | ||
48 | + question.choices.size.should == 1 | ||
48 | 49 | ||
49 | end | 50 | end |
50 | 51 | ||
@@ -67,4 +68,61 @@ describe Choice do | @@ -67,4 +68,61 @@ describe Choice do | ||
67 | @question.reload | 68 | @question.reload |
68 | @question.inactive_choices_count.should == prev_inactive + 1 | 69 | @question.inactive_choices_count.should == prev_inactive + 1 |
69 | end | 70 | end |
71 | + it "should have a default score of 50" do | ||
72 | + choice1 = Choice.create!(@valid_attributes) | ||
73 | + choice1.score.should == 50 | ||
74 | + end | ||
75 | + it "correctly compute a score based on wins and losses" do | ||
76 | + choice1 = Choice.create!(@valid_attributes) | ||
77 | + choice1.votes_count = 30 | ||
78 | + choice1.loss_count = 70 | ||
79 | + choice1.compute_score.should be_close(30,1) | ||
80 | + end | ||
81 | + it "compute score and save" do | ||
82 | + choice1 = Choice.create!(@valid_attributes) | ||
83 | + choice1.score.should == 50 | ||
84 | + choice1.votes_count= 30 | ||
85 | + choice1.loss_count = 70 | ||
86 | + choice1.compute_score! | ||
87 | + choice1.score.should be_close(30, 1) | ||
88 | + end | ||
89 | + | ||
90 | + it "determines whether a choice is admin created" do | ||
91 | + admin_choice = @question.choices.first | ||
92 | + admin_choice.user_created.should be_false | ||
93 | + end | ||
94 | + it "determines whether a choice is user created" do | ||
95 | + new_visitor = Factory.create(:visitor, :site => @aoi_clone) | ||
96 | + user_choice = Factory.create(:choice, :question => @question, :creator => new_visitor) | ||
97 | + user_choice.user_created.should be_true | ||
98 | + end | ||
99 | + | ||
100 | + describe "voting updates things" do | ||
101 | + before do | ||
102 | + @prompt = @question.choose_prompt | ||
103 | + @winning_choice = @prompt.left_choice | ||
104 | + @losing_choice = @prompt.right_choice | ||
105 | + @winning_choice.win! | ||
106 | + @losing_choice.lose! | ||
107 | + end | ||
108 | + | ||
109 | + it "should update score on a win" do | ||
110 | + @winning_choice.score.should be_close(67, 1) | ||
111 | + end | ||
112 | + it "should update score on a loss" do | ||
113 | + @losing_choice.score.should be_close(33,1) | ||
114 | + end | ||
115 | + it "should update win count on a win" do | ||
116 | + @winning_choice.votes_count.should == 1 | ||
117 | + @winning_choice.wins.should == 1 | ||
118 | + @winning_choice.loss_count.should == 0 | ||
119 | + @winning_choice.losses.should == 0 | ||
120 | + end | ||
121 | + it "should update loss count on a loss" do | ||
122 | + @losing_choice.votes_count.should == 0 | ||
123 | + @losing_choice.wins.should == 0 | ||
124 | + @losing_choice.loss_count.should == 1 | ||
125 | + @losing_choice.losses.should == 1 | ||
126 | + end | ||
127 | + end | ||
70 | end | 128 | end |