Add basic specs

This commit is contained in:
Timothée Peignier 2014-12-21 22:09:04 -08:00
parent 0bedcb7d87
commit 64dbc87bb8
2 changed files with 72 additions and 9 deletions

View file

@ -4,37 +4,42 @@ require 'database_cleaner/sequel/truncation'
module DatabaseCleaner::Sequel
class Deletion < Truncation
def disable_referential_integrity
def disable_referential_integrity(tables)
case db.database_type
when :postgres
db.run('SET CONSTRAINTS ALL DEFERRED')
tables_to_truncate(db).each do |table|
db.run("ALTER TABLE #{::Sequel.lit(table)} DISABLE TRIGGER ALL")
db.run("ALTER TABLE \"#{table}\" DISABLE TRIGGER ALL")
end
when :mysql
old = db.fetch('SELECT @@FOREIGN_KEY_CHECKS').first['@@FOREIGN_KEY_CHECKS']
old = db.fetch('SELECT @@FOREIGN_KEY_CHECKS').first[:@@FOREIGN_KEY_CHECKS]
db.run('SET FOREIGN_KEY_CHECKS = 0')
end
yield
ensure
case db.database_type
when :postgres
tables_to_truncate(db).each do |table|
db.run("ALTER TABLE #{::Sequel.lit(table)} ENABLE TRIGGER ALL")
tables.each do |table|
db.run("ALTER TABLE \"#{table}\" ENABLE TRIGGER ALL")
end
when :mysql
db.run("SET FOREIGN_KEY_CHECKS = #{old}")
end
end
def delete_tables(db, tables)
tables.each do |table|
db[table.to_sym].delete
end
end
def clean
return unless dirty?
tables = tables_to_truncate(db)
db.transaction do
disable_referential_integrity do
each_table do |db, table|
db[table].delete
end
disable_referential_integrity(tables) do
delete_tables(db, tables)
end
end
end

View file

@ -0,0 +1,58 @@
require 'spec_helper'
require 'database_cleaner/sequel/deletion'
require 'database_cleaner/shared_strategy'
require 'sequel'
require 'support/active_record/database_setup'
module DatabaseCleaner
module Sequel
describe Deletion do
it_should_behave_like "a generic strategy"
end
shared_examples 'a Sequel deletion strategy' do
let(:deletion) do
d = Deletion.new
d.db = db
d
end
context 'when several tables have data' do
before(:each) 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 }
db[:precious_stones].insert
db[:replaceable_trifles].insert
db[:worthless_junk].insert
end
context 'by default' do
it 'deletes all the tables' do
d = Deletion.new
d.db = db
d.clean
expect(db[:replaceable_trifles]).to have(0).rows
expect(db[:worthless_junk]).to have(0).rows
expect(db[:precious_stones]).to have(0).rows
end
end
end
end
supported_configurations = [
{ url: 'mysql:///', connection_options: db_config['mysql'] },
{ 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]) }
it_behaves_like 'a Sequel deletion strategy'
end
end
end
end