lib/fixtury/minitest_hooks.rb in fixtury-1.0.1 vs lib/fixtury/minitest_hooks.rb in fixtury-2.0.0

- old
+ new

@@ -5,12 +5,11 @@ module Fixtury # MinitestHooks is a module designed to hook into a Minitest test case, and # provide a way to load fixtures into the test case. It is designed to be # prepended into the test case class, and will automatically load fixtures - # before the test case is setup, and rollback any changes after the test - # case is torn down. + # before the test case is setup. # # The module also provides a way to define fixture dependencies, and will # automatically load those dependencies before the test case is setup. # # @example @@ -42,18 +41,19 @@ # # fixtury "/my/user_record", as: :user # # end # + # Use `as: false` if you do not want an accessor created. + # # A Set object named fixtury_dependencies is made available on the test class. # This allows you to load all Minitest runnables and analyze what fixtures are # needed. This is very helpful in CI pipelines when you want to prepare all fixtures # ahead of time to share between multiple processes. # - # The setup and teardown attempt to manage a transaction for each registered database - # connection if ActiveRecord::Base is present. If use_transaction_tests or use_transactional_fixtures - # are present, those settings will be respected. If neither are present, a transaction will be used. + # It is the responsibility of the suite to manage the snapshot or rollback of the database. Generally + # something like ActiveRecord's use_transactional_fixtures will work just fine. module MinitestHooks def self.prepended(klass) klass.class_attribute :fixtury_dependencies klass.fixtury_dependencies = Set.new @@ -65,12 +65,11 @@ end module ClassMethods # Declare fixtury dependencies for this test case. This will automatically - # load the fixtures before the test case is setup, and rollback any changes - # after the test case is torn down. + # load the fixtures before the test case is setup. # # @param searches [Array<String>] A list of fixture names to load. These should be resolvable paths relative to Fixtury.schema (root). # @param opts [Hash] A list of options to customize the behavior of the fixtures. # @option opts [Symbol, String, Boolean] :as (true) The name of the accessor method to define for the fixture. If true (default), the last segment will be used. # @return [void] @@ -113,16 +112,10 @@ def before_setup(...) fixtury_setup if fixtury_dependencies.any? super end - # Minitest after_teardown hook. This will rollback any changes made to the fixtures after the test. - def after_teardown(...) - super - fixtury_teardown if fixtury_dependencies.any? - end - # Access a fixture via a search term. This will access the fixture from the Fixtury store. # If the fixture was not declared as a dependency, an error will be raised. # # @param search [String] The search term to use to find the fixture. # @return [Object] The fixture. @@ -136,67 +129,25 @@ end Fixtury.store.get(dfn.pathname) end - # Retrieve all database connections that are currently registered with a writing role. - # - # @return [Array<ActiveRecord::ConnectionAdapters::AbstractAdapter>] The list of database connections. - def fixtury_database_connections - return [] unless defined?(ActiveRecord::Base) - - pools = ActiveRecord::Base.connection_handler.connection_pool_list(:writing) - pools.map { |pool| pool.respond_to?(:lease_connection) ? pool.lease_connection : pool.connection } - end - # Load all dependenct fixtures and begin a transaction for each database connection. def fixtury_setup Fixtury.store.clear_stale_references! fixtury_load_all_fixtures! - return unless fixtury_use_transactions? - - fixtury_database_connections.each do |conn| - conn.begin_transaction joinable: false - if conn.pool.respond_to?(:lock_thread=) - conn.pool.lock_thread = true - elsif conn.pool.respond_to?(:pin_connection!) - conn.pool.pin_connection!(true) - end - end end - # Rollback any changes made to the fixtures - def fixtury_teardown - return unless fixtury_use_transactions? - - fixtury_database_connections.each do |conn| - conn.rollback_transaction if conn.open_transactions.positive? - if conn.pool.respond_to?(:lock_thread=) - conn.pool.lock_thread = false - elsif conn.pool.respond_to?(:unpin_connection!) - conn.pool.unpin_connection! - end - end - end - # Load all fixture dependencies that have not previously been loaded into the store. # # @return [void] def fixtury_load_all_fixtures! fixtury_dependencies.each do |name| next if Fixtury.store.loaded?(name) ::Fixtury.log("preloading #{name.inspect}", name: "test", level: ::Fixtury::LOG_LEVEL_INFO) fixtury(name) end - end - - # Adhere to common Rails test transaction settings. - def fixtury_use_transactions? - return use_transactional_tests if respond_to?(:use_transactional_tests) - return use_transactional_fixtures if respond_to?(:use_transactional_fixtures) - - true end end end