fix even more global state spec leakage.

This commit is contained in:
Micah Geisel 2018-04-26 14:14:28 -07:00
parent ec82904231
commit 9615711c81
17 changed files with 412 additions and 362 deletions

View file

@ -3,35 +3,41 @@ require 'support/active_record/mysql2_setup'
require 'database_cleaner/active_record/truncation'
require 'database_cleaner/active_record/truncation/shared_fast_truncation'
module ActiveRecord
module ConnectionAdapters
describe do
before(:all) { MySQL2Helper.active_record_mysql2_setup }
describe DatabaseCleaner::ActiveRecord::Truncation do
let(:helper) { MySQL2Helper.new }
let(:connection) { MySQL2Helper.active_record_mysql2_connection }
let(:connection) do
helper.active_record_mysql2_connection
end
describe "#truncate_table" do
it "should truncate the table" do
2.times { User.create }
around do |example|
helper.active_record_mysql2_setup
connection.truncate_table('users')
expect(User.count).to eq 0
end
example.run
it "should reset AUTO_INCREMENT index of table" do
2.times { User.create }
User.delete_all
helper.active_record_mysql2_teardown
end
connection.truncate_table('users')
describe "AR connection adapter monkeypatches" do
describe "#truncate_table" do
it "should truncate the table" do
2.times { User.create }
expect(User.create.id).to eq 1
end
connection.truncate_table('users')
expect(User.count).to eq 0
end
it_behaves_like "an adapter with pre-count truncation" do
let(:connection) { MySQL2Helper.active_record_mysql2_connection }
it "should reset AUTO_INCREMENT index of table" do
2.times { User.create }
User.delete_all
connection.truncate_table('users')
expect(User.create.id).to eq 1
end
end
it_behaves_like "an adapter with pre-count truncation"
end
end

View file

@ -3,35 +3,41 @@ require 'support/active_record/mysql_setup'
require 'database_cleaner/active_record/truncation'
require 'database_cleaner/active_record/truncation/shared_fast_truncation'
module ActiveRecord
module ConnectionAdapters
describe do
before(:all) { MySQLHelper.active_record_mysql_setup }
describe DatabaseCleaner::ActiveRecord::Truncation do
let(:helper) { MySQLHelper.new }
let(:connection) { MySQLHelper.active_record_mysql_connection }
let(:connection) do
helper.active_record_mysql_connection
end
describe "#truncate_table" do
it "should truncate the table" do
2.times { User.create }
around do |example|
helper.active_record_mysql_setup
connection.truncate_table('users')
expect(User.count).to eq 0
end
example.run
it "should reset AUTO_INCREMENT index of table" do
2.times { User.create }
User.delete_all
helper.active_record_mysql_teardown
end
connection.truncate_table('users')
describe "AR connection adapter monkeypatches" do
describe "#truncate_table" do
it "should truncate the table" do
2.times { User.create }
expect(User.create.id).to eq 1
end
connection.truncate_table('users')
expect(User.count).to eq 0
end
it_behaves_like "an adapter with pre-count truncation" do
let(:connection) { MySQLHelper.active_record_mysql_connection }
it "should reset AUTO_INCREMENT index of table" do
2.times { User.create }
User.delete_all
connection.truncate_table('users')
expect(User.create.id).to eq 1
end
end
it_behaves_like "an adapter with pre-count truncation"
end
end

View file

@ -3,72 +3,76 @@ require 'support/active_record/postgresql_setup'
require 'database_cleaner/active_record/truncation'
require 'database_cleaner/active_record/truncation/shared_fast_truncation'
module ActiveRecord
module ConnectionAdapters
describe "schema_migrations table" do
it "is not truncated" do
PostgreSQLHelper.active_record_pg_migrate
DatabaseCleaner::ActiveRecord::Truncation.new.clean
result = PostgreSQLHelper.active_record_pg_connection.execute("select count(*) from schema_migrations;")
expect(result.values.first).to eq ["2"]
describe DatabaseCleaner::ActiveRecord::Truncation do
let(:helper) { PostgreSQLHelper.new }
let(:connection) do
helper.active_record_pg_connection
end
around do |example|
helper.active_record_pg_setup
example.run
helper.active_record_pg_teardown
end
describe "AR connection adapter monkeypatches" do
describe "#truncate_table" do
it "truncates the table" do
2.times { User.create }
connection.truncate_table('users')
expect(User.count).to eq 0
end
it "truncates the table without id sequence" do
2.times { Agent.create }
connection.truncate_table('agents')
expect(Agent.count).to eq 0
end
it "resets AUTO_INCREMENT index of table" do
2.times { User.create }
User.delete_all
connection.truncate_table('users')
expect(User.create.id).to eq 1
end
end
end
describe do
before(:all) { PostgreSQLHelper.active_record_pg_setup }
describe ":except option cleanup" do
it "should not truncate the tables specified in the :except option" do
2.times { User.create }
let(:connection) do
PostgreSQLHelper.active_record_pg_connection
end
described_class.new(except: ['users']).clean
before(:each) do
connection.truncate_tables connection.tables
end
expect( User.count ).to eq 2
end
end
describe "#truncate_table" do
it "truncates the table" do
2.times { User.create }
describe '#database_cleaner_table_cache' do
it 'should default to the list of tables with their schema' do
expect(connection.database_cleaner_table_cache.first).to match(/^public\./)
end
end
connection.truncate_table('users')
expect(User.count).to eq 0
end
it_behaves_like "an adapter with pre-count truncation"
it "truncates the table without id sequence" do
2.times { Agent.create }
describe "schema_migrations table" do
it "is not truncated" do
helper.active_record_pg_teardown
helper.active_record_pg_migrate
connection.truncate_table('agents')
expect(Agent.count).to eq 0
end
it "resets AUTO_INCREMENT index of table" do
2.times { User.create }
User.delete_all
connection.truncate_table('users')
expect(User.create.id).to eq 1
end
end
describe ":except option cleanup" do
it "should not truncate the tables specified in the :except option" do
2.times { User.create }
DatabaseCleaner::ActiveRecord::Truncation.new(:except => ['users']).clean
expect( User.count ).to eq 2
end
end
describe '#database_cleaner_table_cache' do
it 'should default to the list of tables with their schema' do
expect(connection.database_cleaner_table_cache.first).to match(/^public\./)
end
end
it_behaves_like "an adapter with pre-count truncation" do
let(:connection) { PostgreSQLHelper.active_record_pg_connection }
end
subject.clean
result = connection.execute("select count(*) from schema_migrations;")
expect(result.values.first).to eq ["2"]
end
end
end

View file

@ -2,37 +2,38 @@ require 'active_record'
require 'support/active_record/sqlite3_setup'
require 'database_cleaner/active_record/truncation'
module ActiveRecord
module ConnectionAdapters
describe do
before(:all) { SQLite3Helper.active_record_sqlite3_setup }
describe DatabaseCleaner::ActiveRecord::Truncation do
let(:helper) { SQLite3Helper.new }
let(:connection) do
SQLite3Helper.active_record_sqlite3_connection
let(:connection) do
helper.active_record_sqlite3_connection
end
around do |example|
helper.active_record_sqlite3_setup
example.run
helper.active_record_sqlite3_teardown
end
describe "AR connection adapter monkeypatches" do
describe "#truncate_table" do
it "truncates the table" do
2.times { User.create }
connection.truncate_table('users')
expect(User.count).to eq 0
end
before(:each) do
connection.truncate_tables connection.tables
it "resets AUTO_INCREMENT index of table" do
2.times { User.create }
User.delete_all
connection.truncate_table('users')
expect(User.create.id).to eq 1
end
describe "#truncate_table" do
it "truncates the table" do
2.times { User.create }
connection.truncate_table('users')
expect(User.count).to eq 0
end
it "resets AUTO_INCREMENT index of table" do
2.times { User.create }
User.delete_all
connection.truncate_table('users')
expect(User.create.id).to eq 1
end
end
end
end
end

View file

@ -3,37 +3,37 @@ require 'dm-sqlite-adapter'
require 'support/data_mapper/sqlite3_setup'
require 'database_cleaner/data_mapper/truncation'
module DataMapper
module ConnectionAdapters
describe do
before(:all) { DataMapperSQLite3Helper.data_mapper_sqlite3_setup }
describe DatabaseCleaner::DataMapper::Truncation do
let(:helper) { DataMapperSQLite3Helper.new }
let(:adapter) { DataMapperSQLite3Adapter }
let(:connection) do
helper.data_mapper_sqlite3_connection
end
let(:connection) do
DataMapperSQLite3Helper.data_mapper_sqlite3_connection
around do |example|
helper.data_mapper_sqlite3_setup
example.run
helper.data_mapper_sqlite3_teardown
end
describe "DM connection adapter monkeypatches" do
describe "#truncate_table" do
it "truncates the table" do
2.times { DmUser.create }
connection.truncate_table(DmUser.storage_names[:default])
expect(DmUser.count).to eq 0
end
before(:each) do
connection.truncate_tables(DataMapper::Model.descendants.map { |d| d.storage_names[:default] || d.name.underscore })
end
it "resets AUTO_INCREMENT index of table" do
2.times { DmUser.create }
DmUser.destroy
describe "#truncate_table" do
it "truncates the table" do
2.times { DmUser.create }
connection.truncate_table(DmUser.storage_names[:default])
connection.truncate_table(DmUser.storage_names[:default])
expect(DmUser.count).to eq 0
end
it "resets AUTO_INCREMENT index of table" do
2.times { DmUser.create }
DmUser.destroy
connection.truncate_table(DmUser.storage_names[:default])
expect(DmUser.create.id).to eq 1
end
expect(DmUser.create.id).to eq 1
end
end
end

View file

@ -1,5 +1,6 @@
require 'neo4j-core'
require 'database_cleaner/neo4j/transaction'
require 'database_cleaner/shared_strategy'
module DatabaseCleaner
module Neo4j

View file

@ -1,69 +1,54 @@
require 'ohm'
require 'database_cleaner/ohm/truncation'
module DatabaseCleaner
module Ohm
module OhmTests
class Widget < ::Ohm::Model
attribute :name
end
class Widget < ::Ohm::Model
attribute :name
class Gadget < ::Ohm::Model
attribute :name
end
end
describe DatabaseCleaner::Ohm::Truncation do
around do |example|
config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml"))
Ohm.connect url: config['test']['url']
@redis = Ohm.redis
example.run
@redis.flushdb
end
before do
OhmTests::Widget.new(name: 'some widget').save
OhmTests::Gadget.new(name: 'some gadget').save
end
context "by default" do
it "truncates all keys" do
expect { subject.clean }.to change { @redis.keys.size }.from(6).to(0)
end
end
class Gadget < ::Ohm::Model
attribute :name
context "when keys are provided to the :only option" do
subject { described_class.new(only: ['*Widget*']) }
it "only truncates the specified keys" do
expect { subject.clean }.to change { @redis.keys.size }.from(6).to(3)
expect(@redis.get('OhmTests::Gadget:id')).to eq '1'
end
end
describe Truncation do
before(:all) do
config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml"))
::Ohm.connect :url => config['test']['url']
@redis = ::Ohm.redis
end
context "when keys are provided to the :except option" do
subject { described_class.new(except: ['*Widget*']) }
before(:each) do
@redis.flushdb
end
it "should flush the database" do
Truncation.new.clean
end
def create_widget(attrs={})
Widget.new({:name => 'some widget'}.merge(attrs)).save
end
def create_gadget(attrs={})
Gadget.new({:name => 'some gadget'}.merge(attrs)).save
end
it "truncates all keys by default" do
create_widget
create_gadget
expect(@redis.keys.size).to eq 6
Truncation.new.clean
expect(@redis.keys.size).to eq 0
end
context "when keys are provided to the :only option" do
it "only truncates the specified keys" do
create_widget
create_gadget
expect(@redis.keys.size).to eq 6
Truncation.new(:only => ['*Widget*']).clean
expect(@redis.keys.size).to eq 3
expect(@redis.get('DatabaseCleaner::Ohm::Gadget:id')).to eq '1'
end
end
context "when keys are provided to the :except option" do
it "truncates all but the specified keys" do
create_widget
create_gadget
expect(@redis.keys.size).to eq 6
Truncation.new(:except => ['*Widget*']).clean
expect(@redis.keys.size).to eq 3
expect(@redis.get('DatabaseCleaner::Ohm::Widget:id')).to eq '1'
end
end
it "truncates all but the specified keys" do
expect { subject.clean }.to change { @redis.keys.size }.from(6).to(3)
expect(@redis.get('OhmTests::Widget:id')).to eq '1'
end
end
end

View file

@ -1,61 +1,42 @@
require 'redis'
require 'database_cleaner/redis/truncation'
describe DatabaseCleaner::Redis::Truncation do
around do |example|
config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml"))
@redis = ::Redis.new :url => config['test']['url']
module DatabaseCleaner
module Redis
example.run
describe Truncation do
before(:all) do
config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml"))
@redis = ::Redis.new :url => config['test']['url']
end
@redis.flushdb
end
before(:each) do
@redis.flushdb
end
before do
@redis.set 'Widget', 1
@redis.set 'Gadget', 1
end
it "should flush the database" do
Truncation.new.clean
end
context "by default" do
it "truncates all keys" do
expect { subject.clean }.to change { @redis.keys.size }.from(2).to(0)
end
end
def create_widget(attrs={})
@redis.set 'Widget', 1
end
context "when keys are provided to the :only option" do
subject { described_class.new(only: ['Widge*']) }
def create_gadget(attrs={})
@redis.set 'Gadget', 1
end
it "only truncates the specified keys" do
expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1)
expect(@redis.get('Gadget')).to eq '1'
end
end
it "truncates all keys by default" do
create_widget
create_gadget
expect(@redis.keys.size).to eq 2
Truncation.new.clean
expect(@redis.keys.size).to eq 0
end
context "when keys are provided to the :except option" do
subject { described_class.new(except: ['Widg*']) }
context "when keys are provided to the :only option" do
it "only truncates the specified keys" do
create_widget
create_gadget
expect(@redis.keys.size).to eq 2
Truncation.new(:only => ['Widge*']).clean
expect(@redis.keys.size).to eq 1
expect(@redis.get('Gadget')).to eq '1'
end
end
context "when keys are provided to the :except option" do
it "truncates all but the specified keys" do
create_widget
create_gadget
expect(@redis.keys.size).to eq 2
Truncation.new(:except => ['Widg*']).clean
expect(@redis.keys.size).to eq 1
expect(@redis.get('Widget')).to eq '1'
end
end
it "truncates all but the specified keys" do
expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1)
expect(@redis.get('Widget')).to eq '1'
end
end
end

View file

@ -22,10 +22,6 @@ module DatabaseCleaner
expect(subject.db).to eq :my_db
end
it "should default to :default" do
expect(subject.db).to eq :default
end
pending "I figure out how to use Sequel and write some real tests for it..."
end
end

View file

@ -1,6 +1,8 @@
require 'database_cleaner/sequel/deletion'
require 'database_cleaner/shared_strategy'
require 'sequel'
require 'support/sequel/sequel_setup'
# XXX: use ActiveRecord's db_config (`db/config.yml`) for CI/dev convenience
require 'support/active_record/database_setup'
module DatabaseCleaner
@ -43,13 +45,22 @@ module DatabaseCleaner
supported_configurations = [
{ :url => 'mysql:///', :connection_options => db_config['mysql'] },
{ :url => 'postgres:///', :connection_options => db_config['postgres'] }
{ :url => 'postgres:///', :connection_options => db_config['postgres'] },
]
supported_configurations.each do |config|
describe "Sequel deletion (using a #{config[:url]} connection)" do
let(:db) { ::Sequel.connect(config[:url], config[:connection_options]) }
around do |example|
helper = SequelHelper.new(config)
helper.setup
example.run
helper.teardown
end
it_behaves_like 'a Sequel deletion strategy'
end
end

View file

@ -1,7 +1,7 @@
require 'database_cleaner/sequel/truncation'
require 'database_cleaner/shared_strategy'
require 'sequel'
require 'support/sequel/sequel_setup'
# XXX: use ActiveRecord's db_config (`db/config.yml`) for CI/dev convenience
require 'support/active_record/database_setup'
@ -22,7 +22,7 @@ module DatabaseCleaner
end
context 'when several tables have data' do
before(:each) do
before do
db.create_table!(:precious_stones) { primary_key :id }
db.create_table!(:replaceable_trifles) { primary_key :id }
db.create_table!(:worthless_junk) { primary_key :id }
@ -122,13 +122,24 @@ module DatabaseCleaner
{url: 'mysql:///', connection_options: db_config['mysql']},
{url: 'mysql2:///', connection_options: db_config['mysql2']}
]
supported_configurations.each do |config|
describe "Sequel truncation (using a #{config[:url]} connection)" do
around do |example|
helper = SequelHelper.new(config)
helper.setup
example.run
helper.teardown
end
let(:db) { ::Sequel.connect(config[:url], config[:connection_options]) }
it_behaves_like 'a Sequel truncation strategy'
it_behaves_like 'a truncation strategy that resets autoincrement keys by default'
describe '#pre_count?' do
subject { Truncation.new.tap { |t| t.db = db } }
@ -170,8 +181,18 @@ module DatabaseCleaner
end
end
end
half_supported_configurations.each do |config|
describe "Sequel truncation (using a #{config[:url]} connection)" do
around do |example|
helper = SequelHelper.new(config)
helper.setup
example.run
helper.teardown
end
let(:db) { ::Sequel.connect(config[:url], config[:connection_options]) }
it_behaves_like 'a Sequel truncation strategy'

View file

@ -1,29 +1,12 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
module MySQL2Helper
extend self
class MySQL2Helper
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2"
# require 'logger'
# ActiveRecord::Base.logger = Logger.new(STDERR)
def default_config
db_config['mysql2']
end
def create_db
establish_connection(default_config.merge(:database => nil))
ActiveRecord::Base.connection.drop_database default_config['database'] rescue nil
ActiveRecord::Base.connection.create_database default_config['database']
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection config
end
def active_record_mysql2_setup
patch_mysql2_adapter
create_db
@ -35,6 +18,27 @@ module MySQL2Helper
ActiveRecord::Base.connection
end
def active_record_mysql2_teardown
ActiveRecord::Base.connection.drop_database default_config['database']
end
private
def default_config
db_config['mysql2']
end
def create_db
establish_connection(default_config.merge("database" => nil))
ActiveRecord::Base.connection.drop_database default_config['database'] rescue nil
ActiveRecord::Base.connection.create_database default_config['database']
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection config
end
def patch_mysql2_adapter
# remove DEFAULT NULL from column definition, which is an error on primary keys in MySQL 5.7.3+
ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"

View file

@ -1,38 +1,37 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
module MySQLHelper
extend self
class MySQLHelper
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql"
# require 'logger'
# ActiveRecord::Base.logger = Logger.new(STDERR)
def active_record_mysql_setup
patch_mysql_adapter
create_db
active_record_load_schema
end
def active_record_mysql_connection
ActiveRecord::Base.connection
end
def active_record_mysql_teardown
ActiveRecord::Base.connection.drop_database default_config['database']
end
private
def default_config
db_config['mysql']
end
def create_db
establish_connection(default_config.merge(:database => nil))
ActiveRecord::Base.establish_connection default_config.merge("database" => nil)
ActiveRecord::Base.connection.drop_database default_config['database'] rescue nil
ActiveRecord::Base.connection.create_database default_config['database']
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection config
end
def active_record_mysql_setup
patch_mysql_adapter
create_db
establish_connection
active_record_load_schema
end
def active_record_mysql_connection
ActiveRecord::Base.connection
ActiveRecord::Base.establish_connection default_config
end
def patch_mysql_adapter

View file

@ -1,33 +1,11 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
module PostgreSQLHelper
extend self
class PostgreSQLHelper
puts "Active Record #{ActiveRecord::VERSION::STRING}, pg"
# ActiveRecord::Base.logger = Logger.new(STDERR)
def default_config
db_config['postgres']
end
def create_db
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
begin
establish_connection(default_config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.drop_database(default_config['database']) rescue nil
ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding))
rescue Exception => e
$stderr.puts e, *(e.backtrace)
$stderr.puts "Couldn't create database for #{default_config.inspect}"
end
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection(config)
end
def active_record_pg_setup
create_db
establish_connection
@ -35,13 +13,33 @@ module PostgreSQLHelper
end
def active_record_pg_migrate
create_db
establish_connection
ActiveRecord::Migrator.migrate 'spec/support/active_record/migrations'
end
def active_record_pg_connection
ActiveRecord::Base.connection
end
def active_record_pg_teardown
ActiveRecord::Base.connection.execute "DROP TABLE users, agents;"
rescue ActiveRecord::StatementInvalid
end
private
def default_config
db_config['postgres']
end
def create_db
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
establish_connection(default_config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding))
rescue ActiveRecord::StatementInvalid
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection(config)
end
end

View file

@ -1,31 +1,11 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
module SQLite3Helper
extend self
class SQLite3Helper
puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3"
# ActiveRecord::Base.logger = Logger.new(STDERR)
def default_config
db_config['sqlite3']
end
def create_db
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
begin
establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
rescue Exception => e
$stderr.puts e, *(e.backtrace)
$stderr.puts "Couldn't create database for #{default_config.inspect}"
end
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection(config)
end
def active_record_sqlite3_setup
create_db
establish_connection
@ -35,5 +15,25 @@ module SQLite3Helper
def active_record_sqlite3_connection
ActiveRecord::Base.connection
end
def active_record_sqlite3_teardown
ActiveRecord::Base.connection.truncate_table('users')
ActiveRecord::Base.connection.truncate_table('agents')
end
private
def default_config
db_config['sqlite3']
end
def create_db
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection(config)
end
end

View file

@ -1,29 +1,9 @@
require 'support/active_record/database_setup'
require 'support/data_mapper/schema_setup'
module DataMapperSQLite3Helper
extend self
class DataMapperSQLite3Helper
puts "DataMapper #{DataMapper::VERSION}, sqlite3"
def default_config
db_config['sqlite3']
end
def create_db
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
begin
establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
rescue Exception => e
$stderr.puts e, *(e.backtrace)
$stderr.puts "Couldn't create database for #{default_config.inspect}"
end
end
def establish_connection(config = default_config)
DataMapper.setup(:default, config)
end
def data_mapper_sqlite3_setup
create_db
establish_connection
@ -33,5 +13,24 @@ module DataMapperSQLite3Helper
def data_mapper_sqlite3_connection
DataMapper.repository.adapter
end
def data_mapper_sqlite3_teardown
DataMapper.repository.adapter.truncate_tables(DataMapper::Model.descendants.map { |d| d.storage_names[:default] || d.name.underscore })
end
private
def default_config
db_config['sqlite3']
end
def create_db
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
end
def establish_connection(config = default_config)
DataMapper.setup(:default, config)
end
end

View file

@ -0,0 +1,38 @@
class SequelHelper < Struct.new(:config)
def setup
if config[:url] == "postgres:///" || config[:url] == "sqlite:///"
::Sequel.connect(config[:url], config[:connection_options].merge('database' => 'postgres')) do |db|
begin
db.execute "CREATE DATABASE #{database}"
rescue ::Sequel::DatabaseError
end
end
else
::Sequel.connect(config[:url], config[:connection_options].merge('database' => nil)) do |db|
db.execute "DROP DATABASE IF EXISTS #{database}"
db.execute "CREATE DATABASE #{database}"
end
end
end
def teardown
if config[:url] == "postgres:///" || config[:url] == "sqlite:///"
::Sequel.connect(config[:url], config[:connection_options]) do |db|
db.execute "DROP TABLE IF EXISTS precious_stones"
db.execute "DROP TABLE IF EXISTS replaceable_trifles"
db.execute "DROP TABLE IF EXISTS worthless_junk"
end
else
::Sequel.connect(config[:url], config[:connection_options].merge('database' => nil)) do |db|
db.execute "DROP DATABASE IF EXISTS #{database}"
end
end
rescue SQLite3::BusyException
end
private
def database
config[:connection_options]['database']
end
end