rating 0.9.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +36 -10
- data/README.md +40 -16
- data/lib/rating/models/rating/extension.rb +1 -2
- data/lib/rating/models/rating/rate.rb +3 -7
- data/lib/rating/models/rating/rating.rb +25 -5
- data/lib/rating/version.rb +1 -1
- metadata +56 -122
- data/spec/config/rate_table_spec.rb +0 -17
- data/spec/config/rating_table_spec.rb +0 -17
- data/spec/config/validations_spec.rb +0 -33
- data/spec/factories/article.rb +0 -7
- data/spec/factories/author.rb +0 -7
- data/spec/factories/category.rb +0 -7
- data/spec/factories/comment.rb +0 -5
- data/spec/factories/global.rb +0 -6
- data/spec/factories/rating/rate.rb +0 -10
- data/spec/factories/rating/rating.rb +0 -12
- data/spec/models/extension/after_create_spec.rb +0 -47
- data/spec/models/extension/order_by_rating_spec.rb +0 -180
- data/spec/models/extension/rate_for_spec.rb +0 -41
- data/spec/models/extension/rate_spec.rb +0 -60
- data/spec/models/extension/rated_question_spec.rb +0 -48
- data/spec/models/extension/rated_records_spec.rb +0 -12
- data/spec/models/extension/rated_spec.rb +0 -50
- data/spec/models/extension/rates_records_spec.rb +0 -12
- data/spec/models/extension/rates_spec.rb +0 -50
- data/spec/models/extension/rating_records_spec.rb +0 -12
- data/spec/models/extension/rating_spec.rb +0 -40
- data/spec/models/extension/rating_warm_up_spec.rb +0 -115
- data/spec/models/extension/unscoped_rating_spec.rb +0 -121
- data/spec/models/rate/create_spec.rb +0 -364
- data/spec/models/rate/rate_for_spec.rb +0 -107
- data/spec/models/rate_spec.rb +0 -28
- data/spec/models/rating/averager_data_spec.rb +0 -32
- data/spec/models/rating/data_spec.rb +0 -48
- data/spec/models/rating/update_rating_spec.rb +0 -48
- data/spec/models/rating/values_data_spec.rb +0 -40
- data/spec/models/rating_spec.rb +0 -24
- data/spec/rails_helper.rb +0 -17
- data/spec/support/common.rb +0 -22
- data/spec/support/database_cleaner.rb +0 -19
- data/spec/support/db/migrate/add_comment_on_rating_rates_table.rb +0 -8
- data/spec/support/db/migrate/add_extra_fields_on_rating_rates_table.rb +0 -8
- data/spec/support/db/migrate/create_articles_table.rb +0 -9
- data/spec/support/db/migrate/create_authors_table.rb +0 -9
- data/spec/support/db/migrate/create_categories_table.rb +0 -12
- data/spec/support/db/migrate/create_comments_table.rb +0 -7
- data/spec/support/db/migrate/create_globals_table.rb +0 -8
- data/spec/support/db/migrate/create_rates_table.rb +0 -19
- data/spec/support/db/migrate/create_rating_table.rb +0 -20
- data/spec/support/db/migrate/create_review_ratings_table.rb +0 -17
- data/spec/support/db/migrate/create_reviews_table.rb +0 -15
- data/spec/support/factory_bot.rb +0 -9
- data/spec/support/migrate.rb +0 -20
- data/spec/support/models/article.rb +0 -7
- data/spec/support/models/author.rb +0 -5
- data/spec/support/models/category.rb +0 -6
- data/spec/support/models/comment.rb +0 -5
- data/spec/support/models/global.rb +0 -7
- data/spec/support/models/review.rb +0 -5
- data/spec/support/models/review_rating.rb +0 -4
- data/spec/support/shared_context/with_database_records.rb +0 -20
- data/spec/support/shoulda.rb +0 -10
@@ -1,121 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe Rating::Extension, 'unscoped_rating' do
|
6
|
-
let!(:author_1) { create :author }
|
7
|
-
let!(:author_2) { create :author }
|
8
|
-
let!(:scope) { create :category }
|
9
|
-
|
10
|
-
context 'when is false' do
|
11
|
-
let!(:resource) { create :article }
|
12
|
-
|
13
|
-
it 'groups in different line record' do
|
14
|
-
author_1.rate resource, 1, scope: scope
|
15
|
-
author_2.rate resource, 2, scope: scope
|
16
|
-
author_1.rate resource, 5
|
17
|
-
|
18
|
-
ratings = Rating::Rating.all.order('id')
|
19
|
-
|
20
|
-
expect(ratings.size).to eq 2
|
21
|
-
|
22
|
-
rating = ratings[0]
|
23
|
-
|
24
|
-
expect(rating.average.to_s).to eq '1.5'
|
25
|
-
expect(rating.estimate.to_s).to eq '1.5'
|
26
|
-
expect(rating.resource).to eq resource
|
27
|
-
expect(rating.scopeable).to eq scope
|
28
|
-
expect(rating.sum).to eq 3
|
29
|
-
expect(rating.total).to eq 2
|
30
|
-
|
31
|
-
rating = ratings[1]
|
32
|
-
|
33
|
-
expect(rating.average.to_s).to eq '5.0'
|
34
|
-
expect(rating.estimate.to_s).to eq '5.0'
|
35
|
-
expect(rating.resource).to eq resource
|
36
|
-
expect(rating.scopeable).to eq nil
|
37
|
-
expect(rating.sum).to eq 5
|
38
|
-
expect(rating.total).to eq 1
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context 'when is true' do
|
43
|
-
let!(:resource) { create :global }
|
44
|
-
|
45
|
-
it 'groups in the same line record' do
|
46
|
-
author_1.rate resource, 1, scope: scope
|
47
|
-
author_2.rate resource, 2, scope: scope
|
48
|
-
author_1.rate resource, 5
|
49
|
-
|
50
|
-
ratings = Rating::Rating.all.order('id')
|
51
|
-
|
52
|
-
expect(ratings.size).to eq 1
|
53
|
-
|
54
|
-
rating = ratings[0]
|
55
|
-
|
56
|
-
expect(rating.average.to_s).to eq '2.6666666666666667'
|
57
|
-
expect(rating.estimate.to_s).to eq '2.6666666666666667'
|
58
|
-
expect(rating.resource).to eq resource
|
59
|
-
expect(rating.scopeable).to eq nil
|
60
|
-
expect(rating.sum).to eq 8
|
61
|
-
expect(rating.total).to eq 3
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context 'when is true' do
|
66
|
-
let!(:resource) { create :global }
|
67
|
-
|
68
|
-
it 'groups in the same line record' do
|
69
|
-
author_1.rate resource, 1, scope: scope
|
70
|
-
author_2.rate resource, 2, scope: scope
|
71
|
-
author_1.rate resource, 5
|
72
|
-
|
73
|
-
ratings = Rating::Rating.all.order('id')
|
74
|
-
|
75
|
-
expect(ratings.size).to eq 1
|
76
|
-
|
77
|
-
rating = ratings[0]
|
78
|
-
|
79
|
-
expect(rating.average.to_s).to eq '2.6666666666666667'
|
80
|
-
expect(rating.estimate.to_s).to eq '2.6666666666666667'
|
81
|
-
expect(rating.resource).to eq resource
|
82
|
-
expect(rating.scopeable).to eq nil
|
83
|
-
expect(rating.sum).to eq 8
|
84
|
-
expect(rating.total).to eq 3
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context 'when is true and have a non scopeable record first on dabase' do
|
89
|
-
let!(:resource) { create :global }
|
90
|
-
|
91
|
-
before { ::Rating::Rating.create resource: resource, scopeable: scope }
|
92
|
-
|
93
|
-
it 'groups in the line with no scope' do
|
94
|
-
author_1.rate resource, 1, scope: scope
|
95
|
-
author_2.rate resource, 2, scope: scope
|
96
|
-
author_1.rate resource, 5
|
97
|
-
|
98
|
-
ratings = Rating::Rating.all.order('id')
|
99
|
-
|
100
|
-
expect(ratings.size).to eq 2
|
101
|
-
|
102
|
-
rating = ratings[0]
|
103
|
-
|
104
|
-
expect(rating.average.to_s).to eq '0.0'
|
105
|
-
expect(rating.estimate.to_s).to eq '0.0'
|
106
|
-
expect(rating.resource).to eq resource
|
107
|
-
expect(rating.scopeable).to eq scope
|
108
|
-
expect(rating.sum).to eq 0
|
109
|
-
expect(rating.total).to eq 0
|
110
|
-
|
111
|
-
rating = ratings[1]
|
112
|
-
|
113
|
-
expect(rating.average.to_s).to eq '2.6666666666666667'
|
114
|
-
expect(rating.estimate.to_s).to eq '2.6666666666666667'
|
115
|
-
expect(rating.resource).to eq resource
|
116
|
-
expect(rating.scopeable).to eq nil
|
117
|
-
expect(rating.sum).to eq 8
|
118
|
-
expect(rating.total).to eq 3
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
@@ -1,364 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe Rating::Rate, ':create' do
|
6
|
-
let!(:author) { create :author }
|
7
|
-
let!(:article) { create :article }
|
8
|
-
|
9
|
-
context 'with no scopeable' do
|
10
|
-
before { described_class.create author: author, extra_scopes: {}, metadata: {}, resource: article, value: 3 }
|
11
|
-
|
12
|
-
context 'when rate does not exist yet' do
|
13
|
-
it 'creates a rate entry' do
|
14
|
-
rate = described_class.last
|
15
|
-
|
16
|
-
expect(rate.author).to eq author
|
17
|
-
expect(rate.resource).to eq article
|
18
|
-
expect(rate.value).to eq 3
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'creates a rating entry' do
|
22
|
-
rating = Rating::Rating.last
|
23
|
-
|
24
|
-
expect(rating.average).to eq 3
|
25
|
-
expect(rating.estimate).to eq 3
|
26
|
-
expect(rating.resource).to eq article
|
27
|
-
expect(rating.sum).to eq 3
|
28
|
-
expect(rating.total).to eq 1
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'when rate already exists' do
|
33
|
-
let!(:author_2) { create :author }
|
34
|
-
|
35
|
-
before { described_class.create author: author_2, extra_scopes: {}, metadata: {}, resource: article, value: 4 }
|
36
|
-
|
37
|
-
it 'creates one more rate entry' do
|
38
|
-
rates = described_class.where(author: [author, author_2]).order('created_at asc')
|
39
|
-
|
40
|
-
expect(rates.size).to eq 2
|
41
|
-
|
42
|
-
rate = rates[0]
|
43
|
-
|
44
|
-
expect(rate.author).to eq author
|
45
|
-
expect(rate.resource).to eq article
|
46
|
-
expect(rate.value).to eq 3
|
47
|
-
|
48
|
-
rate = rates[1]
|
49
|
-
|
50
|
-
expect(rate.author).to eq author_2
|
51
|
-
expect(rate.resource).to eq article
|
52
|
-
expect(rate.value).to eq 4
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'updates the unique rating entry' do
|
56
|
-
rating = Rating::Rating.find_by(resource: article)
|
57
|
-
|
58
|
-
expect(rating.average).to eq 3.5
|
59
|
-
expect(rating.estimate).to eq 3.5
|
60
|
-
expect(rating.resource).to eq article
|
61
|
-
expect(rating.sum).to eq 7
|
62
|
-
expect(rating.total).to eq 2
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
context 'with scopeable' do
|
68
|
-
let!(:category) { create :category }
|
69
|
-
|
70
|
-
before do
|
71
|
-
described_class.create(
|
72
|
-
author: author,
|
73
|
-
extra_scopes: {},
|
74
|
-
metadata: {},
|
75
|
-
resource: article,
|
76
|
-
scopeable: category,
|
77
|
-
value: 3
|
78
|
-
)
|
79
|
-
end
|
80
|
-
|
81
|
-
context 'when rate does not exist yet' do
|
82
|
-
it 'creates a rate entry' do
|
83
|
-
rate = described_class.last
|
84
|
-
|
85
|
-
expect(rate.author).to eq author
|
86
|
-
expect(rate.resource).to eq article
|
87
|
-
expect(rate.scopeable).to eq category
|
88
|
-
expect(rate.value).to eq 3
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'creates a rating entry' do
|
92
|
-
rating = Rating::Rating.last
|
93
|
-
|
94
|
-
expect(rating.average).to eq 3
|
95
|
-
expect(rating.estimate).to eq 3
|
96
|
-
expect(rating.resource).to eq article
|
97
|
-
expect(rating.scopeable).to eq category
|
98
|
-
expect(rating.sum).to eq 3
|
99
|
-
expect(rating.total).to eq 1
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
context 'when rate already exists' do
|
104
|
-
let!(:author_2) { create :author }
|
105
|
-
|
106
|
-
before do
|
107
|
-
described_class.create(
|
108
|
-
author: author_2,
|
109
|
-
extra_scopes: {},
|
110
|
-
metadata: {},
|
111
|
-
resource: article,
|
112
|
-
scopeable: category,
|
113
|
-
value: 4
|
114
|
-
)
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'creates one more rate entry' do
|
118
|
-
rates = described_class.where(author: [author, author_2]).order('created_at asc')
|
119
|
-
|
120
|
-
expect(rates.size).to eq 2
|
121
|
-
|
122
|
-
rate = rates[0]
|
123
|
-
|
124
|
-
expect(rate.author).to eq author
|
125
|
-
expect(rate.resource).to eq article
|
126
|
-
expect(rate.scopeable).to eq category
|
127
|
-
expect(rate.value).to eq 3
|
128
|
-
|
129
|
-
rate = rates[1]
|
130
|
-
|
131
|
-
expect(rate.author).to eq author_2
|
132
|
-
expect(rate.resource).to eq article
|
133
|
-
expect(rate.scopeable).to eq category
|
134
|
-
expect(rate.value).to eq 4
|
135
|
-
end
|
136
|
-
|
137
|
-
it 'updates the unique rating entry' do
|
138
|
-
rating = Rating::Rating.find_by(resource: article, scopeable: category)
|
139
|
-
|
140
|
-
expect(rating.average).to eq 3.5
|
141
|
-
expect(rating.estimate).to eq 3.5
|
142
|
-
expect(rating.resource).to eq article
|
143
|
-
expect(rating.scopeable).to eq category
|
144
|
-
expect(rating.sum).to eq 7
|
145
|
-
expect(rating.total).to eq 2
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
context 'with metadata' do
|
151
|
-
context 'with nil value' do
|
152
|
-
it 'creates a rate entry ignoring metadata' do
|
153
|
-
described_class.create author: author, extra_scopes: {}, metadata: nil, resource: article, value: 3
|
154
|
-
|
155
|
-
rate = described_class.last
|
156
|
-
|
157
|
-
expect(rate.author).to eq author
|
158
|
-
expect(rate.comment).to eq nil
|
159
|
-
expect(rate.resource).to eq article
|
160
|
-
expect(rate.value).to eq 3
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
context 'with empty value' do
|
165
|
-
it 'creates a rate entry ignoring metadata' do
|
166
|
-
described_class.create author: author, extra_scopes: {}, metadata: '', resource: article, value: 3
|
167
|
-
|
168
|
-
rate = described_class.last
|
169
|
-
|
170
|
-
expect(rate.author).to eq author
|
171
|
-
expect(rate.comment).to eq nil
|
172
|
-
expect(rate.resource).to eq article
|
173
|
-
expect(rate.value).to eq 3
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
context 'with hash value' do
|
178
|
-
it 'creates a rate entry with metadata included' do
|
179
|
-
described_class.create(
|
180
|
-
author: author,
|
181
|
-
extra_scopes: {},
|
182
|
-
metadata: { comment: 'comment' },
|
183
|
-
resource: article,
|
184
|
-
value: 3
|
185
|
-
)
|
186
|
-
|
187
|
-
rate = described_class.last
|
188
|
-
|
189
|
-
expect(rate.author).to eq author
|
190
|
-
expect(rate.comment).to eq 'comment'
|
191
|
-
expect(rate.resource).to eq article
|
192
|
-
expect(rate.value).to eq 3
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
|
198
|
-
context 'with extra scopes' do
|
199
|
-
let!(:category) { create :category }
|
200
|
-
|
201
|
-
it 'creates a rate entry' do
|
202
|
-
described_class.create(
|
203
|
-
author: author,
|
204
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
205
|
-
metadata: {},
|
206
|
-
resource: article,
|
207
|
-
scopeable: category,
|
208
|
-
value: 1
|
209
|
-
)
|
210
|
-
|
211
|
-
rate = described_class.last
|
212
|
-
|
213
|
-
expect(rate.author).to eq author
|
214
|
-
expect(rate.resource).to eq article
|
215
|
-
expect(rate.scope_1).to eq 'scope_1'
|
216
|
-
expect(rate.scope_2).to eq 'scope_2'
|
217
|
-
expect(rate.scopeable).to eq category
|
218
|
-
expect(rate.value).to eq 1
|
219
|
-
end
|
220
|
-
|
221
|
-
it 'creates a rating entry' do
|
222
|
-
described_class.create(
|
223
|
-
author: author,
|
224
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
225
|
-
metadata: {},
|
226
|
-
resource: article,
|
227
|
-
scopeable: category,
|
228
|
-
value: 1
|
229
|
-
)
|
230
|
-
|
231
|
-
rating = Rating::Rating.last
|
232
|
-
|
233
|
-
expect(rating.average).to eq 1
|
234
|
-
expect(rating.estimate).to eq 1
|
235
|
-
expect(rating.resource).to eq article
|
236
|
-
expect(rating.scopeable).to eq category
|
237
|
-
expect(rating.sum).to eq 1
|
238
|
-
expect(rating.total).to eq 1
|
239
|
-
end
|
240
|
-
|
241
|
-
context 'when rate already exists' do
|
242
|
-
before do
|
243
|
-
described_class.create(
|
244
|
-
author: author,
|
245
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
246
|
-
metadata: {},
|
247
|
-
resource: article,
|
248
|
-
scopeable: category,
|
249
|
-
value: 1
|
250
|
-
)
|
251
|
-
end
|
252
|
-
|
253
|
-
it 'updates the rate entry' do
|
254
|
-
described_class.create(
|
255
|
-
author: author,
|
256
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
257
|
-
metadata: {},
|
258
|
-
resource: article,
|
259
|
-
scopeable: category,
|
260
|
-
value: 2
|
261
|
-
)
|
262
|
-
|
263
|
-
rates = described_class.all
|
264
|
-
|
265
|
-
expect(rates.size).to eq 1
|
266
|
-
|
267
|
-
rate = rates[0]
|
268
|
-
|
269
|
-
expect(rate.author).to eq author
|
270
|
-
expect(rate.resource).to eq article
|
271
|
-
expect(rate.scope_1).to eq 'scope_1'
|
272
|
-
expect(rate.scope_2).to eq 'scope_2'
|
273
|
-
expect(rate.scopeable).to eq category
|
274
|
-
expect(rate.value).to eq 2
|
275
|
-
end
|
276
|
-
|
277
|
-
it 'updates the unique rating entry' do
|
278
|
-
described_class.create(
|
279
|
-
author: author,
|
280
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
281
|
-
metadata: {},
|
282
|
-
resource: article,
|
283
|
-
scopeable: category,
|
284
|
-
value: 2
|
285
|
-
)
|
286
|
-
|
287
|
-
ratings = Rating::Rating.all
|
288
|
-
|
289
|
-
expect(ratings.size).to eq 1
|
290
|
-
|
291
|
-
rating = ratings[0]
|
292
|
-
|
293
|
-
expect(rating.average).to eq 2
|
294
|
-
expect(rating.estimate).to eq 2
|
295
|
-
expect(rating.resource).to eq article
|
296
|
-
expect(rating.scopeable).to eq category
|
297
|
-
expect(rating.sum).to eq 2
|
298
|
-
expect(rating.total).to eq 1
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
context 'when rate already exists but with at least one extra scope different' do
|
303
|
-
before do
|
304
|
-
described_class.create(
|
305
|
-
author: author,
|
306
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
307
|
-
metadata: {},
|
308
|
-
resource: article,
|
309
|
-
scopeable: category,
|
310
|
-
value: 1
|
311
|
-
)
|
312
|
-
|
313
|
-
described_class.create(
|
314
|
-
author: author,
|
315
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_missing' },
|
316
|
-
metadata: {},
|
317
|
-
resource: article,
|
318
|
-
scopeable: category,
|
319
|
-
value: 2
|
320
|
-
)
|
321
|
-
end
|
322
|
-
|
323
|
-
it 'creates a new rate entry' do
|
324
|
-
rates = described_class.all
|
325
|
-
|
326
|
-
expect(rates.size).to eq 2
|
327
|
-
|
328
|
-
rate = rates[0]
|
329
|
-
|
330
|
-
expect(rate.author).to eq author
|
331
|
-
expect(rate.resource).to eq article
|
332
|
-
expect(rate.scope_1).to eq 'scope_1'
|
333
|
-
expect(rate.scope_2).to eq 'scope_2'
|
334
|
-
expect(rate.scopeable).to eq category
|
335
|
-
expect(rate.value).to eq 1
|
336
|
-
|
337
|
-
rate = rates[1]
|
338
|
-
|
339
|
-
expect(rate.author).to eq author
|
340
|
-
expect(rate.resource).to eq article
|
341
|
-
expect(rate.scope_1).to eq 'scope_1'
|
342
|
-
expect(rate.scope_2).to eq 'scope_missing'
|
343
|
-
expect(rate.scopeable).to eq category
|
344
|
-
expect(rate.value).to eq 2
|
345
|
-
end
|
346
|
-
|
347
|
-
it 'updates the unique rating entry' do
|
348
|
-
ratings = Rating::Rating.all
|
349
|
-
|
350
|
-
expect(ratings.size).to eq 1
|
351
|
-
|
352
|
-
rating = ratings[0]
|
353
|
-
|
354
|
-
expect(rating.average).to eq 1.5
|
355
|
-
expect(rating.estimate).to eq 1.5
|
356
|
-
expect(rating.resource).to eq article
|
357
|
-
expect(rating.scopeable).to eq category
|
358
|
-
expect(rating.sum).to eq 3
|
359
|
-
expect(rating.total).to eq 2
|
360
|
-
end
|
361
|
-
end
|
362
|
-
end
|
363
|
-
end
|
364
|
-
end
|
@@ -1,107 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe Rating::Rate, ':rate_for' do
|
6
|
-
let!(:author) { create :author }
|
7
|
-
let!(:article) { create :article }
|
8
|
-
|
9
|
-
context 'with no scopeable' do
|
10
|
-
context 'when rate does not exist' do
|
11
|
-
it { expect(described_class.rate_for(author: author, resource: article)).to eq nil }
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'when rate exists' do
|
15
|
-
let!(:record) do
|
16
|
-
described_class.create author: author, extra_scopes: {}, metadata: {}, resource: article, value: 3
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'returns the record' do
|
20
|
-
expect(described_class.rate_for(author: author, resource: article)).to eq record
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context 'with scopeable' do
|
26
|
-
let!(:category) { create :category }
|
27
|
-
|
28
|
-
context 'when rate does not exist' do
|
29
|
-
it do
|
30
|
-
expect(described_class.rate_for(author: author, resource: article, scopeable: category)).to eq nil
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'when rate exists' do
|
35
|
-
let!(:record) do
|
36
|
-
described_class.create(
|
37
|
-
author: author,
|
38
|
-
extra_scopes: {},
|
39
|
-
metadata: {},
|
40
|
-
resource: article,
|
41
|
-
scopeable: category,
|
42
|
-
value: 3
|
43
|
-
)
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'returns the record' do
|
47
|
-
query = described_class.rate_for(author: author, resource: article, scopeable: category)
|
48
|
-
|
49
|
-
expect(query).to eq record
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
|
55
|
-
context 'with extra scopes' do
|
56
|
-
let!(:category) { create :category }
|
57
|
-
|
58
|
-
context 'when matches all attributes including the extra scopes' do
|
59
|
-
let!(:record) do
|
60
|
-
described_class.create(
|
61
|
-
author: author,
|
62
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
63
|
-
metadata: {},
|
64
|
-
resource: article,
|
65
|
-
scopeable: category,
|
66
|
-
value: 1
|
67
|
-
)
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'returns the record' do
|
71
|
-
result = described_class.rate_for(
|
72
|
-
author: author,
|
73
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
74
|
-
resource: article,
|
75
|
-
scopeable: category
|
76
|
-
)
|
77
|
-
|
78
|
-
expect(result).to eq record
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
context 'when matches all attributes but at least one extra scopes' do
|
83
|
-
before do
|
84
|
-
described_class.create(
|
85
|
-
author: author,
|
86
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
87
|
-
metadata: {},
|
88
|
-
resource: article,
|
89
|
-
scopeable: category,
|
90
|
-
value: 1
|
91
|
-
)
|
92
|
-
end
|
93
|
-
|
94
|
-
it 'does not return the record' do
|
95
|
-
result = described_class.rate_for(
|
96
|
-
author: author,
|
97
|
-
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_missing' },
|
98
|
-
resource: article,
|
99
|
-
scopeable: category
|
100
|
-
)
|
101
|
-
|
102
|
-
expect(result).to eq nil
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
data/spec/models/rate_spec.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe Rating::Rate do
|
6
|
-
let!(:object) { build :rating_rate }
|
7
|
-
|
8
|
-
it { expect(object).to be_valid }
|
9
|
-
|
10
|
-
it { is_expected.to belong_to :author }
|
11
|
-
it { is_expected.to belong_to :resource }
|
12
|
-
it { is_expected.to belong_to :scopeable }
|
13
|
-
|
14
|
-
it { is_expected.to validate_presence_of :author }
|
15
|
-
it { is_expected.to validate_presence_of :resource }
|
16
|
-
it { is_expected.to validate_presence_of :value }
|
17
|
-
|
18
|
-
it do
|
19
|
-
expect(subject).to validate_numericality_of(:value).is_less_than_or_equal_to(100).is_less_than_or_equal_to 100
|
20
|
-
end
|
21
|
-
|
22
|
-
it do
|
23
|
-
scopes = %i[author_type resource_id resource_type scopeable_id scopeable_type]
|
24
|
-
scopes += %i[scope_1 scope_2] if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
|
25
|
-
|
26
|
-
expect(object).to validate_uniqueness_of(:author_id).scoped_to(scopes).case_insensitive
|
27
|
-
end
|
28
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
require 'support/shared_context/with_database_records'
|
5
|
-
|
6
|
-
RSpec.describe Rating::Rating, ':averager_data' do
|
7
|
-
include_context 'with_database_records'
|
8
|
-
|
9
|
-
context 'with no scopeable' do
|
10
|
-
subject(:result) { described_class.averager_data article_1, nil }
|
11
|
-
|
12
|
-
it 'returns the values average of given resource type' do
|
13
|
-
expect(result.as_json['rating_avg'].to_f.to_s).to eq '30.5'
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'returns the average of number of records for the given resource type' do
|
17
|
-
expect(result.as_json['count_avg'].to_s).to eq '1.333333333333333333'
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'with scopeable' do
|
22
|
-
subject(:result) { described_class.averager_data article_1, category }
|
23
|
-
|
24
|
-
it 'returns the values average of given resource type' do
|
25
|
-
expect(result.as_json['rating_avg'].to_f.to_s).to eq '1.5'
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'returns the average of number of records for the given resource type' do
|
29
|
-
expect(result.as_json['count_avg'].to_f.to_s).to eq '2.0'
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
require 'support/shared_context/with_database_records'
|
5
|
-
|
6
|
-
RSpec.describe Rating::Rating, ':data' do
|
7
|
-
include_context 'with_database_records'
|
8
|
-
|
9
|
-
context 'with no scopeable' do
|
10
|
-
subject(:result) { described_class.data article_1, nil }
|
11
|
-
|
12
|
-
it 'returns the average of value for a resource' do
|
13
|
-
expect(result[:average]).to eq 50.5
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'returns the sum of values for a resource' do
|
17
|
-
expect(result[:sum]).to eq 101
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'returns the count of votes for a resource' do
|
21
|
-
expect(result[:total]).to eq 2
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'returns the estimate for a resource' do
|
25
|
-
expect(result[:estimate].to_s).to eq '42.5000000000000000012000000505'
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'with scopeable' do
|
30
|
-
subject(:result) { described_class.data article_1, category }
|
31
|
-
|
32
|
-
it 'returns the average of value for a resource' do
|
33
|
-
expect(result[:average]).to eq 1.5
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'returns the sum of values for a resource' do
|
37
|
-
expect(result[:sum]).to eq 3
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'returns the count of votes for a resource' do
|
41
|
-
expect(result[:total]).to eq 2
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'returns the estimate for a resource' do
|
45
|
-
expect(result[:estimate]).to eq 1.5
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|