mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
sequel helper can now be implemented almost entirely in terms of database helper.
This commit is contained in:
parent
28541491de
commit
0e9f57060f
4 changed files with 34 additions and 101 deletions
|
@ -7,14 +7,9 @@ module DatabaseCleaner
|
|||
RSpec.describe Deletion do
|
||||
it_should_behave_like "a generic strategy"
|
||||
|
||||
[
|
||||
{ url: 'mysql:///', connection_options: db_config['mysql'] },
|
||||
{ url: 'mysql2:///', connection_options: db_config['mysql2'] },
|
||||
{ url: 'sqlite:///', connection_options: db_config['sqlite3'] },
|
||||
{ url: 'postgres:///', connection_options: db_config['postgres'] },
|
||||
].each do |config|
|
||||
context "using a #{config[:url]} connection" do
|
||||
let(:helper) { SequelHelper.new(config) }
|
||||
%w[mysql mysql2 sqlite3 postgres].map(&:to_sym).each do |db|
|
||||
context "using a #{db} connection" do
|
||||
let(:helper) { SequelHelper.new(nil, db) }
|
||||
|
||||
around do |example|
|
||||
helper.setup
|
||||
|
@ -22,22 +17,22 @@ module DatabaseCleaner
|
|||
helper.teardown
|
||||
end
|
||||
|
||||
let(:db) { helper.connection }
|
||||
let(:connection) { helper.connection }
|
||||
|
||||
before { subject.db = db }
|
||||
before { subject.db = connection }
|
||||
|
||||
context 'when several tables have data' do
|
||||
before do
|
||||
db[:users].insert
|
||||
db[:agents].insert
|
||||
connection[:users].insert
|
||||
connection[:agents].insert
|
||||
end
|
||||
|
||||
context 'by default' do
|
||||
it 'deletes all the tables' do
|
||||
subject.clean
|
||||
|
||||
expect(db[:users]).to be_empty
|
||||
expect(db[:agents]).to be_empty
|
||||
expect(connection[:users]).to be_empty
|
||||
expect(connection[:agents]).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,14 +8,9 @@ module DatabaseCleaner
|
|||
it_should_behave_like "a generic strategy"
|
||||
it_should_behave_like "a generic truncation strategy"
|
||||
|
||||
[
|
||||
{ url: 'mysql:///', connection_options: db_config['mysql'] },
|
||||
{ url: 'mysql2:///', connection_options: db_config['mysql2'] },
|
||||
{ url: 'sqlite:///', connection_options: db_config['sqlite3'] },
|
||||
{ url: 'postgres:///', connection_options: db_config['postgres'] },
|
||||
].each do |config|
|
||||
context "using a #{config[:url]} connection" do
|
||||
let(:helper) { SequelHelper.new(config) }
|
||||
%w[mysql mysql2 sqlite3 postgres].map(&:to_sym).each do |db|
|
||||
context "using a #{db} connection" do
|
||||
let(:helper) { SequelHelper.new(nil, db) }
|
||||
|
||||
around do |example|
|
||||
helper.setup
|
||||
|
@ -23,22 +18,22 @@ module DatabaseCleaner
|
|||
helper.teardown
|
||||
end
|
||||
|
||||
let(:db) { helper.connection }
|
||||
let(:connection) { helper.connection }
|
||||
|
||||
before { subject.db = db }
|
||||
before { subject.db = connection }
|
||||
|
||||
context 'when several tables have data' do
|
||||
before do
|
||||
db[:users].insert
|
||||
db[:agents].insert
|
||||
connection[:users].insert
|
||||
connection[:agents].insert
|
||||
end
|
||||
|
||||
context 'by default' do
|
||||
it 'truncates all the tables' do
|
||||
subject.clean
|
||||
|
||||
expect(db[:users]).to be_empty
|
||||
expect(db[:agents]).to be_empty
|
||||
expect(connection[:users]).to be_empty
|
||||
expect(connection[:agents]).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -48,8 +43,8 @@ module DatabaseCleaner
|
|||
it 'truncates only the mentioned tables (and leaves the rest alone)' do
|
||||
subject.clean
|
||||
|
||||
expect(db[:users]).to be_empty
|
||||
expect(db[:agents].count).to eq(1)
|
||||
expect(connection[:users]).to be_empty
|
||||
expect(connection[:agents].count).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -59,15 +54,15 @@ module DatabaseCleaner
|
|||
it 'leaves the mentioned tables alone (and truncates the rest)' do
|
||||
subject.clean
|
||||
|
||||
expect(db[:users].count).to eq(1)
|
||||
expect(db[:agents]).to be_empty
|
||||
expect(connection[:users].count).to eq(1)
|
||||
expect(connection[:agents]).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'auto increment sequences' do
|
||||
it "resets AUTO_INCREMENT primary key seqeunce" do
|
||||
table = db[:users]
|
||||
table = connection[:users]
|
||||
2.times { table.insert }
|
||||
|
||||
subject.clean
|
||||
|
@ -80,12 +75,12 @@ module DatabaseCleaner
|
|||
describe "with pre_count optimization option" do
|
||||
subject { described_class.new(pre_count: true) }
|
||||
|
||||
before { db[:users].insert }
|
||||
before { connection[:users].insert }
|
||||
|
||||
it "only truncates non-empty tables" do
|
||||
sql = case config[:url]
|
||||
when 'sqlite:///' then ["DELETE FROM `users`", anything]
|
||||
when 'postgres:///' then ['TRUNCATE TABLE "users" RESTART IDENTITY;', anything]
|
||||
sql = case helper.db
|
||||
when :sqlite3 then ["DELETE FROM `users`", anything]
|
||||
when :postgres then ['TRUNCATE TABLE "users" RESTART IDENTITY;', anything]
|
||||
else ["TRUNCATE TABLE `users`", anything]
|
||||
end
|
||||
expect(subject.db).to receive(:execute_ddl).once.with(*sql)
|
||||
|
|
|
@ -1,18 +1,13 @@
|
|||
require 'yaml'
|
||||
|
||||
class DatabaseHelper < Struct.new(:config, :db)
|
||||
# require 'logger'
|
||||
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
||||
|
||||
def setup
|
||||
create_db
|
||||
establish_connection
|
||||
load_schema
|
||||
end
|
||||
|
||||
def connection
|
||||
raise NotImplementedError
|
||||
end
|
||||
attr_reader :connection
|
||||
|
||||
def teardown
|
||||
drop_db
|
||||
|
@ -28,11 +23,8 @@ class DatabaseHelper < Struct.new(:config, :db)
|
|||
if db == :sqlite3
|
||||
# NO-OP
|
||||
elsif db == :postgres
|
||||
begin
|
||||
establish_connection default_config.merge('database' => 'postgres')
|
||||
connection.execute "CREATE DATABASE #{default_config['database']}"
|
||||
rescue ActiveRecord::StatementInvalid
|
||||
end
|
||||
establish_connection default_config.merge('database' => 'postgres')
|
||||
connection.execute "CREATE DATABASE #{default_config['database']}" rescue nil
|
||||
else
|
||||
establish_connection default_config.merge("database" => nil)
|
||||
connection.execute "CREATE DATABASE IF NOT EXISTS #{default_config['database']}"
|
||||
|
|
|
@ -1,61 +1,12 @@
|
|||
require 'sequel'
|
||||
require 'support/database_helper'
|
||||
|
||||
def db_config
|
||||
SequelHelper.new.send(:db_config)
|
||||
end
|
||||
|
||||
class SequelHelper < DatabaseHelper
|
||||
attr_reader :connection
|
||||
|
||||
private
|
||||
|
||||
def establish_connection
|
||||
@connection = ::Sequel.connect(config[:url], config[:connection_options])
|
||||
end
|
||||
|
||||
def create_db
|
||||
if config[:url] == "sqlite:///"
|
||||
# NO-OP
|
||||
elsif config[:url] == "postgres:///"
|
||||
::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 drop_db
|
||||
if config[:url] == "sqlite:///"
|
||||
begin
|
||||
File.unlink(db_config['sqlite3']['database'])
|
||||
rescue Errno::ENOENT
|
||||
end
|
||||
elsif config[:url] == "postgres:///"
|
||||
::Sequel.connect(config[:url], config[:connection_options]) do |db|
|
||||
db.execute "DROP TABLE IF EXISTS users"
|
||||
db.execute "DROP TABLE IF EXISTS agents"
|
||||
end
|
||||
else
|
||||
::Sequel.connect(config[:url], config[:connection_options].merge('database' => nil)) do |db|
|
||||
db.execute "DROP DATABASE IF EXISTS #{database}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def load_schema
|
||||
connection.create_table!(:users) { primary_key :id }
|
||||
connection.create_table!(:agents) { primary_key :id }
|
||||
end
|
||||
|
||||
def database
|
||||
config[:connection_options]['database']
|
||||
def establish_connection(config = default_config)
|
||||
url = "#{db}:///"
|
||||
url = "sqlite:///" if db == :sqlite3
|
||||
@connection = ::Sequel.connect(url, config)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue