spec: refactor setup on shared context

main
Washington Botelho 2018-01-16 01:25:36 -02:00
parent acfae75b60
commit 20975c8f29
No known key found for this signature in database
GPG Key ID: BECE10A8106CC7A0
12 changed files with 42 additions and 190 deletions

View File

@ -1,26 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Extension, ':order_by_rating' do RSpec.describe Rating::Extension, ':order_by_rating' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
before do
create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end
context 'with default filters' do context 'with default filters' do
it 'sorts by :estimate :desc' do it 'sorts by :estimate :desc' do

View File

@ -1,24 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Extension, '.rated_records' do RSpec.describe Rating::Extension, '.rated_records' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, 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: 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(author_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]

View File

@ -1,24 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Extension, ':rated' do RSpec.describe Rating::Extension, ':rated' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, 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: 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

View File

@ -1,24 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Extension, '.rates_records' do RSpec.describe Rating::Extension, '.rates_records' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, 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: 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

@ -1,24 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Extension, ':rates' do RSpec.describe Rating::Extension, ':rates' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, 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: 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

View File

@ -1,26 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Extension, '.rating' do RSpec.describe Rating::Extension, '.rating' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
before do
create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end
it 'returns all rating of this resource' do it 'returns all rating of this resource' do
expect(article_1.rating_records).to match_array Rating::Rating.where(resource: article_1) expect(article_1.rating_records).to match_array Rating::Rating.where(resource: article_1)

View File

@ -1,24 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Extension, '.rating' do RSpec.describe Rating::Extension, '.rating' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, 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: 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

View File

@ -1,26 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Rating, ':averager_data' do RSpec.describe Rating::Rating, ':averager_data' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
before do
create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end
context 'with no scopeable' do context 'with no scopeable' do
subject { described_class.averager_data article_1, nil } subject { described_class.averager_data article_1, nil }

View File

@ -1,26 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Rating, ':data' do RSpec.describe Rating::Rating, ':data' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
before do
create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end
context 'with no scopeable' do context 'with no scopeable' do
subject { described_class.data article_1, nil } subject { described_class.data article_1, nil }

View File

@ -1,26 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Rating, ':update_rating' do RSpec.describe Rating::Rating, ':update_rating' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
before do
create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: author_2, resource: article_1, value: 1
create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end
context 'with no scopeable' do context 'with no scopeable' do
it 'updates the rating data of the given resource' do it 'updates the rating data of the given resource' do

View File

@ -1,36 +1,20 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
require 'support/shared_context/with_database_records'
RSpec.describe Rating::Rating, ':values_data' do RSpec.describe Rating::Rating, ':values_data' do
let!(:category) { create :category } include_context 'with_database_records'
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
before do
create :rating_rate, author: author_1, resource: article_1, value: 100
create :rating_rate, author: author_1, resource: article_2, value: 11
create :rating_rate, author: author_1, resource: article_3, value: 10
create :rating_rate, author: author_2, resource: article_1, value: 4
create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1
create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2
end
context 'with no scopeable' do context 'with no scopeable' do
subject { described_class.values_data article_1, nil } subject { described_class.values_data article_1, nil }
it 'returns the average of value for a resource' do it 'returns the average of value for a resource' do
expect(subject.as_json['rating_avg']).to eq 52.0 expect(subject.as_json['rating_avg']).to eq 50.5
end end
it 'returns the sum of values for a resource' do it 'returns the sum of values for a resource' do
expect(subject.as_json['rating_sum']).to eq 104 expect(subject.as_json['rating_sum']).to eq 101
end end
it 'returns the count of votes for a resource' do it 'returns the count of votes for a resource' do

View File

@ -0,0 +1,18 @@
RSpec.shared_context 'with_database_records' do
let!(:category) { create :category }
let!(:author_1) { create :author }
let!(:author_2) { create :author }
let!(:article_1) { create :article }
let!(:article_2) { create :article }
let!(:article_3) { create :article }
let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, 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: author_2, resource: article_1, scopeable: category, value: 2 }
end