Sha256: 86e1db6e91233d8fca4a56929b2a4a75bf7071f9d1183968ff5198ae0008ee89

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

module Apartment
  module Spec
    module Setup

      def self.included(base)
        base.instance_eval do
          let(:config){ database_config }

          let(:db1){ Apartment::Test.next_db }
          let(:db2){ Apartment::Test.next_db }
          let(:connection){ ActiveRecord::Base.connection }

          # This around ensures that we run these hooks before and after
          # any before/after hooks defined in individual tests
          # Otherwise these actually get run after test defined hooks
          around(:each) do |example|
            # before
            Apartment::Database.reload!(config)
            ActiveRecord::Base.establish_connection config

            example.run

            # after
            Rails.configuration.database_configuration = {}
            ActiveRecord::Base.clear_all_connections!

            Apartment.excluded_models.each do |model|
              klass = model.constantize
              
              Apartment.connection_class.remove_connection(klass)
              klass.clear_all_connections!
              klass.reset_table_name
            end

            Apartment.reset
            Apartment::Database.reload!
          end
        end
      end

      def database_config
        db = example.metadata.fetch(:database, :postgresql)
        Apartment::Test.config['connections'][db.to_s].symbolize_keys
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
apartment-0.24.3 spec/support/setup.rb
apartment-0.24.2 spec/support/setup.rb
apartment-0.24.1 spec/support/setup.rb
apartment-0.24.0 spec/support/setup.rb
apartment-0.23.2 spec/support/setup.rb
apartment-0.23.1 spec/support/setup.rb
apartment-0.23.0 spec/support/setup.rb