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
/
is_rateable_by_spec.rb
164 lines (126 loc) · 4.45 KB
/
is_rateable_by_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
describe DataMapper::Is::Rateable do
def unload_consts(*consts)
consts.each do |c|
c = "#{c}"
Object.send(:remove_const, c) if Object.const_defined?(c)
end
end
# --------------------------------------------------------------------------------------------------
# SCENARIO
# --------------------------------------------------------------------------------------------------
before do
class Account
include DataMapper::Resource
property :id, Serial
end
class Trip
include DataMapper::Resource
property :id, Serial
is :rateable, :by => :accounts
end
[Trip,Account,TripRatingByAccount].each(&:auto_migrate!)
end
after do
unload_consts(Trip,Account,TripRatingByAccount)
end
let(:rateable_model){ Trip }
let(:rater_model) { Account }
let(:rating_model) { TripRatingByAccount }
# --------------------------------------------------------------------------------------------------
# RATEABLE
# --------------------------------------------------------------------------------------------------
describe 'Rateable' do
describe 'Model' do
subject{ rateable_model }
its(:rating_configs) { should == {
'TripRatingByAccount' => {
:by => {
:name => :account,
:key => :account_id,
:model => 'Account',
:type => Integer,
:options => {:required => true, :min => 0}
},
:with => 0..5,
:as => :ratings_by_accounts,
:model => 'TripRatingByAccount',
:timestamps => true
}
} }
its(:relationships) { should be_named(:ratings_by_accounts) }
end
describe 'Instance' do
let(:rateable) { rateable_model.create }
subject{ rateable }
context 'when no rating exists' do
it{ rateable.average_rating_of(rater_model).should == nil }
its(:average_rating_by_accounts) { should == nil }
end
context 'when one rating exists' do
before{ rateable.rate(1,rater_model.create) }
it{ rateable.average_rating_of(rater_model).should == 1 }
its(:average_rating_by_accounts) { should == 1 }
end
context 'when multiple ratings exist' do
let(:avg) { ratings.sum.to_f/ratings.length }
let(:ratings) { 10.times.map{ (0..5).to_a.sample } }
before{ ratings.each{ |r| rateable.rate(r,rater_model.create) } }
it{ rateable.average_rating_of(rater_model).should == avg }
its(:average_rating_by_accounts) { should == avg }
end
end
end
# --------------------------------------------------------------------------------------------------
# RATER
# --------------------------------------------------------------------------------------------------
describe 'Rater' do
describe 'Model' do
subject{ rater_model }
its(:relationships){ should be_named(:trip_account_ratings) }
end
describe 'Instance' do
let(:rater) { rater_model.create }
subject{ rater }
its(:trip_ratings) { should be_empty }
context 'when rated a rateable' do
let(:rateable) { rateable_model.create }
before{ rateable.rate(1,rater) }
its(:trip_ratings) { should have(1).entry }
its(:'trip_ratings.first.rating') { should == 1 }
end
context 'when rated multiple rateables' do
let(:rateables) { 3.times.map{ rateable_model.create } }
before{ rateables.each{ |r| r.rate(1,rater) } }
its(:trip_ratings) { should have(rateables.length).entries }
its(:'trip_ratings.first.rating') { should == 1 }
end
end
end
# --------------------------------------------------------------------------------------------------
# RATING
# --------------------------------------------------------------------------------------------------
describe 'Rating' do
describe 'Model' do
subject{ rating_model }
its(:properties) { should be_named(:trip_id) }
its(:properties) { should be_named(:account_id) }
its(:properties) { should be_named(:rating) }
its(:properties) { should be_named(:created_at) }
its(:properties) { should be_named(:updated_at) }
its(:relationships) { should be_named(:account) }
its(:relationships) { should be_named(:trip) }
its(:instance_methods) { should include(:rater) }
its(:instance_methods) { should include(:rateable) }
end
describe 'Instance' do
let(:rateable){ rateable_model.create }
let(:rater) { rater_model.create }
let(:rating) { rateable.rating_of(rater) }
before { rateable.rate(1,rater) }
subject{ rating }
its(:rating) { should == 1 }
its(:rateable){ should == rateable }
its(:rater) { should == rater }
end
end
end