vote_spec.rb
6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Vote do
it {should belong_to :voter}
it {should belong_to :question}
it {should belong_to :prompt}
it {should belong_to :choice}
it {should belong_to :loser_choice}
it {should have_one :appearance}
before(:each) do
@question = Factory.create(:aoi_question)
@prompt = @question.prompts.first
@required_params = {:question => @question, :prompt => @prompt,
:choice => @prompt.left_choice,
:voter=> @question.site.default_visitor,
:loser_choice => @prompt.right_choice}
end
it "should create a new instance with factory girl" do
Factory.create(:vote)
end
it "should update counter cache on question" do
@question.votes.size.should == 0
@question.votes_count.should == 0
Factory.create(:vote, :question => @question)
@question.reload
@question.votes.size.should == 1
@question.votes_count.should == 1
end
it "should update counter cache on prompt" do
@prompt.votes.size.should == 0
@prompt.votes_count.should == 0
Factory.create(:vote, :question => @question, :prompt => @prompt)
@prompt.reload
@prompt.votes.size.should == 1
@prompt.votes_count.should == 1
end
it "should update counter cache on choice" do
@prompt.left_choice.votes.size.should == 0
@prompt.left_choice.wins.should == 0
Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice)
@prompt.left_choice.reload
@prompt.left_choice.votes.size.should == 1
@prompt.left_choice.wins.should == 1
end
it "should update counter cache on loser_choice" do
@prompt.left_choice.votes.size.should == 0
@prompt.right_choice.losses.should == 0
@prompt.left_choice.wins.should == 0
Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice,
:loser_choice => @prompt.right_choice)
@prompt.right_choice.reload
@prompt.right_choice.votes.size.should == 0
@prompt.right_choice.wins.should == 0
@prompt.right_choice.losses.should == 1
end
it "should update score of winner choice after create" do
@prompt.left_choice.score.should == 50
Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice)
@prompt.left_choice.reload
@prompt.left_choice.score.should be_close 67, 1
end
it "should update score of loser choice after create" do
@prompt.left_choice.score.should == 50
Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice,
:loser_choice => @prompt.right_choice)
@prompt.right_choice.reload
@prompt.right_choice.score.should be_close 33, 1
end
it "should not display invalid votes by default" do
5.times do
Factory.create(:vote)
end
Vote.count.should == 5
v = Vote.last
v.valid_record.should be_true
v.valid_record = false
v.save
Vote.count.should == 4
end
it "should allow default valid_record behavior to be overriden by default" do
vote = Vote.create!(@required_params)
vote.valid_record.should be_true
vote = Vote.create!(@required_params.merge!(:valid_record => false))
vote.valid_record.should be_false
end
it "should update counter cache on question when vote is flagged as invalid" do
vote = Factory.create(:vote, :question => @question)
@question.reload
@question.votes.size.should == 1
@question.votes_count.should == 1
vote.valid_record = false;
vote.save
@question.reload
@question.votes.size.should == 0
@question.votes_count.should == 0
the_vote = Factory.create(:vote, :question => @question)
the_vote.validity_information = "blah blah blah"
the_vote.save
@question.reload.votes.size.should == 1
end
it "should update counter cache on question when vote is created with invalid flag" do
vote = Vote.create!(@required_params)
@question.reload.votes.size.should == 1
vote = Vote.create!(@required_params.merge!(:valid_record => false))
@question.reload.votes.size.should == 1
end
it "should update counter cache on choice after changing valid status" do
vote = Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice)
@prompt.left_choice.reload
@prompt.left_choice.votes.size.should == 1
@prompt.left_choice.wins.should == 1
vote.valid_record = false
vote.save
@prompt.left_choice.reload
@prompt.left_choice.votes.size.should == 0
@prompt.left_choice.wins.should == 0
end
it "should update counter cache on prompt after changing valid status" do
vote = Factory.create(:vote, :question => @question, :prompt => @prompt)
@prompt.reload
@prompt.votes.size.should == 1
@prompt.votes_count.should == 1
vote.valid_record = false
vote.save
@prompt.reload
@prompt.votes.size.should == 0
@prompt.votes_count.should == 0
end
it "should update counter cache on loser_choice after invalid is changed" do
vote = Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice,
:loser_choice => @prompt.right_choice)
@prompt.right_choice.reload
@prompt.right_choice.votes.size.should == 0
@prompt.right_choice.wins.should == 0
@prompt.right_choice.losses.should == 1
vote.valid_record = false
vote.save
@prompt.right_choice.reload
@prompt.right_choice.votes.size.should == 0
@prompt.right_choice.wins.should == 0
@prompt.right_choice.losses.should == 0
end
it "should update score choice and loser_choice after invalid is changed" do
vote = Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice,
:loser_choice => @prompt.right_choice)
@prompt.left_choice.reload
@prompt.left_choice.score.should be_close(67, 1)
@prompt.right_choice.reload
@prompt.right_choice.score.should be_close(33, 1)
vote.valid_record = false
vote.save
@prompt.left_choice.reload
@prompt.left_choice.score.should == 50
@prompt.right_choice.reload
@prompt.right_choice.score.should == 50
end
end