Skip to content

Commit

Permalink
Only seed tenants when necessary in tests
Browse files Browse the repository at this point in the history
On my machine, seeding a tenant takes about one second, so skipping this
action when it isn't necessary makes tests creating tenants faster
(although creating a tenant still takes about 3-4 seconds on my
machine).
  • Loading branch information
javierm committed Oct 19, 2022
1 parent 7d6a261 commit 1afdcb2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/initializers/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#
Apartment.configure do |config|
ENV["IGNORE_EMPTY_TENANTS"] = "true" unless Rails.application.config.multitenancy.present?
config.seed_after_create = true
config.seed_after_create = !Rails.env.test?

# Add any models that you do not want to be multi-tenanted, but remain in the global (public) namespace.
# A typical example would be a Customer or Tenant model that stores each Tenant's information.
Expand Down
3 changes: 3 additions & 0 deletions spec/lib/tasks/sitemap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,18 @@
create(:tenant, subdomain: "debates")
create(:tenant, subdomain: "proposals")

Setting["process.budgets"] = true
Setting["process.debates"] = false
Setting["process.proposals"] = false

Tenant.switch("debates") do
Setting["process.debates"] = true
Setting["process.budgets"] = false
Setting["process.proposals"] = false
end

Tenant.switch("proposals") do
Setting["process.proposals"] = true
Setting["process.budgets"] = false
Setting["process.debates"] = false
end
Expand Down
1 change: 1 addition & 0 deletions spec/models/attachable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
create(:tenant, subdomain: "image-master")

Tenant.switch("image-master") do
Setting.reset_defaults
tenant_image = create(:image, attachment: fixture_file_upload("clippy.jpg"))

expect(tenant_image.file_path).to include "storage/tenants/image-master/"
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@
Delayed::Worker.delay_jobs = false
end

config.before(:each, :seed_tenants) do
Apartment.seed_after_create = true
end

config.after(:each, :seed_tenants) do
Apartment.seed_after_create = false
end

config.before(:each, :small_window) do
@window_size = Capybara.current_window.size
Capybara.current_window.resize_to(639, 479)
Expand Down
2 changes: 1 addition & 1 deletion spec/system/admin/tenants_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "Tenants", :admin do
describe "Tenants", :admin, :seed_tenants do
before { allow(Tenant).to receive(:default_host).and_return("localhost") }

scenario "Create" do
Expand Down
2 changes: 1 addition & 1 deletion spec/system/multitenancy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "Multitenancy" do
describe "Multitenancy", :seed_tenants do
before do
create(:tenant, subdomain: "mars")
create(:tenant, subdomain: "venus")
Expand Down

0 comments on commit 1afdcb2

Please sign in to comment.