voteable_mongo 0.8.1

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.
data/spec/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --debugger
@@ -0,0 +1,10 @@
1
+ class Category
2
+ include Mongoid::Document
3
+ include Mongo::Voteable
4
+
5
+ field :name
6
+
7
+ has_and_belongs_to_many :posts
8
+
9
+ voteable self, :index => true
10
+ end
@@ -0,0 +1,13 @@
1
+ require 'post'
2
+
3
+ class Comment
4
+ include Mongoid::Document
5
+ include Mongo::Voteable
6
+
7
+ field :content
8
+
9
+ belongs_to :post
10
+
11
+ voteable self, :up => +1, :down => -3
12
+ voteable Post, :up => +2, :down => -1
13
+ end
@@ -0,0 +1,13 @@
1
+ class Post
2
+ include Mongoid::Document
3
+ include Mongo::Voteable
4
+
5
+ field :title
6
+ field :content
7
+
8
+ has_and_belongs_to_many :categories
9
+ has_many :comments
10
+
11
+ voteable self, :up => +1, :down => -1, :index => true
12
+ voteable Category, :up => +3, :down => -5, :update_counters => false
13
+ end
@@ -0,0 +1,4 @@
1
+ class User
2
+ include Mongoid::Document
3
+ include Mongo::Voter
4
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+
9
+ MODELS = File.join(File.dirname(__FILE__), "models")
10
+ $LOAD_PATH.unshift(MODELS)
11
+
12
+
13
+ require 'mongoid'
14
+ require 'voteable_mongo'
15
+ require 'rspec'
16
+ require 'rspec/autorun'
17
+
18
+
19
+ Mongoid.configure do |config|
20
+ name = "voteable_mongo_test"
21
+ host = "localhost"
22
+ config.master = Mongo::Connection.new.db(name)
23
+ end
24
+
25
+
26
+ Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
27
+
28
+ User.collection.drop
29
+ Post.collection.drop
30
+ Comment.collection.drop
31
+ Category.collection.drop
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Mongo::Voteable::Tasks do
4
+ describe 'Mongo::Voteable::Tasks.init_stats' do
5
+ before :all do
6
+ @post1 = Post.create!
7
+ @post2 = Post.create!
8
+ end
9
+
10
+ it 'after create votes has default value' do
11
+ @post1.votes.should == Mongo::Voteable::Votes::DEFAULT_ATTRIBUTES
12
+ @post2.votes.should == Mongo::Voteable::Votes::DEFAULT_ATTRIBUTES
13
+ end
14
+
15
+ it 'reset votes data' do
16
+ @post1.votes = nil
17
+ @post1.save
18
+
19
+ @post2.votes = nil
20
+ @post2.save
21
+ end
22
+
23
+ it 'init_stats recover votes default value' do
24
+ Mongo::Voteable::Tasks.init_stats
25
+
26
+ @post1.reload
27
+ @post2.reload
28
+
29
+ @post1.votes.should == Mongo::Voteable::Votes::DEFAULT_ATTRIBUTES
30
+ @post2.votes.should == Mongo::Voteable::Votes::DEFAULT_ATTRIBUTES
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,426 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Mongo::Voteable do
4
+
5
+ context 'when :index is passed as an argument' do
6
+ before do
7
+ Post.collection.drop_indexes
8
+ Category.collection.drop_indexes
9
+ end
10
+
11
+ it 'defines indexes' do
12
+ [Post, Category].each do |klass|
13
+ klass.create_indexes
14
+ [ 'votes.up_1__id_1',
15
+ 'votes.down_1__id_1'
16
+ ].each { |index_key|
17
+ klass.collection.index_information.should have_key index_key
18
+ klass.collection.index_information[index_key]['unique'].should be_true
19
+ }
20
+
21
+ [ 'votes.count_-1',
22
+ 'votes.up_count_-1',
23
+ 'votes.down_count_-1',
24
+ 'votes.point_-1'
25
+ ].each { |index_key|
26
+ klass.collection.index_information.should have_key index_key
27
+ }
28
+ end
29
+ end
30
+ end
31
+
32
+ before :all do
33
+ @category1 = Category.create!(:name => 'xyz')
34
+ @category2 = Category.create!(:name => 'abc')
35
+
36
+ @post1 = Post.create!
37
+ @post2 = Post.create!
38
+
39
+ @post1.categories << @category1
40
+ @category2.posts << @post1
41
+
42
+ @comment = @post2.comments.create!
43
+
44
+ @user1 = User.create!
45
+ @user2 = User.create!
46
+ end
47
+
48
+ context "just created" do
49
+ it 'votes_count, up_votes_count, down_votes_count, votes_point should be zero' do
50
+ @category1.up_votes_count.should == 0
51
+ @category1.down_votes_count.should == 0
52
+ @category1.votes_count.should == 0
53
+ @category1.votes_point.should == 0
54
+
55
+ @category2.up_votes_count.should == 0
56
+ @category2.down_votes_count.should == 0
57
+ @category2.votes_count.should == 0
58
+ @category2.votes_point.should == 0
59
+
60
+ @post1.up_votes_count.should == 0
61
+ @post1.down_votes_count.should == 0
62
+ @post1.votes_count.should == 0
63
+ @post1.votes_point.should == 0
64
+
65
+ @post2.up_votes_count.should == 0
66
+ @post2.down_votes_count.should == 0
67
+ @post2.votes_count.should == 0
68
+ @post2.votes_point.should == 0
69
+
70
+ @comment.up_votes_count.should == 0
71
+ @comment.down_votes_count.should == 0
72
+ @comment.votes_count.should == 0
73
+ @comment.votes_point.should == 0
74
+ end
75
+
76
+ it 'up_voter_ids, down_voter_ids should be empty' do
77
+ @category1.up_voter_ids.should be_empty
78
+ @category1.down_voter_ids.should be_empty
79
+
80
+ @category2.up_voter_ids.should be_empty
81
+ @category2.down_voter_ids.should be_empty
82
+
83
+ @post1.up_voter_ids.should be_empty
84
+ @post1.down_voter_ids.should be_empty
85
+
86
+ @post2.up_voter_ids.should be_empty
87
+ @post2.down_voter_ids.should be_empty
88
+
89
+ @comment.up_voter_ids.should be_empty
90
+ @comment.down_voter_ids.should be_empty
91
+ end
92
+
93
+ it 'voted by voter should be empty' do
94
+ Category.voted_by(@user1).should be_empty
95
+ Category.voted_by(@user2).should be_empty
96
+
97
+ Post.voted_by(@user1).should be_empty
98
+ Post.voted_by(@user2).should be_empty
99
+
100
+ Comment.voted_by(@user1).should be_empty
101
+ Comment.voted_by(@user2).should be_empty
102
+ end
103
+
104
+ it 'revote post1 has no effect' do
105
+ @post1.vote(:revote => true, :voter => @user1, :value => 'up')
106
+
107
+ @post1.up_votes_count.should == 0
108
+ @post1.down_votes_count.should == 0
109
+ @post1.votes_count.should == 0
110
+ @post1.votes_point.should == 0
111
+ end
112
+
113
+ it 'revote post2 has no effect' do
114
+ Post.vote(:revote => true, :votee_id => @post2.id, :voter_id => @user2.id, :value => :down)
115
+ @post2.reload
116
+
117
+ @post2.up_votes_count.should == 0
118
+ @post2.down_votes_count.should == 0
119
+ @post2.votes_count.should == 0
120
+ @post2.votes_point.should == 0
121
+ end
122
+ end
123
+
124
+ context 'user1 vote up post1 the first time' do
125
+ before :all do
126
+ @post = @post1.vote(:voter_id => @user1.id, :value => :up, :return_votee => true)
127
+ end
128
+
129
+ it 'validates return post' do
130
+ @post.should be_is_a Post
131
+ @post.should_not be_new_record
132
+
133
+ @post.votes.should == {
134
+ 'up' => [@user1.id],
135
+ 'down' => [],
136
+ 'up_count' => 1,
137
+ 'down_count' => 0,
138
+ 'count' => 1,
139
+ 'point' => 1
140
+ }
141
+ end
142
+
143
+ it 'validates' do
144
+ @post1.up_votes_count.should == 1
145
+ @post1.down_votes_count.should == 0
146
+ @post1.votes_count.should == 1
147
+ @post1.votes_point.should == 1
148
+
149
+ @post1.vote_value(@user1).should == :up
150
+ @post1.should be_voted_by(@user1)
151
+ @post1.vote_value(@user2.id).should be_nil
152
+ @post1.should_not be_voted_by(@user2.id)
153
+
154
+ @post1.up_voters(User).should == [ @user1 ]
155
+ @post1.voters(User).should == [ @user1 ]
156
+ @post1.down_voters(User).should be_empty
157
+
158
+ Post.voted_by(@user1).to_a.should == [ @post1 ]
159
+ Post.voted_by(@user2).to_a.should be_empty
160
+
161
+ @category1.reload
162
+ @category1.up_votes_count.should == 0
163
+ @category1.down_votes_count.should == 0
164
+ @category1.votes_count.should == 0
165
+ @category1.votes_point.should == 3
166
+
167
+ @category2.reload
168
+ @category2.up_votes_count.should == 0
169
+ @category2.down_votes_count.should == 0
170
+ @category2.votes_count.should == 0
171
+ @category2.votes_point.should == 3
172
+ end
173
+
174
+ it 'user1 vote post1 has no effect' do
175
+ Post.vote(:revote => false, :votee_id => @post1.id, :voter_id => @user1.id, :value => :up)
176
+ @post1.reload
177
+
178
+ @post1.up_votes_count.should == 1
179
+ @post1.down_votes_count.should == 0
180
+ @post1.votes_count.should == 1
181
+ @post1.votes_point.should == 1
182
+
183
+ @post1.vote_value(@user1.id).should == :up
184
+ end
185
+ end
186
+
187
+ context 'user2 vote down post1 the first time' do
188
+ before :all do
189
+ Post.vote(:votee_id => @post1.id, :voter_id => @user2.id, :value => :down)
190
+ @post1.reload
191
+ end
192
+
193
+ it 'post1 up_votes_count is the same' do
194
+ @post1.up_votes_count.should == 1
195
+ end
196
+
197
+ it 'post1 vote_value on user1 is the same' do
198
+ @post1.vote_value(@user1.id).should == :up
199
+ end
200
+
201
+ it 'down_votes_count, votes_count, and votes_point changed' do
202
+ @post1.down_votes_count.should == 1
203
+ @post1.votes_count.should == 2
204
+ @post1.votes_point.should == 0
205
+ @post1.vote_value(@user2.id).should == :down
206
+ end
207
+
208
+ it 'post1 get voters' do
209
+ @post1.up_voters(User).should == [ @user1 ]
210
+ @post1.down_voters(User).should == [ @user2 ]
211
+ @post1.voters(User).should == [ @user1, @user2 ]
212
+ end
213
+
214
+ it 'posts voted_by user1, user2 is post1 only' do
215
+ Post.voted_by(@user1).to_a.should == [ @post1 ]
216
+ Post.voted_by(@user2).to_a.should == [ @post1 ]
217
+ end
218
+
219
+ it 'categories votes' do
220
+ @category1.reload
221
+ @category1.up_votes_count.should == 0
222
+ @category1.down_votes_count.should == 0
223
+ @category1.votes_count.should == 0
224
+ @category1.votes_point.should == -2
225
+
226
+ @category2.reload
227
+ @category2.up_votes_count.should == 0
228
+ @category2.down_votes_count.should == 0
229
+ @category2.votes_count.should == 0
230
+ @category2.votes_point.should == -2
231
+ end
232
+ end
233
+
234
+ context 'user1 change vote on post1 from up to down' do
235
+ before :all do
236
+ Post.vote(:revote => true, :votee_id => @post1.id, :voter_id => @user1.id, :value => :down)
237
+ Mongo::Voteable::Tasks.remake_stats
238
+ @post1.reload
239
+ end
240
+
241
+ it 'validates' do
242
+ @post1.up_votes_count.should == 0
243
+ @post1.down_votes_count.should == 2
244
+ @post1.votes_count.should == 2
245
+ @post1.votes_point.should == -2
246
+
247
+ @post1.vote_value(@user1.id).should == :down
248
+ @post1.vote_value(@user2.id).should == :down
249
+
250
+ Post.voted_by(@user1).to_a.should == [ @post1 ]
251
+ Post.voted_by(@user2).to_a.should == [ @post1 ]
252
+ end
253
+ end
254
+
255
+ context 'user1 vote down post2 the first time' do
256
+ before :all do
257
+ @post2.vote(:voter_id => @user1.id, :value => :down)
258
+ end
259
+
260
+ it 'validates' do
261
+ @post2.up_votes_count.should == 0
262
+ @post2.down_votes_count.should == 1
263
+ @post2.votes_count.should == 1
264
+ @post2.votes_point.should == -1
265
+
266
+ @post2.vote_value(@user1.id).should == :down
267
+ @post2.vote_value(@user2.id).should be_nil
268
+
269
+ Post.voted_by(@user1).to_a.should == [ @post1, @post2 ]
270
+ end
271
+ end
272
+
273
+ context 'user1 change vote on post2 from down to up' do
274
+ before :all do
275
+ Post.vote(:revote => true, :votee_id => @post2.id.to_s, :voter_id => @user1.id.to_s, :value => :up)
276
+ Mongo::Voteable::Tasks.remake_stats
277
+ @post2.reload
278
+ end
279
+
280
+ it 'validates' do
281
+ @post2.up_votes_count.should == 1
282
+ @post2.down_votes_count.should == 0
283
+ @post2.votes_count.should == 1
284
+ @post2.votes_point.should == 1
285
+
286
+ @post2.vote_value(@user1.id).should == :up
287
+ @post2.vote_value(@user2.id).should be_nil
288
+
289
+ Post.voted_by(@user1).sort.should == [ @post1, @post2 ].sort
290
+ end
291
+ end
292
+
293
+
294
+ context 'user1 vote up post2 comment the first time' do
295
+ before :all do
296
+ @comment.vote(:voter_id => @user1.id, :value => :up)
297
+ @comment.reload
298
+ @post2.reload
299
+ end
300
+
301
+ it 'validates' do
302
+ @post2.up_votes_count.should == 2
303
+ @post2.down_votes_count.should == 0
304
+ @post2.votes_count.should == 2
305
+ @post2.votes_point.should == 3
306
+
307
+ @comment.up_votes_count.should == 1
308
+ @comment.down_votes_count.should == 0
309
+ @comment.votes_count.should == 1
310
+ @comment.votes_point.should == 1
311
+ end
312
+ end
313
+
314
+
315
+ context 'user1 revote post2 comment from up to down' do
316
+ before :all do
317
+ @user1.vote(:votee => @comment, :value => :down)
318
+ @comment.reload
319
+ @post2.reload
320
+ end
321
+
322
+ it 'validates' do
323
+ @post2.up_votes_count.should == 1
324
+ @post2.down_votes_count.should == 1
325
+ @post2.votes_count.should == 2
326
+ @post2.votes_point.should == 0
327
+
328
+ @comment.up_votes_count.should == 0
329
+ @comment.down_votes_count.should == 1
330
+ @comment.votes_count.should == 1
331
+ @comment.votes_point.should == -3
332
+ end
333
+
334
+ it 'revote with wrong value has no effect' do
335
+ @user1.vote(:votee => @comment, :value => :down)
336
+
337
+ @post2.up_votes_count.should == 1
338
+ @post2.down_votes_count.should == 1
339
+ @post2.votes_count.should == 2
340
+ @post2.votes_point.should == 0
341
+
342
+ @comment.up_votes_count.should == 0
343
+ @comment.down_votes_count.should == 1
344
+ @comment.votes_count.should == 1
345
+ @comment.votes_point.should == -3
346
+ end
347
+ end
348
+
349
+ context "user1 unvote on post1" do
350
+ before(:all) do
351
+ @post1.vote(:voter_id => @user1.id, :votee_id => @post1.id, :unvote => true)
352
+ Mongo::Voteable::Tasks.remake_stats
353
+ @post1.reload
354
+ end
355
+
356
+ it 'validates' do
357
+ @post1.up_votes_count.should == 0
358
+ @post1.down_votes_count.should == 1
359
+ @post1.votes_count.should == 1
360
+ @post1.votes_point.should == -1
361
+
362
+ @post1.vote_value(@user1.id).should be_nil
363
+ @post1.vote_value(@user2.id).should == :down
364
+
365
+ Post.voted_by(@user1).to_a.should_not include(@post1)
366
+ end
367
+ end
368
+
369
+ context "@post1 has 1 down vote and -1 point, @post2 has 1 up vote, 1 down vote and 0 point" do
370
+ it "verify @post1 counters" do
371
+ @post1.up_votes_count.should == 0
372
+ @post1.down_votes_count.should == 1
373
+ @post1.votes_count.should == 1
374
+ @post1.votes_point.should == -1
375
+ end
376
+
377
+ it "verify @post2 counters" do
378
+ @post2.up_votes_count.should == 1
379
+ @post2.down_votes_count.should == 1
380
+ @post2.votes_count.should == 2
381
+ @post2.votes_point.should == 0
382
+ end
383
+ end
384
+
385
+ context "user1 unvote on comment" do
386
+ before(:all) do
387
+ @user1.unvote(@comment)
388
+ Mongo::Voteable::Tasks.remake_stats
389
+ @comment.reload
390
+ @post2.reload
391
+ end
392
+
393
+ it "" do
394
+ @comment.up_votes_count.should == 0
395
+ @comment.down_votes_count.should == 0
396
+ @comment.votes_count.should == 0
397
+ @comment.votes_point.should == 0
398
+
399
+ @post2.up_votes_count.should == 1
400
+ @post2.down_votes_count.should == 0
401
+ @post2.votes_count.should == 1
402
+ @post2.votes_point.should == 1
403
+ end
404
+ end
405
+
406
+ context 'final' do
407
+ it "test remake stats" do
408
+ Mongo::Voteable::Tasks.remake_stats
409
+
410
+ @post1.up_votes_count.should == 0
411
+ @post1.down_votes_count.should == 1
412
+ @post1.votes_count.should == 1
413
+ @post1.votes_point.should == -1
414
+
415
+ @post2.up_votes_count.should == 1
416
+ @post2.down_votes_count.should == 0
417
+ @post2.votes_count.should == 1
418
+ @post2.votes_point.should == 1
419
+
420
+ @comment.up_votes_count.should == 0
421
+ @comment.down_votes_count.should == 0
422
+ @comment.votes_count.should == 0
423
+ @comment.votes_point.should == 0
424
+ end
425
+ end
426
+ end