Sha256: 8cb14458547783a27486417f1f2b7ee03132253cfed3526667e745863d7b0268

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

module AdapterTestHelper

  def self.before_all(adapter_name)
    method = "#{adapter_name}_before_all"
    send method if respond_to? method
  end

  def self.after_all(adapter_name)
    method = "#{adapter_name}_after_all"
    send method if respond_to? method
  end

  def self.before_each(adapter_name)
    method = "#{adapter_name}_before_each"
    send method if respond_to? method
  end

  def self.after_each(adapter_name)
    method = "#{adapter_name}_after_each"
    send method if respond_to? method
  end

  private

  def self.sqlite3_before_each
    ActiveTenant.configuration.global = 'test'
    ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: "#{temp_path}/test.sqlite3"
    ActiveRecord::Base.connection
    load 'models.rb'
  end

  def self.sqlite3_after_each
    ActiveRecord::Base.connection.disconnect!
    Dir.glob("#{temp_path}/*.sqlite3").each do |file|
      FileUtils.rm(file)
    end
  end

  def self.postgresql_before_all
    ActiveTenant.configuration.global = 'public'
    config = {
        adapter: 'postgresql',
        database: 'active_tenant_test',
        schema_search_path: 'public',
        username: 'postgres',
        password: 'password'
    }
    ActiveRecord::Base.establish_connection config.merge database: 'postgres'
    ActiveRecord::Base.connection.drop_database config[:database] rescue nil
    ActiveRecord::Base.connection.create_database config[:database]
    ActiveRecord::Base.establish_connection config
    load 'models.rb'
  end

  def self.postgresql_after_all
    config = ActiveRecord::Base.connection_config
    ActiveRecord::Base.establish_connection config.merge database: 'postgres'
    ActiveRecord::Base.disconnect!
    ActiveRecord::Base.connection.drop_database config[:database]
  end

  private

  def self.temp_path
    path = Pathname.new("#{File.dirname(__FILE__)}/../../tmp").expand_path.to_s
    FileUtils.mkpath path unless Dir.exist? path
    path
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_tenant-0.0.3 spec/support/adapter_test_helper.rb