Files
has_aux_table/spec/spec_helper.rb
Dylan Knutson 8e1c193801 spec refactor
2025-07-18 05:51:24 +00:00

137 lines
3.3 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:"
)
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.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
sig { void }
def self.initialize_spec_schema!
# Set up the database schema for testing
ActiveRecord::Schema.define do
create_table :vehicle_lots do |t|
t.string :name
t.timestamps
end
create_base_table :vehicles do |t|
t.string :name
t.references :vehicle_lot, foreign_key: { to_table: :vehicle_lots }
t.timestamps
t.create_aux :car do |t|
t.string :fuel_type
t.decimal :engine_size, precision: 3, scale: 1
end
t.create_aux :boat do |t|
t.boolean :only_freshwater
end
end
create_base_table :people do |t|
t.string :name
t.timestamps
t.create_aux :driver do |t|
t.integer :license_number
t.references :car,
null: false,
foreign_key: {
to_table: :vehicles_car_aux,
primary_key: :base_table_id
}
end
t.create_aux :captain do |t|
t.references :boat,
null: false,
foreign_key: {
to_table: :vehicles_boat_aux,
primary_key: :base_table_id
}
end
t.create_aux :passenger do |t|
t.references :boat,
null: false,
foreign_key: {
to_table: :vehicles_boat_aux,
primary_key: :base_table_id
}
end
end
create_base_table :utensils do |t|
t.string :name
t.string :material
t.timestamps
t.create_aux :fork do |t|
t.integer :num_tongs
end
t.create_aux :spoon do |t|
t.string :curvature
end
end
require_relative "spec_models"
end
end
end