mongoid_rateable 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,238 @@
1
+ require "spec_helper"
2
+
3
+ describe Post do
4
+
5
+ before(:each) do
6
+ @bob = User.create :name => "Bob"
7
+ @alice = User.create :name => "Alice"
8
+ @sally = User.create :name => "Sally"
9
+ @post = Post.create :name => "Announcement"
10
+ end
11
+
12
+ it "should have Mongoid::Rateable module" do
13
+ #TODO: Refactor this
14
+ @post.class.const_get("Mongoid").const_get("Rateable").should be_true
15
+ end
16
+
17
+ subject { @post }
18
+ it { should respond_to :rate }
19
+ it { should respond_to :unrate }
20
+ it { should respond_to :rate_and_save }
21
+ it { should respond_to :unrate_and_save }
22
+ it { should respond_to :rated? }
23
+ it { should respond_to :rate_count }
24
+ it { should respond_to :rates }
25
+ it { should respond_to :rating }
26
+ it { should respond_to :rating_marks }
27
+
28
+ describe "#rates" do
29
+ it "should be proper Mongoid field" do
30
+ @post.fields['rates'].should be_an_instance_of Mongoid::Field
31
+ end
32
+ end
33
+
34
+ describe "#rating_marks" do
35
+ it "should be proper Mongoid field" do
36
+ @post.rating_marks.should be_an_instance_of Array
37
+ end
38
+ end
39
+
40
+ context "when rated" do
41
+ before (:each) { @post.rate 1, @bob }
42
+
43
+ describe "#rate" do
44
+ it "should track #rates properly" do
45
+ @post.rate 1, @sally
46
+ @post.rates.should eql 2
47
+ end
48
+
49
+ it "should limit #rates by user properly" do
50
+ @post.rate 5, @bob
51
+ @post.rates.should eql 5
52
+ end
53
+
54
+ #TODO: Rewrite for random values
55
+ describe "when using negative values" do
56
+ it "should work properly for -1" do
57
+ @post.rate -1, @sally
58
+ @post.rates.should eql 0
59
+ end
60
+ it "should work properly for -2" do
61
+ @post.rate -3, @sally
62
+ @post.rates.should eql -2
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "#rated?" do
68
+ describe "for Bob" do
69
+ specify { @post.rated?(@bob).should be_true }
70
+ end
71
+
72
+ describe "when rated by someone else" do
73
+ before { @post.rate 1, @alice }
74
+
75
+ describe "for Alice" do
76
+ specify { @post.rated?(@alice).should be_true }
77
+ end
78
+ end
79
+
80
+ describe "when not rated by someone else" do
81
+ describe "for Sally" do
82
+ specify { @post.rated?(@sally).should be_false }
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "#unrate" do
88
+ before { @post.unrate @bob }
89
+
90
+ it "should have null #rate_count" do
91
+ @post.rate_count.should eql 0
92
+ end
93
+
94
+ it "should have null #rates" do
95
+ @post.rates.should eql 0
96
+ end
97
+ end
98
+
99
+ describe "#rate_count" do
100
+ it "should know how many rates have been cast" do
101
+ @post.rate 1, @sally
102
+ @post.rate_count.should eql 2
103
+ end
104
+ end
105
+
106
+ describe "#rating" do
107
+ it "should calculate the average rate" do
108
+ @post.rate 4, @sally
109
+ @post.rating.should eql 2.5
110
+ end
111
+
112
+ it "should average if the result is zero" do
113
+ @post.rate -1, @sally
114
+ @post.rating.should eql 0.0
115
+ end
116
+ end
117
+ end
118
+
119
+ context "when not rated" do
120
+ describe "#rates" do
121
+ specify { @post.rates.should eql 0 }
122
+ end
123
+
124
+ describe "#rating" do
125
+ specify { @post.rating.should be_nil }
126
+ end
127
+
128
+ describe "#unrate" do
129
+ before { @post.unrate @sally }
130
+
131
+ it "should have null #rate_count" do
132
+ @post.rate_count.should eql 0
133
+ end
134
+
135
+ it "should have null #rates" do
136
+ @post.rates.should eql 0
137
+ end
138
+ end
139
+ end
140
+
141
+ context "when saving the collection" do
142
+ before (:each) do
143
+ @post.rate 8, @bob
144
+ @post.rate -10, @sally
145
+ @post.save
146
+ @finded_post = Post.where(:name => "Announcement").first
147
+ end
148
+
149
+ describe "#rated?" do
150
+ describe "for Bob" do
151
+ specify { @finded_post.rated?(@bob).should be_true }
152
+ end
153
+
154
+ describe "for Sally" do
155
+ specify { @finded_post.rated?(@sally).should be_true }
156
+ end
157
+
158
+ describe "for Alice" do
159
+ specify { @finded_post.rated?(@alice).should be_false}
160
+ end
161
+ end
162
+
163
+ describe "#rates" do
164
+ specify { @finded_post.rates.should eql -2 }
165
+ end
166
+
167
+ describe "#rate_count" do
168
+ specify { @finded_post.rate_count.should eql 2 }
169
+ end
170
+
171
+ describe "#rating" do
172
+ specify { @finded_post.rating.should eql -1.0 }
173
+ end
174
+ end
175
+
176
+ describe "#rate_and_save" do
177
+ before (:each) do
178
+ @post.rate_and_save 8, @bob
179
+ @post.rate_and_save -10, @sally
180
+ @finded_post = Post.where(:name => "Announcement").first
181
+ end
182
+
183
+ describe "#rated?" do
184
+ it "should be #rated? by Bob" do
185
+ @finded_post.rated?(@bob).should be_true
186
+ end
187
+
188
+ it "should be #rated? by Sally" do
189
+ @finded_post.rated?(@sally).should be_true
190
+ end
191
+
192
+ it "should be not #rated? by Alice" do
193
+ @finded_post.rated?(@alice).should be_false
194
+ end
195
+ end
196
+
197
+ it "should have #rates equal -2" do
198
+ @finded_post.rates.should eql -2
199
+ end
200
+
201
+ it "should have #rate_count equal 2" do
202
+ @finded_post.rate_count.should eql 2
203
+ end
204
+
205
+ it "should have #rating equal -1.0" do
206
+ @finded_post.rating.should eql -1.0
207
+ end
208
+
209
+ describe "#unrate_and_save" do
210
+ before (:each) do
211
+ @post.unrate_and_save @sally
212
+ @finded_post = Post.where(:name => "Announcement").first
213
+ end
214
+
215
+ describe "#rated?" do
216
+ it "should be #rated? by Bob" do
217
+ @finded_post.rated?(@bob).should be_true
218
+ end
219
+
220
+ it "should be not #rated? by Sally" do
221
+ @finded_post.rated?(@sally).should be_false
222
+ end
223
+ end
224
+
225
+ it "should have #rates equal 8" do
226
+ @finded_post.rates.should eql 8
227
+ end
228
+
229
+ it "should have #rate_count equal 1" do
230
+ @finded_post.rate_count.should eql 1
231
+ end
232
+
233
+ it "should have #rating equal 8.0" do
234
+ @finded_post.rating.should eql 8.0
235
+ end
236
+ end
237
+ end
238
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,10 +6,12 @@ MODELS = File.join(File.dirname(__FILE__), "models")
6
6
  require "rubygems"
7
7
  require "mongoid"
8
8
  require "mongoid_rateable"
9
- require "rspec"
10
9
  require "database_cleaner"
10
+ require "simplecov"
11
11
 
12
- Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
12
+ SimpleCov.start
13
+
14
+ Dir["#{MODELS}/*.rb"].each { |f| require f }
13
15
 
14
16
  Mongoid.config.master = Mongo::Connection.new.db("mongoid_rateable_test")
15
17
  Mongoid.logger = Logger.new($stdout)