Files
has_aux_table/spec/spec_helper.rb
2025-07-20 17:56:08 +00:00

66 lines
1.5 KiB
Ruby

# typed: strict
# frozen_string_literal: true
require "active_record"
require "active_record/errors"
require "has_aux_table"
require "pry"
# Configure ActiveRecord to use in-memory SQLite database
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: ":memory:"
)
require_relative "spec_models"
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
# config.backtrace_inclusion_patterns = [/\bactiverecord\b/]
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.around(:each) do |example|
ActiveRecord::Base.transaction do
example.run
raise ActiveRecord::Rollback
end
end
end
module SpecHelper
extend T::Sig
extend T::Helpers
LOG_QUERIES = T.let(false, T::Boolean)
# Helper method to count queries
sig { params(block: T.proc.void).returns(Integer) }
def self.count_queries(&block)
query_count = 0
query_callback =
lambda { |name, start, finish, message_id, values| query_count += 1 }
ActiveSupport::Notifications.subscribed(
query_callback,
"sql.active_record"
) do
if LOG_QUERIES
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger.level = Logger::DEBUG
end
block.call
ActiveRecord::Base.logger = old_logger if LOG_QUERIES
end
query_count
end
end