spec: optimization using one less class

main
Washington Botelho 2018-01-16 01:14:05 -02:00
parent 71f31e09ea
commit 63f2cb9320
No known key found for this signature in database
GPG Key ID: BECE10A8106CC7A0
22 changed files with 147 additions and 169 deletions

View File

@ -4,7 +4,7 @@ FactoryBot.define do
factory :rating_rate, class: Rating::Rate do factory :rating_rate, class: Rating::Rate do
value 100 value 100
author { create :user } author { create :author }
resource { create :article } resource { create :article }
end end
end end

View File

@ -1,7 +0,0 @@
# frozen_string_literal: true
FactoryBot.define do
factory :user do
sequence(:name) { |i| "User #{i}" }
end
end

View File

@ -3,22 +3,22 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Rating::Extension, ':after_create' do RSpec.describe Rating::Extension, ':after_create' do
context 'with :as as nil' do context 'when :as is nil' do
let!(:user) { create :user } let!(:article) { create :article }
it 'creates a rating record with zero values just to be easy to make the count' do it 'creates a rating record with zero values just to be easy to make the count' do
rating = Rating::Rating.find_by(resource: user) rating = Rating::Rating.find_by(resource: article)
expect(rating.average).to eq 0 expect(rating.average).to eq 0
expect(rating.estimate).to eq 0 expect(rating.estimate).to eq 0
expect(rating.resource).to eq user expect(rating.resource).to eq article
expect(rating.scopeable).to eq nil expect(rating.scopeable).to eq nil
expect(rating.sum).to eq 0 expect(rating.sum).to eq 0
expect(rating.total).to eq 0 expect(rating.total).to eq 0
end end
end end
context 'with :as as :author' do context 'when :as is :author' do
let!(:author) { create :author } let!(:author) { create :author }
it 'does not creates a rating record' do it 'does not creates a rating record' do

View File

@ -5,21 +5,21 @@ require 'rails_helper'
RSpec.describe Rating::Extension, ':order_by_rating' do RSpec.describe Rating::Extension, ':order_by_rating' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
before do before do
create :rating_rate, author: user_1, resource: article_1, value: 100 create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: user_1, resource: article_2, value: 11 create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: user_1, resource: article_3, value: 10 create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: user_2, resource: article_1, value: 1 create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end end
context 'with default filters' do context 'with default filters' do
@ -184,12 +184,12 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
context 'with other resource' do context 'with other resource' do
it 'works' do it 'works' do
expect(User.order_by_rating(:total, :desc)).to match_array [user_1, user_2] expect(Author.order_by_rating(:total, :desc)).to match_array [author_1, author_2]
end end
context 'with scope' do context 'with scope' do
it 'returns empty since creation has no scope' do it 'returns empty since creation has no scope' do
expect(User.order_by_rating(:total, :desc, scope: category)).to eq [] expect(Author.order_by_rating(:total, :desc, scope: category)).to eq []
end end
end end
end end

View File

@ -3,14 +3,14 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Rating::Extension, ':rate_for' do RSpec.describe Rating::Extension, ':rate_for' do
let!(:user) { create :user } let!(:author) { create :author }
let!(:article) { create :article } let!(:article) { create :article }
context 'with no scopeable' do context 'with no scopeable' do
it 'delegates to rate object' do it 'delegates to rate object' do
expect(Rating::Rate).to receive(:rate_for).with author: user, resource: article, scopeable: nil expect(Rating::Rate).to receive(:rate_for).with author: author, resource: article, scopeable: nil
user.rate_for article author.rate_for article
end end
end end
@ -18,9 +18,9 @@ RSpec.describe Rating::Extension, ':rate_for' do
let!(:category) { build :category } let!(:category) { build :category }
it 'delegates to rate object' do it 'delegates to rate object' do
expect(Rating::Rate).to receive(:rate_for).with author: user, resource: article, scopeable: category expect(Rating::Rate).to receive(:rate_for).with author: author, resource: article, scopeable: category
user.rate_for article, scope: category author.rate_for article, scope: category
end end
end end
end end

View File

@ -3,14 +3,14 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Rating::Extension, ':rate' do RSpec.describe Rating::Extension, ':rate' do
let!(:user) { create :user } let!(:author) { create :author }
let!(:article) { create :article } let!(:article) { create :article }
context 'with no scopeable' do context 'with no scopeable' do
it 'delegates to rate object' do it 'delegates to rate object' do
expect(Rating::Rate).to receive(:create).with author: user, resource: article, scopeable: nil, value: 3 expect(Rating::Rate).to receive(:create).with author: author, resource: article, scopeable: nil, value: 3
user.rate article, 3 author.rate article, 3
end end
end end
@ -18,9 +18,9 @@ RSpec.describe Rating::Extension, ':rate' do
let!(:category) { build :category } let!(:category) { build :category }
it 'delegates to rate object' do it 'delegates to rate object' do
expect(Rating::Rate).to receive(:create).with author: user, resource: article, scopeable: category, value: 3 expect(Rating::Rate).to receive(:create).with author: author, resource: article, scopeable: category, value: 3
user.rate article, 3, scope: category author.rate article, 3, scope: category
end end
end end
end end

View File

@ -3,20 +3,20 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Rating::Extension, ':rated?' do RSpec.describe Rating::Extension, ':rated?' do
let!(:user) { create :user } let!(:author) { create :author }
let!(:article) { create :article } let!(:article) { create :article }
context 'with no scopeable' do context 'with no scopeable' do
context 'when has no rate for the given resource' do context 'when has no rate for the given resource' do
before { allow(user).to receive(:rate_for).with(article, scope: nil) { nil } } before { allow(author).to receive(:rate_for).with(article, scope: nil) { nil } }
specify { expect(user.rated?(article)).to eq false } specify { expect(author.rated?(article)).to eq false }
end end
context 'when has rate for the given resource' do context 'when has rate for the given resource' do
before { allow(user).to receive(:rate_for).with(article, scope: nil) { double } } before { allow(author).to receive(:rate_for).with(article, scope: nil) { double } }
specify { expect(user.rated?(article)).to eq true } specify { expect(author.rated?(article)).to eq true }
end end
end end
@ -24,15 +24,15 @@ RSpec.describe Rating::Extension, ':rated?' do
let!(:category) { build :category } let!(:category) { build :category }
context 'when has no rate for the given resource' do context 'when has no rate for the given resource' do
before { allow(user).to receive(:rate_for).with(article, scope: category) { nil } } before { allow(author).to receive(:rate_for).with(article, scope: category) { nil } }
specify { expect(user.rated?(article, scope: category)).to eq false } specify { expect(author.rated?(article, scope: category)).to eq false }
end end
context 'when has rate for the given resource' do context 'when has rate for the given resource' do
before { allow(user).to receive(:rate_for).with(article, scope: category) { double } } before { allow(author).to receive(:rate_for).with(article, scope: category) { double } }
specify { expect(user.rated?(article, scope: category)).to eq true } specify { expect(author.rated?(article, scope: category)).to eq true }
end end
end end
end end

View File

@ -5,22 +5,22 @@ require 'rails_helper'
RSpec.describe Rating::Extension, '.rated_records' do RSpec.describe Rating::Extension, '.rated_records' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 } let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 } let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 } let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 } let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, value: 1 }
let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 } let!(:rate_5) { create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1 }
let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 } let!(:rate_6) { create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2 }
it 'returns all rates that this author gave' do it 'returns all rates that this author gave' do
expect(user_1.rated_records).to match_array [rate_1, rate_2, rate_3, rate_5] expect(author_1.rated_records).to match_array [rate_1, rate_2, rate_3, rate_5]
end end
end end

View File

@ -5,42 +5,42 @@ require 'rails_helper'
RSpec.describe Rating::Extension, ':rated' do RSpec.describe Rating::Extension, ':rated' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 } let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 } let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 } let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 } let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, value: 1 }
let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 } let!(:rate_5) { create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1 }
let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 } let!(:rate_6) { create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2 }
context 'with no scope' do context 'with no scope' do
it 'returns rates made by this author' do it 'returns rates made by this author' do
expect(user_1.rated).to match_array [rate_1, rate_2, rate_3] expect(author_1.rated).to match_array [rate_1, rate_2, rate_3]
end end
end end
context 'with no scope' do context 'with no scope' do
it 'returns scoped rates made by this author' do it 'returns scoped rates made by this author' do
expect(user_1.rated(scope: category)).to eq [rate_5] expect(author_1.rated(scope: category)).to eq [rate_5]
end end
end end
context 'when destroy author' do context 'when destroy author' do
before do before do
expect(Rating::Rate.where(author: user_1).count).to eq 4 expect(Rating::Rate.where(author: author_1).count).to eq 4
user_1.destroy! author_1.destroy!
end end
it 'destroys rates of that author' do it 'destroys rates of that author' do
expect(Rating::Rate.where(author: user_1).count).to eq 0 expect(Rating::Rate.where(author: author_1).count).to eq 0
end end
end end

View File

@ -5,20 +5,20 @@ require 'rails_helper'
RSpec.describe Rating::Extension, '.rates_records' do RSpec.describe Rating::Extension, '.rates_records' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 } let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 } let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 } let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 } let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, value: 1 }
let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 } let!(:rate_5) { create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1 }
let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 } let!(:rate_6) { create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2 }
it 'returns all rates that this resource received' do it 'returns all rates that this resource received' do
expect(article_1.rates_records).to match_array [rate_1, rate_4, rate_5, rate_6] expect(article_1.rates_records).to match_array [rate_1, rate_4, rate_5, rate_6]

View File

@ -5,20 +5,20 @@ require 'rails_helper'
RSpec.describe Rating::Extension, ':rates' do RSpec.describe Rating::Extension, ':rates' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 } let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 } let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 } let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 } let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, value: 1 }
let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 } let!(:rate_5) { create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1 }
let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 } let!(:rate_6) { create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2 }
context 'with no scope' do context 'with no scope' do
it 'returns rates that this resource received' do it 'returns rates that this resource received' do
@ -34,11 +34,11 @@ RSpec.describe Rating::Extension, ':rates' do
context 'when destroy author' do context 'when destroy author' do
it 'destroys rates of that author' do it 'destroys rates of that author' do
expect(Rating::Rate.where(author: user_1).count).to eq 4 expect(Rating::Rate.where(author: author_1).count).to eq 4
user_1.destroy! author_1.destroy!
expect(Rating::Rate.where(author: user_1).count).to eq 0 expect(Rating::Rate.where(author: author_1).count).to eq 0
end end
end end

View File

@ -5,21 +5,21 @@ require 'rails_helper'
RSpec.describe Rating::Extension, '.rating' do RSpec.describe Rating::Extension, '.rating' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
before do before do
create :rating_rate, author: user_1, resource: article_1, value: 100 create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: user_1, resource: article_2, value: 11 create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: user_1, resource: article_3, value: 10 create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: user_2, resource: article_1, value: 1 create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end end
it 'returns all rating of this resource' do it 'returns all rating of this resource' do

View File

@ -5,20 +5,20 @@ require 'rails_helper'
RSpec.describe Rating::Extension, '.rating' do RSpec.describe Rating::Extension, '.rating' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 } let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 } let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 } let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 } let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, value: 1 }
let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 } let!(:rate_5) { create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1 }
let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 } let!(:rate_6) { create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2 }
context 'with no scope' do context 'with no scope' do
it 'returns rating record' do it 'returns rating record' do
@ -36,7 +36,7 @@ RSpec.describe Rating::Extension, '.rating' do
it 'does not destroy resource rating' do it 'does not destroy resource rating' do
expect(Rating::Rating.where(resource: article_1).count).to eq 2 expect(Rating::Rating.where(resource: article_1).count).to eq 2
user_1.destroy! author_1.destroy!
expect(Rating::Rating.where(resource: article_1).count).to eq 2 expect(Rating::Rating.where(resource: article_1).count).to eq 2
end end

View File

@ -3,17 +3,17 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Rating::Rate, ':create' do RSpec.describe Rating::Rate, ':create' do
let!(:user) { create :user } let!(:author) { create :author }
let!(:article) { create :article } let!(:article) { create :article }
context 'with no scopeable' do context 'with no scopeable' do
before { create :rating_rate, author: user, resource: article, value: 3 } before { create :rating_rate, author: author, resource: article, value: 3 }
context 'when rate does not exist yet' do context 'when rate does not exist yet' do
it 'creates a rate entry' do it 'creates a rate entry' do
rate = described_class.last rate = described_class.last
expect(rate.author).to eq user expect(rate.author).to eq author
expect(rate.resource).to eq article expect(rate.resource).to eq article
expect(rate.value).to eq 3 expect(rate.value).to eq 3
end end
@ -30,24 +30,24 @@ RSpec.describe Rating::Rate, ':create' do
end end
context 'when rate already exists' do context 'when rate already exists' do
let!(:user_2) { create :user } let!(:author_2) { create :author }
before { create :rating_rate, author: user_2, resource: article, value: 4 } before { create :rating_rate, author: author_2, resource: article, value: 4 }
it 'creates one more rate entry' do it 'creates one more rate entry' do
rates = described_class.where(author: [user, user_2]).order('created_at asc') rates = described_class.where(author: [author, author_2]).order('created_at asc')
expect(rates.size).to eq 2 expect(rates.size).to eq 2
rate = rates[0] rate = rates[0]
expect(rate.author).to eq user expect(rate.author).to eq author
expect(rate.resource).to eq article expect(rate.resource).to eq article
expect(rate.value).to eq 3 expect(rate.value).to eq 3
rate = rates[1] rate = rates[1]
expect(rate.author).to eq user_2 expect(rate.author).to eq author_2
expect(rate.resource).to eq article expect(rate.resource).to eq article
expect(rate.value).to eq 4 expect(rate.value).to eq 4
end end
@ -67,13 +67,13 @@ RSpec.describe Rating::Rate, ':create' do
context 'with scopeable' do context 'with scopeable' do
let!(:category) { create :category } let!(:category) { create :category }
before { create :rating_rate, author: user, resource: article, scopeable: category, value: 3 } before { create :rating_rate, author: author, resource: article, scopeable: category, value: 3 }
context 'when rate does not exist yet' do context 'when rate does not exist yet' do
it 'creates a rate entry' do it 'creates a rate entry' do
rate = described_class.last rate = described_class.last
expect(rate.author).to eq user expect(rate.author).to eq author
expect(rate.resource).to eq article expect(rate.resource).to eq article
expect(rate.scopeable).to eq category expect(rate.scopeable).to eq category
expect(rate.value).to eq 3 expect(rate.value).to eq 3
@ -92,25 +92,25 @@ RSpec.describe Rating::Rate, ':create' do
end end
context 'when rate already exists' do context 'when rate already exists' do
let!(:user_2) { create :user } let!(:author_2) { create :author }
before { create :rating_rate, author: user_2, resource: article, scopeable: category, value: 4 } before { create :rating_rate, author: author_2, resource: article, scopeable: category, value: 4 }
it 'creates one more rate entry' do it 'creates one more rate entry' do
rates = described_class.where(author: [user, user_2]).order('created_at asc') rates = described_class.where(author: [author, author_2]).order('created_at asc')
expect(rates.size).to eq 2 expect(rates.size).to eq 2
rate = rates[0] rate = rates[0]
expect(rate.author).to eq user expect(rate.author).to eq author
expect(rate.resource).to eq article expect(rate.resource).to eq article
expect(rate.scopeable).to eq category expect(rate.scopeable).to eq category
expect(rate.value).to eq 3 expect(rate.value).to eq 3
rate = rates[1] rate = rates[1]
expect(rate.author).to eq user_2 expect(rate.author).to eq author_2
expect(rate.resource).to eq article expect(rate.resource).to eq article
expect(rate.scopeable).to eq category expect(rate.scopeable).to eq category
expect(rate.value).to eq 4 expect(rate.value).to eq 4

View File

@ -3,19 +3,19 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Rating::Rate, ':rate_for' do RSpec.describe Rating::Rate, ':rate_for' do
let!(:user) { create :user } let!(:author) { create :author }
let!(:article) { create :article } let!(:article) { create :article }
context 'with no scopeable' do context 'with no scopeable' do
context 'when rate does not exist' do context 'when rate does not exist' do
specify { expect(described_class.rate_for(author: user, resource: article)).to eq nil } specify { expect(described_class.rate_for(author: author, resource: article)).to eq nil }
end end
context 'when rate does not exist' do context 'when rate does not exist' do
before { described_class.create author: user, resource: article, value: 3 } before { described_class.create author: author, resource: article, value: 3 }
it 'returns the record' do it 'returns the record' do
expect(described_class.rate_for(author: user, resource: article)).to eq described_class.last expect(described_class.rate_for(author: author, resource: article)).to eq described_class.last
end end
end end
end end
@ -24,14 +24,14 @@ RSpec.describe Rating::Rate, ':rate_for' do
let!(:category) { create :category } let!(:category) { create :category }
context 'when rate does not exist' do context 'when rate does not exist' do
specify { expect(described_class.rate_for(author: user, resource: article, scopeable: category)).to eq nil } specify { expect(described_class.rate_for(author: author, resource: article, scopeable: category)).to eq nil }
end end
context 'when rate does not exist' do context 'when rate does not exist' do
before { described_class.create author: user, resource: article, scopeable: category, value: 3 } before { described_class.create author: author, resource: article, scopeable: category, value: 3 }
it 'returns the record' do it 'returns the record' do
query = described_class.rate_for(author: user, resource: article, scopeable: category) query = described_class.rate_for(author: author, resource: article, scopeable: category)
expect(query).to eq described_class.last expect(query).to eq described_class.last
end end

View File

@ -5,21 +5,21 @@ require 'rails_helper'
RSpec.describe Rating::Rating, ':averager_data' do RSpec.describe Rating::Rating, ':averager_data' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
before do before do
create :rating_rate, author: user_1, resource: article_1, value: 100 create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: user_1, resource: article_2, value: 11 create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: user_1, resource: article_3, value: 10 create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: user_2, resource: article_1, value: 1 create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end end
context 'with no scopeable' do context 'with no scopeable' do

View File

@ -5,21 +5,21 @@ require 'rails_helper'
RSpec.describe Rating::Rating, ':data' do RSpec.describe Rating::Rating, ':data' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
before do before do
create :rating_rate, author: user_1, resource: article_1, value: 100 create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: user_1, resource: article_2, value: 11 create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: user_1, resource: article_3, value: 10 create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: user_2, resource: article_1, value: 1 create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end end
context 'with no scopeable' do context 'with no scopeable' do

View File

@ -5,21 +5,21 @@ require 'rails_helper'
RSpec.describe Rating::Rating, ':update_rating' do RSpec.describe Rating::Rating, ':update_rating' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
before do before do
create :rating_rate, author: user_1, resource: article_1, value: 100 create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: user_1, resource: article_2, value: 11 create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: user_1, resource: article_3, value: 10 create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: user_2, resource: article_1, value: 1 create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end end
context 'with no scopeable' do context 'with no scopeable' do

View File

@ -5,21 +5,21 @@ require 'rails_helper'
RSpec.describe Rating::Rating, ':values_data' do RSpec.describe Rating::Rating, ':values_data' do
let!(:category) { create :category } let!(:category) { create :category }
let!(:user_1) { create :user } let!(:author_1) { create :author }
let!(:user_2) { create :user } let!(:author_2) { create :author }
let!(:article_1) { create :article } let!(:article_1) { create :article }
let!(:article_2) { create :article } let!(:article_2) { create :article }
let!(:article_3) { create :article } let!(:article_3) { create :article }
before do before do
create :rating_rate, author: user_1, resource: article_1, value: 100 create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: user_1, resource: article_2, value: 11 create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: user_1, resource: article_3, value: 10 create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: user_2, resource: article_1, value: 4 create :rating_rate, author: author_2, resource: article_1, value: 4
create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end end
context 'with no scopeable' do context 'with no scopeable' do

View File

@ -1,9 +0,0 @@
# frozen_string_literal: true
class CreateUsersTable < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :name, null: false
end
end
end

View File

@ -8,4 +8,3 @@ CreateArticlesTable.new.change
CreateAuthorsTable.new.change CreateAuthorsTable.new.change
CreateCategoriesTable.new.change CreateCategoriesTable.new.change
CreateRatingTables.new.change CreateRatingTables.new.change
CreateUsersTable.new.change

View File

@ -1,5 +0,0 @@
# frozen_string_literal: true
class User < ::ActiveRecord::Base
rating
end