From 1791ea2d1aa5ceafc126119c25d42d874eec5b63 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Mon, 20 Feb 2023 21:47:46 +0100 Subject: [PATCH] move timeline test data to shared context --- spec/models/answer_spec.rb | 106 +----------------- spec/models/list_spec.rb | 106 +----------------- spec/models/user_spec.rb | 94 +--------------- spec/shared_examples/timeline_test_data.rb | 123 +++++++++++++++++++++ 4 files changed, 130 insertions(+), 299 deletions(-) create mode 100644 spec/shared_examples/timeline_test_data.rb diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb index cd6654c4..bca0c829 100644 --- a/spec/models/answer_spec.rb +++ b/spec/models/answer_spec.rb @@ -92,112 +92,8 @@ describe Answer, type: :model do end end - describe ".public_timeline" do + describe ".public_timeline", timeline_test_data: true do let(:user) { FactoryBot.create(:user) } - let(:user1) { FactoryBot.create(:user) } - let(:user2) { FactoryBot.create(:user) } - - let(:blocked_user) { FactoryBot.create(:user) } - let(:muted_user) { FactoryBot.create(:user) } - let!(:answer_to_anonymous) do - FactoryBot.create( - :answer, - user: user1, - content: "answer to a true anonymous coward", - question: FactoryBot.create( - :question, - author_is_anonymous: true - ) - ) - end - let!(:answer_to_normal_user) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a normal user", - question: FactoryBot.create( - :question, - user: user1, - author_is_anonymous: false - ) - ) - end - let!(:answer_to_normal_user_anonymous) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a cowardly user", - question: FactoryBot.create( - :question, - user: user1, - author_is_anonymous: true - ) - ) - end - let!(:answer_from_blocked_user) do - FactoryBot.create( - :answer, - user: blocked_user, - content: "answer from a blocked user", - question: FactoryBot.create(:question) - ) - end - let!(:answer_to_blocked_user) do - FactoryBot.create( - :answer, - user: user1, - content: "answer to a blocked user", - question: FactoryBot.create( - :question, - user: blocked_user, - author_is_anonymous: false - ) - ) - end - let!(:answer_to_blocked_user_anonymous) do - FactoryBot.create( - :answer, - user: user1, - content: "answer to a blocked user who's a coward", - question: FactoryBot.create( - :question, - user: blocked_user, - author_is_anonymous: true - ) - ) - end - let!(:answer_from_muted_user) do - FactoryBot.create( - :answer, - user: muted_user, - content: "answer from a muted user", - question: FactoryBot.create(:question) - ) - end - let!(:answer_to_muted_user) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a muted user", - question: FactoryBot.create( - :question, - user: muted_user, - author_is_anonymous: false - ) - ) - end - let!(:answer_to_muted_user_anonymous) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a muted user who's a coward", - question: FactoryBot.create( - :question, - user: muted_user, - author_is_anonymous: true - ) - ) - end subject { Answer.public_timeline } diff --git a/spec/models/list_spec.rb b/spec/models/list_spec.rb index 8e5068dd..8cedd6d7 100644 --- a/spec/models/list_spec.rb +++ b/spec/models/list_spec.rb @@ -64,112 +64,8 @@ RSpec.describe(List, type: :model) do end end - describe "#timeline" do + describe "#timeline", timeline_test_data: true do let(:list) { List.create(user:, display_name: "test list") } - let(:user1) { FactoryBot.create(:user) } - let(:user2) { FactoryBot.create(:user) } - - let(:blocked_user) { FactoryBot.create(:user) } - let(:muted_user) { FactoryBot.create(:user) } - let!(:answer_to_anonymous) do - FactoryBot.create( - :answer, - user: user1, - content: "answer to a true anonymous coward", - question: FactoryBot.create( - :question, - author_is_anonymous: true - ) - ) - end - let!(:answer_to_normal_user) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a normal user", - question: FactoryBot.create( - :question, - user: user1, - author_is_anonymous: false - ) - ) - end - let!(:answer_to_normal_user_anonymous) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a cowardly user", - question: FactoryBot.create( - :question, - user: user1, - author_is_anonymous: true - ) - ) - end - let!(:answer_from_blocked_user) do - FactoryBot.create( - :answer, - user: blocked_user, - content: "answer from a blocked user", - question: FactoryBot.create(:question) - ) - end - let!(:answer_to_blocked_user) do - FactoryBot.create( - :answer, - user: user1, - content: "answer to a blocked user", - question: FactoryBot.create( - :question, - user: blocked_user, - author_is_anonymous: false - ) - ) - end - let!(:answer_to_blocked_user_anonymous) do - FactoryBot.create( - :answer, - user: user1, - content: "answer to a blocked user who's a coward", - question: FactoryBot.create( - :question, - user: blocked_user, - author_is_anonymous: true - ) - ) - end - let!(:answer_from_muted_user) do - FactoryBot.create( - :answer, - user: muted_user, - content: "answer from a muted user", - question: FactoryBot.create(:question) - ) - end - let!(:answer_to_muted_user) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a muted user", - question: FactoryBot.create( - :question, - user: muted_user, - author_is_anonymous: false - ) - ) - end - let!(:answer_to_muted_user_anonymous) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a muted user who's a coward", - question: FactoryBot.create( - :question, - user: muted_user, - author_is_anonymous: true - ) - ) - end before do list.add_member user1 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4097c4a2..c3071896 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -212,95 +212,7 @@ RSpec.describe User, type: :model do end end - context "user follows users with answers to questions from blocked or muted users" do - let(:blocked_user) { FactoryBot.create(:user) } - let(:muted_user) { FactoryBot.create(:user) } - let(:user1) { FactoryBot.create(:user) } - let(:user2) { FactoryBot.create(:user) } - let!(:answer_to_anonymous) do - FactoryBot.create( - :answer, - user: user1, - content: "answer to a true anonymous coward", - question: FactoryBot.create( - :question, - author_is_anonymous: true - ) - ) - end - let!(:answer_to_normal_user) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a normal user", - question: FactoryBot.create( - :question, - user: user1, - author_is_anonymous: false - ) - ) - end - let!(:answer_to_normal_user_anonymous) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a cowardly user", - question: FactoryBot.create( - :question, - user: user1, - author_is_anonymous: true - ) - ) - end - let!(:answer_to_blocked_user) do - FactoryBot.create( - :answer, - user: user1, - content: "answer to a blocked user", - question: FactoryBot.create( - :question, - user: blocked_user, - author_is_anonymous: false - ) - ) - end - let!(:answer_to_blocked_user_anonymous) do - FactoryBot.create( - :answer, - user: user1, - content: "answer to a blocked user who's a coward", - question: FactoryBot.create( - :question, - user: blocked_user, - author_is_anonymous: true - ) - ) - end - let!(:answer_to_muted_user) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a muted user", - question: FactoryBot.create( - :question, - user: muted_user, - author_is_anonymous: false - ) - ) - end - let!(:answer_to_muted_user_anonymous) do - FactoryBot.create( - :answer, - user: user2, - content: "answer to a muted user who's a coward", - question: FactoryBot.create( - :question, - user: muted_user, - author_is_anonymous: true - ) - ) - end - + context "user follows users with answers to questions from blocked or muted users", timeline_test_data: true do before do me.follow user1 me.follow user2 @@ -314,6 +226,8 @@ RSpec.describe User, type: :model do expect(subject).to include(answer_to_muted_user_anonymous) expect(subject).to include(answer_to_blocked_user) expect(subject).to include(answer_to_muted_user) + expect(subject).not_to include(answer_from_blocked_user) + expect(subject).not_to include(answer_from_muted_user) end context "when blocking and muting some users" do @@ -330,6 +244,8 @@ RSpec.describe User, type: :model do expect(subject).to include(answer_to_muted_user_anonymous) expect(subject).not_to include(answer_to_blocked_user) expect(subject).not_to include(answer_to_muted_user) + expect(subject).not_to include(answer_from_blocked_user) + expect(subject).not_to include(answer_from_muted_user) end end end diff --git a/spec/shared_examples/timeline_test_data.rb b/spec/shared_examples/timeline_test_data.rb new file mode 100644 index 00000000..06ae644b --- /dev/null +++ b/spec/shared_examples/timeline_test_data.rb @@ -0,0 +1,123 @@ +# frozen_string_literal: true + +RSpec.shared_context "Timeline test data" do + let(:user1) { FactoryBot.create(:user) } + + let(:user2) { FactoryBot.create(:user) } + + let(:blocked_user) { FactoryBot.create(:user) } + + let(:muted_user) { FactoryBot.create(:user) } + + let!(:answer_to_anonymous) do + FactoryBot.create( + :answer, + user: user1, + content: "answer to a true anonymous coward", + question: FactoryBot.create( + :question, + author_is_anonymous: true + ) + ) + end + + let!(:answer_to_normal_user) do + FactoryBot.create( + :answer, + user: user2, + content: "answer to a normal user", + question: FactoryBot.create( + :question, + user: user1, + author_is_anonymous: false + ) + ) + end + + let!(:answer_to_normal_user_anonymous) do + FactoryBot.create( + :answer, + user: user2, + content: "answer to a cowardly user", + question: FactoryBot.create( + :question, + user: user1, + author_is_anonymous: true + ) + ) + end + + let!(:answer_from_blocked_user) do + FactoryBot.create( + :answer, + user: blocked_user, + content: "answer from a blocked user", + question: FactoryBot.create(:question) + ) + end + + let!(:answer_to_blocked_user) do + FactoryBot.create( + :answer, + user: user1, + content: "answer to a blocked user", + question: FactoryBot.create( + :question, + user: blocked_user, + author_is_anonymous: false + ) + ) + end + + let!(:answer_to_blocked_user_anonymous) do + FactoryBot.create( + :answer, + user: user1, + content: "answer to a blocked user who's a coward", + question: FactoryBot.create( + :question, + user: blocked_user, + author_is_anonymous: true + ) + ) + end + + let!(:answer_from_muted_user) do + FactoryBot.create( + :answer, + user: muted_user, + content: "answer from a muted user", + question: FactoryBot.create(:question) + ) + end + + let!(:answer_to_muted_user) do + FactoryBot.create( + :answer, + user: user2, + content: "answer to a muted user", + question: FactoryBot.create( + :question, + user: muted_user, + author_is_anonymous: false + ) + ) + end + + let!(:answer_to_muted_user_anonymous) do + FactoryBot.create( + :answer, + user: user2, + content: "answer to a muted user who's a coward", + question: FactoryBot.create( + :question, + user: muted_user, + author_is_anonymous: true + ) + ) + end +end + +RSpec.configure do |config| + config.include_context "Timeline test data", timeline_test_data: true +end