mongoid_rating 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,220 @@
1
+ require "spec_helper"
2
+
3
+ # specs for embedded model with rating
4
+ describe Comment do
5
+
6
+ before(:each) do
7
+ @bob = User.create :name => "Bob"
8
+ @alice = User.create :name => "Alice"
9
+ @sally = User.create :name => "Sally"
10
+ @post = Post.create :name => "Announcement"
11
+ @comment1 = @post.comments.create :content => 'Hello!'
12
+ @comment2 = @post.comments.create :content => 'Goodbye!'
13
+ end
14
+
15
+ subject { @comment1 }
16
+ it { should respond_to :rate }
17
+ it { should respond_to :rate! }
18
+ it { should respond_to :unrate! }
19
+ it { should respond_to :did_rate? }
20
+ it { should respond_to :rate_average }
21
+ it { should respond_to :rate_data }
22
+ it { should respond_to :rate_by }
23
+
24
+ describe "#rate_values" do
25
+ it "should contain an array of rating marks" do
26
+ @comment1.rate_values.should be_an_instance_of Array
27
+ @comment1.rate_values.should eq []
28
+ @comment1.rate! 2, @bob
29
+ @comment1.rate_values.should eq [2]
30
+ @comment1.rate! 2.5, @bob
31
+ @comment1.rate_values.should eq [2.5]
32
+ end
33
+ end
34
+
35
+ context "when rated" do
36
+ before (:each) do
37
+ @comment1.rate! 2, @bob
38
+ end
39
+
40
+ describe "#rate" do
41
+ it "should track #rates properly" do
42
+ @comment1.rate! 3, @sally
43
+ @comment1.rate.should eq 2.5
44
+ end
45
+
46
+ it "should limit #rates by user properly" do
47
+ @comment1.rate! 5, @bob
48
+ @comment1.rate.should eql 5.0
49
+ end
50
+
51
+ context "when rate_value in rating range" do
52
+ it { expect { @comment1.rate 1, @sally }.not_to raise_error }
53
+ end
54
+
55
+ context "when rate_value not in rating range" do
56
+ it { expect { @comment1.rate 9, @sally }.to raise_error() }
57
+ end
58
+
59
+ describe "when using positive values" do
60
+ let(:num) { rand(1..5) }
61
+ let(:exp) { ((num + 2) / 2.0) - 2 }
62
+ it { expect { @comment1.rate num, @sally }.to change { @comment1.rate }.by(exp) }
63
+ end
64
+ end
65
+
66
+ describe "#rate_by" do
67
+ describe "for Bob" do
68
+ specify { @comment1.rate_by?(@bob).should be_true }
69
+ specify { @comment1.rate_by(@bob).should eq 2 }
70
+ end
71
+ describe "for Bob" do
72
+ specify { @comment2.rate_by(@bob).should be_nil }
73
+ end
74
+
75
+ describe "when rated by someone else" do
76
+ before do
77
+ @comment1.rate 1, @alice
78
+ end
79
+
80
+ describe "for Alice" do
81
+ specify { @comment1.rate_by?(@alice).should be_true }
82
+ end
83
+ end
84
+
85
+ describe "when not rated by someone else" do
86
+ describe "for Sally" do
87
+ specify { @comment1.rate_by(@sally).should be_false }
88
+ end
89
+ end
90
+ end
91
+
92
+ describe "#unrate" do
93
+ before { @comment1.unrate! @bob }
94
+
95
+ it "should have null #rate_count" do
96
+ @comment1.rate_count.should eql 0
97
+ end
98
+
99
+ it "should have null #rate" do
100
+ @comment1.rate.should be_nil
101
+ end
102
+
103
+ it "should be not rated by bob" do
104
+ @comment1.rate_by(@bob).should be_nil
105
+ end
106
+
107
+ it "should have no rate_data" do
108
+ @comment1.rate_data.should eq []
109
+ end
110
+ end
111
+
112
+ describe "#rate_count" do
113
+ it "should know how many rates have been cast" do
114
+ @comment1.rate! 1, @sally
115
+ @comment1.rate_count.should eql 2
116
+ end
117
+ end
118
+
119
+ describe "#rating" do
120
+ it "should calculate the average rate" do
121
+ @comment1.rate! 4, @sally
122
+ @comment1.rate.should eq 3.0
123
+ end
124
+ end
125
+ end
126
+
127
+ context "when not rated" do
128
+ describe "#rate" do
129
+ specify { @comment1.rate.should be_nil }
130
+ end
131
+
132
+ describe "#unrate" do
133
+ before do
134
+ @comment1.unrate! @sally
135
+ end
136
+
137
+ it "should have zero #rate_count" do
138
+ @comment1.rate_count.should eql 0
139
+ end
140
+
141
+ it "should have null #rate" do
142
+ @comment1.rate.should be_nil
143
+ end
144
+ end
145
+ end
146
+
147
+ context "when saving the collection" do
148
+ before (:each) do
149
+ @comment1.rate 3, @bob
150
+ @comment1.rate 2, @sally
151
+ @comment1.save
152
+ @comment1.reload
153
+ @f_post = Post.where(:name => "Announcement").first
154
+ @f_comment = @f_post.comments.where(:content => "Hello!").first
155
+ end
156
+
157
+ describe "#rated_by?" do
158
+ describe "for Bob" do
159
+ specify { @f_comment.rate_by?(@bob).should be_true}
160
+ end
161
+
162
+ describe "for Sally" do
163
+ specify { @f_comment.rate_by?(@sally).should be_true }
164
+ end
165
+
166
+ describe "for Alice" do
167
+ specify { @f_comment.rate_by?(@alice).should be_false}
168
+ end
169
+ end
170
+
171
+ describe "#rate" do
172
+ specify { @f_comment.rate.should eql 2.5 }
173
+ end
174
+
175
+ describe "#rate_count" do
176
+ specify { @f_comment.rate_count.should eql 2 }
177
+ end
178
+ end
179
+
180
+ describe "#scopes" do
181
+ before (:each) do
182
+ @post1 = Post.create(:name => "Post 1")
183
+ @c1 = @post1.comments.create(:content => 'c1')
184
+ @c2 = @post1.comments.create(:content => 'c2')
185
+ @c3 = @post1.comments.create(:content => 'c3')
186
+ @c4 = @post1.comments.create(:content => 'c4')
187
+ @c5 = @post1.comments.create(:content => 'c5')
188
+ @c1.rate! 5, @sally
189
+ @c1.rate! 3, @bob
190
+ @c4.rate! 1, @sally
191
+ end
192
+
193
+ describe "#rate_by" do
194
+ it "should return proper count of comments rated by Bob" do
195
+ @post1.comments.rate_by(@bob).size.should eql 1
196
+ end
197
+
198
+ it "should return proper count of comments rated by Sally" do
199
+ @post1.comments.rate_by(@sally).size.should eql 2
200
+ end
201
+ end
202
+
203
+
204
+ describe "#highest_rate" do
205
+ # count is broken on embedded models
206
+ it "should return proper count of comments" do
207
+ @post1.comments.highest_rate.limit(1).to_a.length.should eq 1
208
+ end
209
+
210
+ it "should return proper count of comments" do
211
+ # includes only with rating
212
+ @post1.comments.highest_rate.limit(10).to_a.length.should eq 2
213
+ end
214
+
215
+ it "should return proper document" do
216
+ @post1.comments.highest_rate.limit(1).first.content.should eql "c1"
217
+ end
218
+ end
219
+ end
220
+ end
@@ -0,0 +1,7 @@
1
+ class Article
2
+ include Mongoid::Document
3
+
4
+ field :name
5
+
6
+ rateable :overall, range: -5..5
7
+ end
@@ -0,0 +1,9 @@
1
+ class Comment
2
+ include Mongoid::Document
3
+
4
+ rateable :rate, eval: false
5
+
6
+ embedded_in :post
7
+
8
+ field :content
9
+ end
@@ -0,0 +1,9 @@
1
+ class Post
2
+ include Mongoid::Document
3
+
4
+ field :name, type: String
5
+
6
+ embeds_many :comments
7
+
8
+ rateable :rate
9
+ end
@@ -0,0 +1,5 @@
1
+ class User
2
+ include Mongoid::Document
3
+
4
+ field :name
5
+ end
data/spec/post_spec.rb ADDED
@@ -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
+ @article = Article.create :name => "Article"
11
+ end
12
+
13
+ subject { @post }
14
+ it { should respond_to :rate }
15
+ it { should respond_to :rate! }
16
+ it { should respond_to :unrate! }
17
+ it { should respond_to :did_rate? }
18
+ it { should respond_to :rate_average }
19
+ it { should respond_to :rate_data }
20
+ it { should respond_to :rate_by }
21
+
22
+
23
+ describe "#rate_values" do
24
+ it "should be an array with rated values" do
25
+ @post.rate_values.should be_an_instance_of Array
26
+ @post.rate! 1, @bob
27
+ @post.rate_values.should eq [1]
28
+ end
29
+ end
30
+
31
+ context "when rated" do
32
+ before (:each) do
33
+ @post.rate! 1, @bob
34
+ end
35
+
36
+ describe "#rate" do
37
+ it "should track #rates properly" do
38
+ @post.rate! 1, @sally
39
+ @post.rate_count.should eql 2
40
+ end
41
+
42
+ it "should limit #rates by user properly" do
43
+ @post.rate! 5, @bob
44
+ @post.rate.should eql 5.0
45
+ end
46
+
47
+ context "when rate_value in rating range" do
48
+ it { expect { @post.rate 1, @sally }.not_to raise_error }
49
+ end
50
+
51
+ context "when rate_value not in rating range" do
52
+ it { expect { @post.rate 17, @sally }.to raise_error() }
53
+ it { expect { @post.rate -17, @sally }.to raise_error() }
54
+ end
55
+ end
56
+
57
+ describe "#rated?" do
58
+ describe "for Bob" do
59
+ specify { @post.rate_by?(@bob).should be_true }
60
+ end
61
+ describe "for Sally" do
62
+ specify { @post.rate_by?(@sally).should be_false }
63
+ end
64
+
65
+ describe "when rated by someone else" do
66
+ before do
67
+ @post.rate 1, @alice
68
+ end
69
+ describe "for Bob" do
70
+ specify { @post.rate_by?(@bob).should be_true }
71
+ end
72
+ describe "for Sally" do
73
+ specify { @post.rate_by?(@sally).should be_false }
74
+ end
75
+ describe "for Alice" do
76
+ specify { @post.rate_by?(@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.rate_by?(@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 #rate" do
95
+ @post.rate.should be_nil
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 "#rate" do
107
+ it "should calculate the average rate" do
108
+ @post.rate 4, @sally
109
+ @post.rate.should eq 2.5
110
+ end
111
+ end
112
+
113
+ describe "#rate_by" do
114
+ describe "should give mark" do
115
+ specify { @post.rate_by(@bob).should eq 1}
116
+ end
117
+ describe "should give nil" do
118
+ specify { @post.rate_by(@alice).should be_nil}
119
+ end
120
+ end
121
+ end
122
+
123
+ context "when not rated" do
124
+ describe "#rates" do
125
+ specify { @post.rate_count.should eql 0 }
126
+ end
127
+
128
+ describe "#rating" do
129
+ specify { @post.rate.should be_nil }
130
+ end
131
+
132
+ describe "#unrate" do
133
+ before do
134
+ @post.unrate @sally
135
+ end
136
+
137
+ it "should have null #rate_count" do
138
+ @post.rate_count.should eql 0
139
+ end
140
+
141
+ it "should have null #rates" do
142
+ @post.rate.should be_nil
143
+ end
144
+ end
145
+ end
146
+
147
+ context "when saving the collection" do
148
+ before (:each) do
149
+ @post.rate 3, @bob
150
+ @post.save
151
+ @f_post = Post.where(:name => "Announcement").first
152
+ end
153
+
154
+ describe "#rate_by?" do
155
+ describe "for Bob" do
156
+ specify { @f_post.rate_by?(@bob).should be_true }
157
+ end
158
+
159
+ describe "for Alice" do
160
+ specify { @f_post.rate_by?(@alice).should be_false}
161
+ end
162
+ end
163
+
164
+ describe "#rate" do
165
+ specify { @f_post.rate.should eql 3.0 }
166
+ end
167
+
168
+ describe "#rate_count" do
169
+ specify { @f_post.rate_count.should eql 1 }
170
+ end
171
+ end
172
+
173
+ describe "#scopes" do
174
+ before (:each) do
175
+ @post.delete
176
+ @post1 = Post.create(:name => "Post 1")
177
+ @post2 = Post.create(:name => "Post 2")
178
+ @post3 = Post.create(:name => "Post 3")
179
+ @post4 = Post.create(:name => "Post 4")
180
+ @post5 = Post.create(:name => "Post 5")
181
+ @post1.rate 5, @sally
182
+ @post1.rate 3, @bob
183
+ @post4.rate 1, @sally
184
+ end
185
+
186
+ describe "#rate_by scope" do
187
+ it "should return proper count of posts rated by Bob" do
188
+ Post.rate_by(@bob).size.should eql 1
189
+ end
190
+
191
+ it "should return proper count of posts rated by Sally" do
192
+ Post.rate_by(@sally).size.should eql 2
193
+ end
194
+ end
195
+
196
+ describe "#with_rating" do
197
+ before (:each) do
198
+ @post1.rate 4, @alice
199
+ @post2.rate 2, @alice
200
+ @post3.rate 5, @alice
201
+ @post4.rate 2, @alice
202
+ end
203
+
204
+ it "should return proper count of posts with rating 4..5" do
205
+ Post.rate_in(4..5).size.should eql 2
206
+ end
207
+
208
+ it "should return proper count of posts with rating 0..2" do
209
+ Post.rate_in(0..2).size.should eql 2
210
+ end
211
+
212
+ it "should return proper count of posts with rating 0..5" do
213
+ Post.rate_in(0..5).size.should eql 4
214
+ end
215
+ end
216
+
217
+ describe "#highest_rated" do
218
+ it "should return proper count of posts" do
219
+ #mongoid has problems with returning count of documents (https://github.com/mongoid/mongoid/issues/817)
220
+ posts_count = 0
221
+ Post.highest_rate.limit(1).each {|x| posts_count+=1 }
222
+ posts_count.should eql 1
223
+ end
224
+
225
+ it "should return proper count of posts" do
226
+ #mongoid has problems with returning count of documents (https://github.com/mongoid/mongoid/issues/817)
227
+ posts_count = 0
228
+ Post.highest_rate.limit(10).each {|x| posts_count+=1 }
229
+ posts_count.should eql 2
230
+ end
231
+
232
+ it "should return proper document" do
233
+ Post.highest_rate.limit(1).first.name.should eql "Post 1"
234
+ end
235
+ end
236
+ end
237
+ end
238
+