From b4af1bf4e7aab07a7cba08a88adc6057fed8e010 Mon Sep 17 00:00:00 2001 From: elasticspoon Date: Thu, 25 Apr 2024 14:18:59 -0400 Subject: [PATCH] refactor(tests): change db seeding strategy All specs tagged with seed_db will get seeded. This happens on a group level, so it will happen before the whole group. Ex: if the outer most Rspec.describe is tagged with seed_db: true the seeding will run once before the entire spec file and once after. Tracks the previous execution group. - seed or not seed base on flag on current group - truncate if the previous group seeded --- spec/mailers/account_request_mailer_spec.rb | 2 +- spec/mailers/custom_devise_mailer_spec.rb | 2 +- spec/mailers/distribution_mailer_spec.rb | 2 +- spec/mailers/organization_mailer_spec.rb | 2 +- spec/mailers/partner_mailer_spec.rb | 2 +- spec/mailers/reminder_deadline_mailer_spec.rb | 2 +- spec/mailers/request_mailer_spec.rb | 2 +- .../requests_confirmation_mailer_spec.rb | 2 +- spec/mailers/user_mailer_spec.rb | 2 +- spec/rails_helper.rb | 18 +++++++++++++----- spec/requests/product_drives_requests_spec.rb | 2 +- .../organization_update_service_spec.rb | 2 +- .../partner_profile_update_service_spec.rb | 1 - spec/services/user_invite_service_spec.rb | 2 +- 14 files changed, 25 insertions(+), 18 deletions(-) diff --git a/spec/mailers/account_request_mailer_spec.rb b/spec/mailers/account_request_mailer_spec.rb index 40d146b22f..967550995c 100644 --- a/spec/mailers/account_request_mailer_spec.rb +++ b/spec/mailers/account_request_mailer_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe AccountRequestMailer, type: :mailer, seed_items: false do +RSpec.describe AccountRequestMailer, type: :mailer, skip_seed: true do describe '#confirmation' do let(:mail) { AccountRequestMailer.confirmation(account_request_id: account_request_id) } let(:account_request_id) { account_request.id } diff --git a/spec/mailers/custom_devise_mailer_spec.rb b/spec/mailers/custom_devise_mailer_spec.rb index 3eb7942d2e..fe837979f1 100644 --- a/spec/mailers/custom_devise_mailer_spec.rb +++ b/spec/mailers/custom_devise_mailer_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe CustomDeviseMailer, type: :mailer, seed_items: false do +RSpec.describe CustomDeviseMailer, type: :mailer, skip_seed: true do describe "#invitation_instructions" do let(:user) { create(:partner_user) } let(:mail) { described_class.invitation_instructions(user, SecureRandom.uuid) } diff --git a/spec/mailers/distribution_mailer_spec.rb b/spec/mailers/distribution_mailer_spec.rb index cb1c394f7d..53cc28941b 100644 --- a/spec/mailers/distribution_mailer_spec.rb +++ b/spec/mailers/distribution_mailer_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe DistributionMailer, type: :mailer, seed_items: false do +RSpec.describe DistributionMailer, type: :mailer, skip_seed: true do let(:organization) { create(:organization, skip_items: true, name: "TEST ORG") } let(:user) { create(:user, organization: organization) } let(:partner) { create(:partner, name: 'PARTNER', organization_id: organization.id) } diff --git a/spec/mailers/organization_mailer_spec.rb b/spec/mailers/organization_mailer_spec.rb index f8abf52845..7efbd4a2b0 100644 --- a/spec/mailers/organization_mailer_spec.rb +++ b/spec/mailers/organization_mailer_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe OrganizationMailer, type: :mailer, seed_items: false do +RSpec.describe OrganizationMailer, type: :mailer, skip_seed: true do describe "#partner_approval_request" do subject { described_class.partner_approval_request(organization: organization, partner: partner) } let(:organization) { create(:organization, skip_items: true) } diff --git a/spec/mailers/partner_mailer_spec.rb b/spec/mailers/partner_mailer_spec.rb index f1b9bb086c..a554656001 100644 --- a/spec/mailers/partner_mailer_spec.rb +++ b/spec/mailers/partner_mailer_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe PartnerMailer, type: :mailer, seed_items: false do +RSpec.describe PartnerMailer, type: :mailer, skip_seed: true do describe "#recertification_request" do subject { PartnerMailer.recertification_request(partner: partner) } let(:partner) { create(:partner) } diff --git a/spec/mailers/reminder_deadline_mailer_spec.rb b/spec/mailers/reminder_deadline_mailer_spec.rb index 3ad5e34d9a..3c7f2b0f35 100644 --- a/spec/mailers/reminder_deadline_mailer_spec.rb +++ b/spec/mailers/reminder_deadline_mailer_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe ReminderDeadlineMailer, type: :job, seed_items: false do +RSpec.describe ReminderDeadlineMailer, type: :job do let(:organization) { create(:organization, skip_items: true) } describe 'notify deadline' do diff --git a/spec/mailers/request_mailer_spec.rb b/spec/mailers/request_mailer_spec.rb index f06b6477b1..b5b4733092 100644 --- a/spec/mailers/request_mailer_spec.rb +++ b/spec/mailers/request_mailer_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe RequestMailer, type: :mailer, seed_items: false do +RSpec.describe RequestMailer, type: :mailer, skip_seed: true do describe "#request_cancel_partner_notification" do subject { described_class.request_cancel_partner_notification(request_id: request.id) } let(:request) { create(:request) } diff --git a/spec/mailers/requests_confirmation_mailer_spec.rb b/spec/mailers/requests_confirmation_mailer_spec.rb index ca8a17aa07..6aaeafb1f3 100644 --- a/spec/mailers/requests_confirmation_mailer_spec.rb +++ b/spec/mailers/requests_confirmation_mailer_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe RequestsConfirmationMailer, type: :mailer, seed_items: false do +RSpec.describe RequestsConfirmationMailer, type: :mailer, skip_seed: true do let(:organization) { create(:organization, :with_items) } let(:request) { create(:request, organization: organization) } let(:mail) { RequestsConfirmationMailer.confirmation_email(request) } diff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rb index 657fc30f98..e4b0d56d42 100644 --- a/spec/mailers/user_mailer_spec.rb +++ b/spec/mailers/user_mailer_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe User, type: :mailer, seed_items: false do +RSpec.describe User, type: :mailer, skip_seed: true do describe "#role_added" do let(:user) { create(:user, email: "me@me.com") } let(:partner) { create(:partner, name: "Partner 1") } diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 4182bcfe13..54b0e85a52 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -204,8 +204,6 @@ def seed_base_data_for_tests ] ) end - - seed_base_data_for_tests if !ENV["SKIP_SEED"] end config.before(:each, type: :system) do @@ -214,10 +212,20 @@ def seed_base_data_for_tests Capybara.server = :puma, { Silent: true } end - config.before(:each) do - # Defined shared @ global variables used throughout the test suite. - define_global_variables if RSpec.current_example.metadata[:seed_items] != false + config.before(:all) do + unless Thread.current[:skipped_last_seeding] + DatabaseCleaner.clean_with(:truncation) + end + Thread.current[:skipped_last_seeding] = true + unless self.class.metadata[:skip_seed] + seed_base_data_for_tests + define_global_variables + Thread.current[:skipped_last_seeding] = false + end + end + + config.before(:each) do if ENV['EVENTS_READ'] == 'true' allow(Event).to receive(:read_events?).and_return(true) end diff --git a/spec/requests/product_drives_requests_spec.rb b/spec/requests/product_drives_requests_spec.rb index 10cb04e133..0120f2a76b 100644 --- a/spec/requests/product_drives_requests_spec.rb +++ b/spec/requests/product_drives_requests_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe "ProductDrives", type: :request, skip_seed: true do +RSpec.describe "ProductDrives", type: :request do let(:organization) { create(:organization) } let(:user) { create(:user, organization: organization) } let(:default_params) { { organization_name: organization.to_param } } diff --git a/spec/services/organization_update_service_spec.rb b/spec/services/organization_update_service_spec.rb index f936a2a5ec..3ed51f340c 100644 --- a/spec/services/organization_update_service_spec.rb +++ b/spec/services/organization_update_service_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe OrganizationUpdateService, skip_seed: true do +RSpec.describe OrganizationUpdateService do let(:organization) { create(:organization) } describe "#update" do diff --git a/spec/services/partner_profile_update_service_spec.rb b/spec/services/partner_profile_update_service_spec.rb index e6184b778c..0de174e996 100644 --- a/spec/services/partner_profile_update_service_spec.rb +++ b/spec/services/partner_profile_update_service_spec.rb @@ -40,7 +40,6 @@ expect(result.error.to_s).to include("Validation failed: Total client share must be 0 or 100") profile.reload - puts profile.served_areas.size expect(profile.served_areas.size).to eq(0) end end diff --git a/spec/services/user_invite_service_spec.rb b/spec/services/user_invite_service_spec.rb index 7c85a02601..1cfff287d9 100644 --- a/spec/services/user_invite_service_spec.rb +++ b/spec/services/user_invite_service_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe UserInviteService, type: :service, skip_seed: true do +RSpec.describe UserInviteService, type: :service do let(:organization) { FactoryBot.create(:organization) } before(:each) do allow(UserMailer).to receive(:role_added).and_return(double(:mail, deliver_later: nil))