2018-03-05 23:26:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe Rating::Extension, 'unscoped_rating' do
|
|
|
|
let!(:author_1) { create :author }
|
|
|
|
let!(:author_2) { create :author }
|
2020-01-16 17:15:38 +00:00
|
|
|
let!(:author_3) { create :author }
|
2018-03-05 23:26:50 +00:00
|
|
|
let!(:scope) { create :category }
|
|
|
|
|
|
|
|
context 'when is false' do
|
|
|
|
let!(:resource) { create :article }
|
|
|
|
|
|
|
|
it 'groups in different line record' do
|
|
|
|
author_1.rate resource, 1, scope: scope
|
|
|
|
author_2.rate resource, 2, scope: scope
|
2020-01-16 17:15:38 +00:00
|
|
|
author_2.rate resource, 5
|
2018-03-05 23:26:50 +00:00
|
|
|
|
|
|
|
ratings = Rating::Rating.all.order('id')
|
|
|
|
|
|
|
|
expect(ratings.size).to eq 2
|
|
|
|
|
|
|
|
rating = ratings[0]
|
|
|
|
|
|
|
|
expect(rating.average.to_s).to eq '1.5'
|
|
|
|
expect(rating.estimate.to_s).to eq '1.5'
|
|
|
|
expect(rating.resource).to eq resource
|
|
|
|
expect(rating.scopeable).to eq scope
|
|
|
|
expect(rating.sum).to eq 3
|
|
|
|
expect(rating.total).to eq 2
|
|
|
|
|
|
|
|
rating = ratings[1]
|
|
|
|
|
|
|
|
expect(rating.average.to_s).to eq '5.0'
|
|
|
|
expect(rating.estimate.to_s).to eq '5.0'
|
|
|
|
expect(rating.resource).to eq resource
|
2022-05-18 02:29:08 +00:00
|
|
|
expect(rating.scopeable).to be(nil)
|
2018-03-05 23:26:50 +00:00
|
|
|
expect(rating.sum).to eq 5
|
|
|
|
expect(rating.total).to eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when is true' do
|
|
|
|
let!(:resource) { create :global }
|
|
|
|
|
|
|
|
it 'groups in the same line record' do
|
|
|
|
author_1.rate resource, 1, scope: scope
|
|
|
|
author_2.rate resource, 2, scope: scope
|
2020-01-16 17:15:38 +00:00
|
|
|
author_2.rate resource, 5
|
2018-03-05 23:26:50 +00:00
|
|
|
|
|
|
|
ratings = Rating::Rating.all.order('id')
|
|
|
|
|
|
|
|
expect(ratings.size).to eq 1
|
|
|
|
|
|
|
|
rating = ratings[0]
|
|
|
|
|
|
|
|
expect(rating.average.to_s).to eq '2.6666666666666667'
|
|
|
|
expect(rating.estimate.to_s).to eq '2.6666666666666667'
|
|
|
|
expect(rating.resource).to eq resource
|
2022-05-18 02:29:08 +00:00
|
|
|
expect(rating.scopeable).to be(nil)
|
2018-03-05 23:26:50 +00:00
|
|
|
expect(rating.sum).to eq 8
|
|
|
|
expect(rating.total).to eq 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-16 17:15:38 +00:00
|
|
|
context 'when is true and have a non scoped record first on database' do
|
2018-03-05 23:26:50 +00:00
|
|
|
let!(:resource) { create :global }
|
|
|
|
|
|
|
|
before { ::Rating::Rating.create resource: resource, scopeable: scope }
|
|
|
|
|
2020-01-16 17:15:38 +00:00
|
|
|
it 'sets the result on record with no scope' do
|
2018-03-05 23:26:50 +00:00
|
|
|
author_1.rate resource, 1, scope: scope
|
|
|
|
author_2.rate resource, 2, scope: scope
|
2020-01-16 17:15:38 +00:00
|
|
|
author_3.rate resource, 5
|
2018-03-05 23:26:50 +00:00
|
|
|
|
|
|
|
ratings = Rating::Rating.all.order('id')
|
|
|
|
|
|
|
|
expect(ratings.size).to eq 2
|
|
|
|
|
|
|
|
rating = ratings[0]
|
|
|
|
|
|
|
|
expect(rating.average.to_s).to eq '0.0'
|
|
|
|
expect(rating.estimate.to_s).to eq '0.0'
|
|
|
|
expect(rating.resource).to eq resource
|
|
|
|
expect(rating.scopeable).to eq scope
|
|
|
|
expect(rating.sum).to eq 0
|
|
|
|
expect(rating.total).to eq 0
|
|
|
|
|
|
|
|
rating = ratings[1]
|
|
|
|
|
|
|
|
expect(rating.average.to_s).to eq '2.6666666666666667'
|
|
|
|
expect(rating.estimate.to_s).to eq '2.6666666666666667'
|
|
|
|
expect(rating.resource).to eq resource
|
2022-05-18 02:29:08 +00:00
|
|
|
expect(rating.scopeable).to be(nil)
|
2018-03-05 23:26:50 +00:00
|
|
|
expect(rating.sum).to eq 8
|
|
|
|
expect(rating.total).to eq 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|