Skip to content

Commit

Permalink
refactor(tests): change db seeding strategy
Browse files Browse the repository at this point in the history
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
  • Loading branch information
elasticspoon committed Apr 25, 2024
1 parent 24e0edb commit b4af1bf
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion spec/mailers/account_request_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/custom_devise_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/distribution_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/organization_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/partner_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/reminder_deadline_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/request_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/requests_confirmation_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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: "[email protected]") }
let(:partner) { create(:partner, name: "Partner 1") }
Expand Down
18 changes: 13 additions & 5 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/product_drives_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -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 } }
Expand Down
2 changes: 1 addition & 1 deletion spec/services/organization_update_service_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion spec/services/partner_profile_update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/services/user_invite_service_spec.rb
Original file line number Diff line number Diff line change
@@ -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))
Expand Down

0 comments on commit b4af1bf

Please sign in to comment.