From 20975c8f298c7f8b3a88b4e6f7e3d1f8e993cf52 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Tue, 16 Jan 2018 01:25:36 -0200 Subject: [PATCH] spec: refactor setup on shared context --- spec/models/extension/order_by_rating_spec.rb | 20 ++-------------- spec/models/extension/rated_records_spec.rb | 18 ++------------ spec/models/extension/rated_spec.rb | 18 ++------------ spec/models/extension/rates_records_spec.rb | 18 ++------------ spec/models/extension/rates_spec.rb | 18 ++------------ spec/models/extension/rating_records_spec.rb | 20 ++-------------- spec/models/extension/rating_spec.rb | 18 ++------------ spec/models/rating/averager_data_spec.rb | 20 ++-------------- spec/models/rating/data_spec.rb | 20 ++-------------- spec/models/rating/update_rating_spec.rb | 20 ++-------------- spec/models/rating/values_data_spec.rb | 24 ++++--------------- .../shared_context/with_database_records.rb | 18 ++++++++++++++ 12 files changed, 42 insertions(+), 190 deletions(-) create mode 100644 spec/support/shared_context/with_database_records.rb diff --git a/spec/models/extension/order_by_rating_spec.rb b/spec/models/extension/order_by_rating_spec.rb index caa285b..5c1ccae 100644 --- a/spec/models/extension/order_by_rating_spec.rb +++ b/spec/models/extension/order_by_rating_spec.rb @@ -1,26 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, ':order_by_rating' 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 } - - 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 + include_context 'with_database_records' context 'with default filters' do it 'sorts by :estimate :desc' do diff --git a/spec/models/extension/rated_records_spec.rb b/spec/models/extension/rated_records_spec.rb index 90a0db5..8883431 100644 --- a/spec/models/extension/rated_records_spec.rb +++ b/spec/models/extension/rated_records_spec.rb @@ -1,24 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, '.rated_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 } + include_context 'with_database_records' 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] diff --git a/spec/models/extension/rated_spec.rb b/spec/models/extension/rated_spec.rb index 179e2e4..1110d8d 100644 --- a/spec/models/extension/rated_spec.rb +++ b/spec/models/extension/rated_spec.rb @@ -1,24 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, ':rated' 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 } + include_context 'with_database_records' context 'with no scope' do it 'returns rates made by this author' do diff --git a/spec/models/extension/rates_records_spec.rb b/spec/models/extension/rates_records_spec.rb index 7a87e7d..90c6e6b 100644 --- a/spec/models/extension/rates_records_spec.rb +++ b/spec/models/extension/rates_records_spec.rb @@ -1,24 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, '.rates_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 } + include_context 'with_database_records' 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] diff --git a/spec/models/extension/rates_spec.rb b/spec/models/extension/rates_spec.rb index 0580c89..40213f4 100644 --- a/spec/models/extension/rates_spec.rb +++ b/spec/models/extension/rates_spec.rb @@ -1,24 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, ':rates' 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 } + include_context 'with_database_records' context 'with no scope' do it 'returns rates that this resource received' do diff --git a/spec/models/extension/rating_records_spec.rb b/spec/models/extension/rating_records_spec.rb index fefe92b..78f9fbc 100644 --- a/spec/models/extension/rating_records_spec.rb +++ b/spec/models/extension/rating_records_spec.rb @@ -1,26 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, '.rating' 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 } - - 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 + include_context 'with_database_records' it 'returns all rating of this resource' do expect(article_1.rating_records).to match_array Rating::Rating.where(resource: article_1) diff --git a/spec/models/extension/rating_spec.rb b/spec/models/extension/rating_spec.rb index 505d13b..a0a0cf2 100644 --- a/spec/models/extension/rating_spec.rb +++ b/spec/models/extension/rating_spec.rb @@ -1,24 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, '.rating' 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 } + include_context 'with_database_records' context 'with no scope' do it 'returns rating record' do diff --git a/spec/models/rating/averager_data_spec.rb b/spec/models/rating/averager_data_spec.rb index f6f50bb..979a92c 100644 --- a/spec/models/rating/averager_data_spec.rb +++ b/spec/models/rating/averager_data_spec.rb @@ -1,26 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Rating, ':averager_data' 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 } - - 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 + include_context 'with_database_records' context 'with no scopeable' do subject { described_class.averager_data article_1, nil } diff --git a/spec/models/rating/data_spec.rb b/spec/models/rating/data_spec.rb index bc12225..095befd 100644 --- a/spec/models/rating/data_spec.rb +++ b/spec/models/rating/data_spec.rb @@ -1,26 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Rating, ':data' 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 } - - 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 + include_context 'with_database_records' context 'with no scopeable' do subject { described_class.data article_1, nil } diff --git a/spec/models/rating/update_rating_spec.rb b/spec/models/rating/update_rating_spec.rb index 5db016e..fca017a 100644 --- a/spec/models/rating/update_rating_spec.rb +++ b/spec/models/rating/update_rating_spec.rb @@ -1,26 +1,10 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Rating, ':update_rating' 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 } - - 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 + include_context 'with_database_records' context 'with no scopeable' do it 'updates the rating data of the given resource' do diff --git a/spec/models/rating/values_data_spec.rb b/spec/models/rating/values_data_spec.rb index d79119e..95ffe4a 100644 --- a/spec/models/rating/values_data_spec.rb +++ b/spec/models/rating/values_data_spec.rb @@ -1,36 +1,20 @@ # frozen_string_literal: true require 'rails_helper' +require 'support/shared_context/with_database_records' RSpec.describe Rating::Rating, ':values_data' 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 } - - 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 + include_context 'with_database_records' context 'with no scopeable' do subject { described_class.values_data article_1, nil } 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 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 it 'returns the count of votes for a resource' do diff --git a/spec/support/shared_context/with_database_records.rb b/spec/support/shared_context/with_database_records.rb new file mode 100644 index 0000000..7e9df85 --- /dev/null +++ b/spec/support/shared_context/with_database_records.rb @@ -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