This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
forked from snusnu/dm-is-rateable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rateable_spec.rb
676 lines (528 loc) · 19.4 KB
/
rateable_spec.rb
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
describe DataMapper::Is::Rateable do
unless HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
fail 'need a database for testing (e.g. sqlite3)'
end
def unload_consts(*consts)
consts.each do |c|
c = "#{c}"
Object.send(:remove_const, c) if Object.const_defined?(c)
end
end
setup_rateable = ->(&code) {
class User
include DataMapper::Resource
property :id, Serial
end
class Account
include DataMapper::Resource
property :id, Serial
end
class Trip
include DataMapper::Resource
property :id, Serial
end
Trip.class_eval(&code)
[Trip,Account,User].each(&:auto_migrate!)
[Trip,Account,User]
}
after do
unload_consts('User','Account','Trip','TripUserRating','TripAccountRating')
end
# --------------------------------------------------------------------------------------------------
#
# SHARED CONTEXTS
#
# --------------------------------------------------------------------------------------------------
# ----------------------------------------
shared_context 'is :rateable, :by => :users', :rateable => :by_users do
before do
setup_rateable.call do
is :rateable, :by => :users
end
[TripUserRating].each(&:auto_migrate!)
end
let(:rateable_class) { Trip }
let(:rating_class) { TripUserRating }
let(:rater_class) { User }
end
# ----------------------------------------
shared_context 'is :rateable, :by => User', :rateable => :by_user_model do
before do
setup_rateable.call do
is :rateable, :by => User
end
[TripUserRating].each(&:auto_migrate!)
end
let(:rateable_class) { Trip }
let(:rating_class) { TripUserRating }
let(:rater_class) { User }
end
# ----------------------------------------
shared_context 'is :rateable, :by => :accounts', :rateable => :by_accounts do
before do
setup_rateable.call do
is :rateable, :by => :accounts
end
[TripAccountRating].each(&:auto_migrate!)
end
let(:rateable_class) { Trip }
let(:rating_class) { TripAccountRating }
let(:rater_class) { Account }
end
# ----------------------------------------
shared_context 'is :rateable, :by => Account', :rateable => :by_account_model do
before do
setup_rateable.call do
is :rateable, :by => Account
end
[TripAccountRating].each(&:auto_migrate!)
end
let(:rateable_class) { Trip }
let(:rating_class) { TripAccountRating }
let(:rater_class) { Account }
end
# ----------------------------------------
shared_context 'is :rateable, :by => :users, :as => :special_ratings', :rateable => :by_users_as_special_ratings do
before do
setup_rateable.call do
is :rateable, :by => :users, :as => :special_ratings
end
[TripUserRating].each(&:auto_migrate!)
end
let(:rateable_class) { Trip }
let(:rating_class) { TripUserRating }
let(:rater_class) { User }
end
# ----------------------------------------
shared_context 'is :rateable, :by => {:name => :author, :key => :user_id}', :rateable => :by_author_user_id do
before do
setup_rateable.call do
is :rateable, :by => {:name => :author, :key => :user_id}
end
[TripUserRating].each(&:auto_migrate!)
end
let(:rateable_class) { Trip }
let(:rating_class) { TripUserRating }
let(:rater_class) { User }
end
# --------------------------------------------------------------------------------------------------
#
# SHARED EXAMPLES
#
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
shared_examples :rateable_model do
let(:rateable_model) { raise 'rateable model not set' }
subject{ rateable_model }
its(:rating_configs) { should include('TripUserRating') }
its(:rateable_fk) { should == :trip_id }
describe :rating_config do
subject{ rateable_model.rating_configs['TripUserRating'] }
its([:as]) { should == :user_ratings }
its([:with]) { should == (0..5) }
its([:by]) { should == {
:name => :user,
:model => 'User',
:key => :user_id,
:type => Integer,
:options => {:required => true, :min => 0}
}}
end
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
shared_examples "every rating" do
let(:rated_model) { raise 'rated model not set' }
let(:rating_model) { raise 'rating model not set' }
let(:rating_model_name) { raise 'rating model name not set' }
let(:rateable) { raise 'rateable not set' }
describe :rating_model do
subject{ rating_model }
it{ expect { subject.auto_migrate! }.to_not raise_error }
its(:total_rating) { should == 0 }
end
describe :rated_model do
subject{ rated_model }
#its(:rateable_model) { should == rating_model.name }
#its(:rateable_key) { should == :trip_user_rating } # FIXME
#its(:rateable_fk) { should == :trip_id } # FIXME
#its(:total_rating) { should == 0 } # FIXME total_user_rating
[ :rating_togglable?,:anonymous_rating_togglable?,
#:rater_fk,
:rateable_fk].each do |method|
it{ should respond_to(method) }
end
it 'has accessor :allowed_ratings' do
pending
subject.allowed_ratings = (-3..3)
subject.allowed_ratings.should == (-3..3)
end
end
describe :rateable do
subject{ rateable }
[ # :ratings,
:rating_togglable?, :anonymous_rating_togglable?,
:rating_enabled?, :rating_disabled?, :anonymous_rating_enabled?,
:anonymous_rating_disabled?#, :user_rating
].each do |method|
it { should respond_to(method) }
end
#its(:average_user_rating) { should == 0 }
end
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
shared_examples :timestamped_ratings do
subject{ rating }
let(:rating) { raise 'rating not set' }
it { should respond_to(:created_at) }
it { should respond_to(:updated_at) }
#its(:created_at) { should_not be_nil }
#its(:updated_at) { should_not be_nil }
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
shared_examples 'rateable with rating enabled' do
subject{ rateable }
let(:rateable) { raise 'rateable not set' }
let(:rater) { raise 'rater not set' }
describe 'can be rated' do
it 'when rater passed' do
pending
rateable.rate(1,rater)
end
end
end
# --------------------------------------------------------------------------------------------------
#
# EXAMPLES
#
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'is :rateable, :by => :users' do
before do
setup_rateable.call do
is :rateable, :by => :users
end
[TripUserRating].each(&:auto_migrate!)
end
it_behaves_like :rateable_model do
let(:rateable_model) { Trip }
end
it_behaves_like 'every rating' do
let(:rating_model) { TripUserRating }
let(:rated_model) { Trip }
let(:rateable) { Trip.create }
end
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'is :rateable, :by => :users, :as => :ratings' do
before do
setup_rateable.call do
is :rateable, :by => :users, :as => :ratings
end
[TripUserRating].each(&:auto_migrate!)
end
describe :rateable do
subject{ Trip.create }
it { should respond_to(:ratings) }
it { should_not respond_to(:user_ratings) }
it { should respond_to(:average_rating) }
it { should_not respond_to(:average_user_rating) }
context 'when no rating supplied' do
its(:average_rating) { should be_nil }
end
context 'when rating supplied by rater' do
let(:rater) { User.create }
let(:rating) { 2 }
before{ subject.rate(rating,rater) }
its(:average_rating) { should == rating }
end
end
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'is :rateable, :by => :users and :accounts' do
before do
setup_rateable.call do
is :rateable, :by => :users
is :rateable, :by => :accounts
end
[TripUserRating,TripAccountRating].each(&:auto_migrate!)
end
#it_behaves_like 'every rating' do
# let(:rating_model) { TripUserRating }
# let(:rated_model) { Trip }
# let(:rateable) { Trip.create }
#end
it_behaves_like :rateable_model do
let(:rateable_model) { Trip }
end
describe :rateable do
let(:rateable) { Trip.create }
subject{ rateable }
it { should respond_to(:user_ratings) }
it { should respond_to(:account_ratings) }
it { should_not respond_to(:ratings) }
it { should respond_to(:average_user_rating) }
it { should respond_to(:average_account_rating) }
it { should_not respond_to(:average_rating) }
context 'when no rating supplied' do
its(:average_user_rating) { should be_nil }
its(:average_account_rating) { should be_nil }
end
context 'when rating supplied by user' do
let(:rater) { User.create }
let(:rating) { 2 }
before{ subject.rate(rating,rater) }
its(:average_user_rating) { should == rating }
its(:average_account_rating) { should be_nil }
end
context 'when rating supplied by account' do
let(:rater) { Account.create }
let(:rating) { 2 }
before{ subject.rate(rating,rater) }
its(:average_account_rating) { should == rating }
its(:average_user_rating) { should be_nil }
end
end
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'when rateable twice by same rater' do
it 'raises error' do
expect {
setup_rateable.call do
is :rateable, :by => :users, :as => :special_ratings
is :rateable, :by => :users, :as => :regular_ratings
end
}.to raise_error
end
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'is :rateable, :by => :users with enhancements' do
before do
setup_rateable.call do
is :rateable, :by => :users do
def extra_method; true; end
end
end
[TripUserRating].each(&:auto_migrate!)
end
describe :rating do
let(:rater) { User.create }
let(:rateable) { Trip.create }
let(:rating) { rateable.rate(1,rater) }
subject{ rating }
it { should respond_to(:extra_method) }
its(:extra_method) { should be_true }
end
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'is :rateable, :by => :users, :model => not implemented' do
it ':model option not implemented' do
expect{ setup_rateable.call do
is :rateable, :by => :users, :model => 'AwkwardRating'
end }.to raise_error(NotImplementedError)
end
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'Rateable Class' do
# before do
# setup_rateable.call do
# is :rateable, :by => :users
# end
#
# [TripUserRating].each(&:auto_migrate!)
# end
#
# let(:rateable_model){ Trip }
# let(:rater_model) { User }
# let(:rating_model) { TripUserRating }
describe '#rateable_fk' do
subject{ rateable_model.rateable_fk }
context 'when ' do
it{ should == :trip_id }
end
end
# #rating_config_for
describe '#rating_config_for' do
context 'when user model passed' do
subject{ rateable_model.rating_config_for(rater_model) }
it{ should be_a(Hash) }
its([:by]){ should include(:model => rater_model.name) }
end
context 'when association name passed' do
let(:assoc) { :users }
subject{ rateable_model.rating_config_for(assoc) }
it{ should be_a(Hash) }
its([:by]) { should include(:name => assoc.to_s.singularize.to_sym) }
its([:by]) { should include(:model => rater_model.name) }
end
end
# #rating_model_for
describe '#rating_model_for' do
context 'when user model passed' do
subject{ rateable_model.rating_model_for(rater_model) }
it{ should == rating_model }
end
context 'when association name passed' do
let(:assoc) { :users }
subject{ rateable_model.rating_model_for(assoc) }
it{ should == rating_model }
end
end
=begin
# #average_rating_for
describe '#average_rating_for' do
let(:rateable) { rateable_model.create }
subject{ rateable_model.average_rating_of(rateable,rater_model) }
context 'when no ratings exist' do
it{ should be_nil }
end
context 'when a rating exists' do
let(:rater) { rater_model.create }
let(:value) { 2 }
let!(:rating) { rateable.rate(value, rater) }
it{ should == value }
end
context 'when multiple ratings exist' do
let(:raters) { 3.times.map{ rater_model.create } }
let(:value) { rand(5) }
let!(:ratings) { raters.map{ |r| rateable.rate(value, r) } }
it{ should == ratings.map(&:rating).sum.to_f / ratings.length }
end
end
=end
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'Rateable Instance' do
before do
setup_rateable.call do
is :rateable, :by => :users
end
[TripUserRating].each(&:auto_migrate!)
end
let(:rateable_model){ Trip }
let(:rater_model) { User }
let(:rateable) { Trip.create }
# #average_rating_of(User)
# #average_rating_of(:users)
describe '#average_rating_of' do
subject{ rateable.average_rating_of(param) }
shared_examples 'average ratings' do
let(:rater) { User.create }
context 'when no ratings present' do
it { should be_nil }
end
context 'when one rating present' do
let!(:rating) { rateable.rate(2, rater) }
it{ should == rating.rating }
end
context 'when ratings of multiple users present' do
let!(:ratings) { 4.times.map{ (0..5).to_a.sample } }
before{ ratings.each{ |r| rateable.rate(r, User.create) } }
it{ should == ratings.sum.to_f / ratings.length }
end
context 'when ratings on other rateables are present too' do
let(:other_rateable) { rateable_model.create }
let!(:ratings) { 4.times.map{ (0..5).to_a.sample } }
before{
ratings.each{ |r| rateable.rate(r, User.create) }
4.times{ other_rateable.rate((0..5).to_a.sample, User.create) }
}
it{ should == ratings.sum.to_f / ratings.length }
end
end
context 'when passing class' do
let(:param) { rater.class }
it_behaves_like 'average ratings'
end
context 'when passing :users' do
let(:param) { :users }
it_behaves_like 'average ratings'
end
end#average_rating_of
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'Rater' do
describe 'Class' do
subject{ rater_class }
#shared_examples :relationships do
# its(:relationships) { should be_named(:trip_user_ratings) }
#end
describe 'when rateable, :by => :users' do
include_context 'is :rateable, :by => :users' # FIXME rspec issue
its(:relationships) { should be_named(:trip_user_ratings) }
end
describe 'when rateable, :by => User' do
include_context 'is :rateable, :by => User' # FIXME rspec issue
its(:relationships) { should be_named(:trip_user_ratings) }
end
end#Class
describe 'Instance' do
end#Instance
end
# --------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------
describe 'Rating' do
describe 'Class' do
subject{ rating_class }
shared_examples :default_properties do
subject{ rating_class.properties }
#it { should be_named(:user_id) }
it { should be_named(:trip_id) }
it { should be_named(:rating) }
end
#describe 'when rateable by :users', :rateable => :by_users do
describe 'when rateable by :users' do
include_context 'is :rateable, :by => :users' # FIXME rspec issue
it_behaves_like :default_properties do
it { should be_named(:user_id) }
end
end
describe 'when rateable by User' do
include_context 'is :rateable, :by => User'
it_behaves_like :default_properties do
it { should be_named(:user_id) }
end
end
describe 'when rateable by Account' do
include_context 'is :rateable, :by => Account'
it_behaves_like :default_properties do
it { should be_named(:account_id) }
end
end
describe 'when rateable by :accounts' do
include_context 'is :rateable, :by => :accounts'
it_behaves_like :default_properties do
it { should be_named(:account_id) }
end
end
describe 'when rateable by :users as :special_ratings' do
include_context 'is :rateable, :by => :users, :as => :special_ratings'
it_behaves_like :default_properties do
it { should be_named(:user_id) }
end
end
describe 'when is :rateable, :by => {:name => :author, :key => :user_id}' do
include_context 'is :rateable, :by => {:name => :author, :key => :user_id}'
it_behaves_like :default_properties do
it { should be_named(:user_id) }
end
end
end
describe 'Instance' do
end
end
end