2017-10-30 01:44:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Rating::Rate, ':create' do
|
2018-01-16 03:14:05 +00:00
|
|
|
let!(:author) { create :author }
|
2017-10-30 01:44:23 +00:00
|
|
|
let!(:article) { create :article }
|
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'with no scopeable' do
|
2018-01-26 21:07:37 +00:00
|
|
|
before { described_class.create author: author, resource: article, value: 3 }
|
2017-11-02 18:55:46 +00:00
|
|
|
|
|
|
|
context 'when rate does not exist yet' do
|
|
|
|
it 'creates a rate entry' do
|
|
|
|
rate = described_class.last
|
|
|
|
|
2018-01-16 03:14:05 +00:00
|
|
|
expect(rate.author).to eq author
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(rate.resource).to eq article
|
|
|
|
expect(rate.value).to eq 3
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a rating entry' do
|
|
|
|
rating = Rating::Rating.last
|
|
|
|
|
|
|
|
expect(rating.average).to eq 3
|
|
|
|
expect(rating.estimate).to eq 3
|
|
|
|
expect(rating.resource).to eq article
|
|
|
|
expect(rating.sum).to eq 3
|
|
|
|
expect(rating.total).to eq 1
|
|
|
|
end
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'when rate already exists' do
|
2018-01-16 03:14:05 +00:00
|
|
|
let!(:author_2) { create :author }
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2018-01-26 21:07:37 +00:00
|
|
|
before { described_class.create author: author_2, resource: article, value: 4 }
|
2017-11-02 18:55:46 +00:00
|
|
|
|
|
|
|
it 'creates one more rate entry' do
|
2018-01-16 03:14:05 +00:00
|
|
|
rates = described_class.where(author: [author, author_2]).order('created_at asc')
|
2017-11-02 18:55:46 +00:00
|
|
|
|
|
|
|
expect(rates.size).to eq 2
|
|
|
|
|
|
|
|
rate = rates[0]
|
|
|
|
|
2018-01-16 03:14:05 +00:00
|
|
|
expect(rate.author).to eq author
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(rate.resource).to eq article
|
|
|
|
expect(rate.value).to eq 3
|
|
|
|
|
|
|
|
rate = rates[1]
|
|
|
|
|
2018-01-16 03:14:05 +00:00
|
|
|
expect(rate.author).to eq author_2
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(rate.resource).to eq article
|
|
|
|
expect(rate.value).to eq 4
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
it 'updates the unique rating entry' do
|
|
|
|
rating = Rating::Rating.find_by(resource: article)
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(rating.average).to eq 3.5
|
|
|
|
expect(rating.estimate).to eq 3.5
|
|
|
|
expect(rating.resource).to eq article
|
|
|
|
expect(rating.sum).to eq 7
|
|
|
|
expect(rating.total).to eq 2
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'with scopeable' do
|
|
|
|
let!(:category) { create :category }
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2018-01-26 21:07:37 +00:00
|
|
|
before { described_class.create author: author, resource: article, scopeable: category, value: 3 }
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'when rate does not exist yet' do
|
|
|
|
it 'creates a rate entry' do
|
|
|
|
rate = described_class.last
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2018-01-16 03:14:05 +00:00
|
|
|
expect(rate.author).to eq author
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(rate.resource).to eq article
|
|
|
|
expect(rate.scopeable).to eq category
|
|
|
|
expect(rate.value).to eq 3
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
it 'creates a rating entry' do
|
|
|
|
rating = Rating::Rating.last
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(rating.average).to eq 3
|
|
|
|
expect(rating.estimate).to eq 3
|
|
|
|
expect(rating.resource).to eq article
|
|
|
|
expect(rating.scopeable).to eq category
|
|
|
|
expect(rating.sum).to eq 3
|
|
|
|
expect(rating.total).to eq 1
|
|
|
|
end
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'when rate already exists' do
|
2018-01-16 03:14:05 +00:00
|
|
|
let!(:author_2) { create :author }
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2018-01-26 21:07:37 +00:00
|
|
|
before { described_class.create author: author_2, resource: article, scopeable: category, value: 4 }
|
2017-11-02 18:55:46 +00:00
|
|
|
|
|
|
|
it 'creates one more rate entry' do
|
2018-01-16 03:14:05 +00:00
|
|
|
rates = described_class.where(author: [author, author_2]).order('created_at asc')
|
2017-11-02 18:55:46 +00:00
|
|
|
|
|
|
|
expect(rates.size).to eq 2
|
|
|
|
|
|
|
|
rate = rates[0]
|
|
|
|
|
2018-01-16 03:14:05 +00:00
|
|
|
expect(rate.author).to eq author
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(rate.resource).to eq article
|
|
|
|
expect(rate.scopeable).to eq category
|
|
|
|
expect(rate.value).to eq 3
|
|
|
|
|
|
|
|
rate = rates[1]
|
|
|
|
|
2018-01-16 03:14:05 +00:00
|
|
|
expect(rate.author).to eq author_2
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(rate.resource).to eq article
|
|
|
|
expect(rate.scopeable).to eq category
|
|
|
|
expect(rate.value).to eq 4
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
it 'updates the unique rating entry' do
|
|
|
|
rating = Rating::Rating.find_by(resource: article, scopeable: category)
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(rating.average).to eq 3.5
|
|
|
|
expect(rating.estimate).to eq 3.5
|
|
|
|
expect(rating.resource).to eq article
|
|
|
|
expect(rating.scopeable).to eq category
|
|
|
|
expect(rating.sum).to eq 7
|
|
|
|
expect(rating.total).to eq 2
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
end
|
2018-01-26 21:16:56 +00:00
|
|
|
|
|
|
|
context 'with metadata' do
|
|
|
|
before { AddCommentOnRatingRatesTable.new.change }
|
|
|
|
|
|
|
|
context 'with nil value' do
|
|
|
|
it 'creates a rate entry ignoring metadata' do
|
|
|
|
described_class.create author: author, metadata: nil, resource: article, value: 3
|
|
|
|
|
|
|
|
rate = described_class.last
|
|
|
|
|
|
|
|
expect(rate.author).to eq author
|
|
|
|
expect(rate.comment).to eq nil
|
|
|
|
expect(rate.resource).to eq article
|
|
|
|
expect(rate.value).to eq 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with empty value' do
|
|
|
|
it 'creates a rate entry ignoring metadata' do
|
|
|
|
described_class.create author: author, metadata: '', resource: article, value: 3
|
|
|
|
|
|
|
|
rate = described_class.last
|
|
|
|
|
|
|
|
expect(rate.author).to eq author
|
|
|
|
expect(rate.comment).to eq nil
|
|
|
|
expect(rate.resource).to eq article
|
|
|
|
expect(rate.value).to eq 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with hash value' do
|
|
|
|
it 'creates a rate entry with metadata included' do
|
|
|
|
described_class.create author: author, metadata: { comment: 'comment' }, resource: article, value: 3
|
|
|
|
|
|
|
|
rate = described_class.last
|
|
|
|
|
|
|
|
expect(rate.author).to eq author
|
|
|
|
expect(rate.comment).to eq 'comment'
|
|
|
|
expect(rate.resource).to eq article
|
|
|
|
expect(rate.value).to eq 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|