forked from test-prof/test-prof
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle new connection pools in before_all
Ref test-prof#310
- Loading branch information
Showing
10 changed files
with
140 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem "activerecord", "~> 7.0" | ||
gem "factory_bot" | ||
gem "fabrication" | ||
gem "sqlite3", "~> 2.0" | ||
gem "sidekiq" | ||
gem "timecop" | ||
gem "pg" | ||
|
||
gemspec path: '..' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
# https://github.com/test-prof/test-prof/issues/310 | ||
describe "before_all + any_fixture + multidb", type: :integration do | ||
specify "works" do | ||
output = run_rspec( | ||
"before_all_any_fixture_multidb", | ||
chdir: File.join(__dir__, "fixtures") | ||
) | ||
|
||
expect(output).to include("0 failures") | ||
end | ||
end |
79 changes: 79 additions & 0 deletions
79
spec/bugs/fixtures/before_all_any_fixture_multidb_fixture.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
require "action_controller/railtie" | ||
require "action_view/railtie" | ||
require "active_record/railtie" | ||
require "rspec/rails" | ||
|
||
require_relative "../../support/ar_models" | ||
|
||
RSpec.configure do |config| | ||
config.fixture_path = File.join(__dir__, "fixtures") | ||
config.use_transactional_fixtures = true | ||
end | ||
|
||
require "test_prof/recipes/rspec/before_all" | ||
require "test_prof/recipes/rspec/let_it_be" | ||
require "test_prof/recipes/rspec/any_fixture" | ||
|
||
RSpec.configure do |config| | ||
config.include TestProf::FactoryBot::Syntax::Methods | ||
|
||
config.before(:suite) do | ||
TestProf::AnyFixture.register(:user) { TestProf::FactoryBot.create(:user) } | ||
end | ||
end | ||
|
||
# ActiveSupport::Notifications.subscribe("transaction.active_record") do |event| | ||
# puts "[TRANSACTION] #{event.payload[:outcome].upcase} #{event.payload[:connection].inspect} from: #{caller_locations.find { |l| l.to_s.include?(Rails.root.to_s) }&.to_s}" | ||
# end | ||
|
||
# TestProf::BeforeAll.configure do |config| | ||
# config.before(:begin) do | ||
# puts "[BEFORE_ALL] connection pools #{::ActiveRecord::Base.connection_handler.connection_pool_list(:writing).map(&:inspect)}" | ||
# end | ||
|
||
# config.before(:rollback) do | ||
# puts "[BEFORE_ALL] connection pools before unpin_connection! #{::ActiveRecord::Base.connection_handler.connection_pool_list(:writing).map(&:inspect)}" | ||
# end | ||
# end | ||
|
||
describe "let_it_be vs lazy multi db" do | ||
let_it_be(:user) { TestProf::FactoryBot.create(:user) } | ||
|
||
# Loading an AR class with a custom DB configuration | ||
# triggers a new connection pool creation that hasn't been tracked by | ||
# before_all... | ||
let!(:dual_comments_class) do | ||
Class.new(ApplicationRecord) do | ||
def self.name | ||
"DualCommentsRecord" | ||
end | ||
|
||
self.abstract_class = true | ||
|
||
connects_to database: {writing: :comments, reading: :primary} if multi_db? | ||
end.then do |record_class| | ||
Class.new(record_class) do | ||
self.table_name = "comments" | ||
|
||
belongs_to :user, dependent: :destroy | ||
end | ||
end | ||
end | ||
|
||
specify do | ||
expect(User.count).to eq 2 | ||
|
||
dual_comments_class.create!(user: user, comment: "Hello!") | ||
end | ||
|
||
specify do | ||
expect(TestProf::FactoryBot.create(:comment)).to be_present | ||
expect(Comment.count).to eq 1 | ||
end | ||
|
||
context "with clean fixture", :with_clean_fixture do | ||
specify { expect(User.count).to eq 0 } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters