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

View file

@ -2,37 +2,38 @@ require 'active_record'
require 'support/active_record/sqlite3_setup' require 'support/active_record/sqlite3_setup'
require 'database_cleaner/active_record/truncation' require 'database_cleaner/active_record/truncation'
module ActiveRecord describe DatabaseCleaner::ActiveRecord::Truncation do
module ConnectionAdapters let(:helper) { SQLite3Helper.new }
describe do
before(:all) { SQLite3Helper.active_record_sqlite3_setup }
let(:connection) do let(:connection) do
SQLite3Helper.active_record_sqlite3_connection 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 end
before(:each) do it "resets AUTO_INCREMENT index of table" do
connection.truncate_tables connection.tables 2.times { User.create }
User.delete_all
connection.truncate_table('users')
expect(User.create.id).to eq 1
end 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 end
end end

View file

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

View file

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

View file

@ -1,69 +1,54 @@
require 'ohm' require 'ohm'
require 'database_cleaner/ohm/truncation' require 'database_cleaner/ohm/truncation'
module DatabaseCleaner module OhmTests
module Ohm class Widget < ::Ohm::Model
attribute :name
end
class Widget < ::Ohm::Model class Gadget < ::Ohm::Model
attribute :name 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
end
class Gadget < ::Ohm::Model context "when keys are provided to the :only option" do
attribute :name 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
end
describe Truncation do context "when keys are provided to the :except option" do
before(:all) do subject { described_class.new(except: ['*Widget*']) }
config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml"))
::Ohm.connect :url => config['test']['url']
@redis = ::Ohm.redis
end
before(:each) do it "truncates all but the specified keys" do
@redis.flushdb expect { subject.clean }.to change { @redis.keys.size }.from(6).to(3)
end expect(@redis.get('OhmTests::Widget:id')).to eq '1'
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
end end
end end
end end

View file

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

View file

@ -22,10 +22,6 @@ module DatabaseCleaner
expect(subject.db).to eq :my_db expect(subject.db).to eq :my_db
end 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..." pending "I figure out how to use Sequel and write some real tests for it..."
end end
end end

View file

@ -1,6 +1,8 @@
require 'database_cleaner/sequel/deletion' require 'database_cleaner/sequel/deletion'
require 'database_cleaner/shared_strategy' require 'database_cleaner/shared_strategy'
require 'sequel' 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' require 'support/active_record/database_setup'
module DatabaseCleaner module DatabaseCleaner
@ -43,13 +45,22 @@ module DatabaseCleaner
supported_configurations = [ supported_configurations = [
{ :url => 'mysql:///', :connection_options => db_config['mysql'] }, { :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| supported_configurations.each do |config|
describe "Sequel deletion (using a #{config[:url]} connection)" do describe "Sequel deletion (using a #{config[:url]} connection)" do
let(:db) { ::Sequel.connect(config[:url], config[:connection_options]) } 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' it_behaves_like 'a Sequel deletion strategy'
end end
end end

View file

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

View file

@ -1,29 +1,12 @@
require 'support/active_record/database_setup' require 'support/active_record/database_setup'
require 'support/active_record/schema_setup' require 'support/active_record/schema_setup'
module MySQL2Helper class MySQL2Helper
extend self
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2" puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2"
# require 'logger' # require 'logger'
# ActiveRecord::Base.logger = Logger.new(STDERR) # 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 def active_record_mysql2_setup
patch_mysql2_adapter patch_mysql2_adapter
create_db create_db
@ -35,6 +18,27 @@ module MySQL2Helper
ActiveRecord::Base.connection ActiveRecord::Base.connection
end 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 def patch_mysql2_adapter
# remove DEFAULT NULL from column definition, which is an error on primary keys in MySQL 5.7.3+ # 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" 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/database_setup'
require 'support/active_record/schema_setup' require 'support/active_record/schema_setup'
module MySQLHelper class MySQLHelper
extend self
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql" puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql"
# require 'logger' # require 'logger'
# ActiveRecord::Base.logger = Logger.new(STDERR) # 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 def default_config
db_config['mysql'] db_config['mysql']
end end
def create_db 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.drop_database default_config['database'] rescue nil
ActiveRecord::Base.connection.create_database default_config['database'] ActiveRecord::Base.connection.create_database default_config['database']
end ActiveRecord::Base.establish_connection default_config
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
end end
def patch_mysql_adapter def patch_mysql_adapter

View file

@ -1,33 +1,11 @@
require 'support/active_record/database_setup' require 'support/active_record/database_setup'
require 'support/active_record/schema_setup' require 'support/active_record/schema_setup'
module PostgreSQLHelper class PostgreSQLHelper
extend self
puts "Active Record #{ActiveRecord::VERSION::STRING}, pg" puts "Active Record #{ActiveRecord::VERSION::STRING}, pg"
# ActiveRecord::Base.logger = Logger.new(STDERR) # 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 def active_record_pg_setup
create_db create_db
establish_connection establish_connection
@ -35,13 +13,33 @@ module PostgreSQLHelper
end end
def active_record_pg_migrate def active_record_pg_migrate
create_db
establish_connection
ActiveRecord::Migrator.migrate 'spec/support/active_record/migrations' ActiveRecord::Migrator.migrate 'spec/support/active_record/migrations'
end end
def active_record_pg_connection def active_record_pg_connection
ActiveRecord::Base.connection ActiveRecord::Base.connection
end 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 end

View file

@ -1,31 +1,11 @@
require 'support/active_record/database_setup' require 'support/active_record/database_setup'
require 'support/active_record/schema_setup' require 'support/active_record/schema_setup'
module SQLite3Helper class SQLite3Helper
extend self
puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3" puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3"
# ActiveRecord::Base.logger = Logger.new(STDERR) # 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 def active_record_sqlite3_setup
create_db create_db
establish_connection establish_connection
@ -35,5 +15,25 @@ module SQLite3Helper
def active_record_sqlite3_connection def active_record_sqlite3_connection
ActiveRecord::Base.connection ActiveRecord::Base.connection
end 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 end

View file

@ -1,29 +1,9 @@
require 'support/active_record/database_setup' require 'support/active_record/database_setup'
require 'support/data_mapper/schema_setup' require 'support/data_mapper/schema_setup'
module DataMapperSQLite3Helper class DataMapperSQLite3Helper
extend self
puts "DataMapper #{DataMapper::VERSION}, sqlite3" 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 def data_mapper_sqlite3_setup
create_db create_db
establish_connection establish_connection
@ -33,5 +13,24 @@ module DataMapperSQLite3Helper
def data_mapper_sqlite3_connection def data_mapper_sqlite3_connection
DataMapper.repository.adapter DataMapper.repository.adapter
end 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 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