2011-03-30 08:27:03 +00:00
|
|
|
require 'database_cleaner/sequel/truncation'
|
2012-07-08 10:51:27 +00:00
|
|
|
require 'database_cleaner/shared_strategy'
|
2018-05-22 14:42:17 +00:00
|
|
|
require 'support/sequel_helper'
|
2013-10-02 04:34:51 +00:00
|
|
|
|
2011-03-30 08:27:03 +00:00
|
|
|
module DatabaseCleaner
|
|
|
|
module Sequel
|
2018-05-01 02:31:44 +00:00
|
|
|
RSpec.describe Truncation do
|
2011-03-30 08:27:03 +00:00
|
|
|
it_should_behave_like "a generic strategy"
|
|
|
|
it_should_behave_like "a generic truncation strategy"
|
2013-10-02 04:34:51 +00:00
|
|
|
|
2018-05-22 14:33:24 +00:00
|
|
|
%w[mysql mysql2 sqlite3 postgres].map(&:to_sym).each do |db|
|
|
|
|
context "using a #{db} connection" do
|
2018-05-22 14:36:09 +00:00
|
|
|
let(:helper) { SequelHelper.new(db) }
|
2018-05-22 13:02:13 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
around do |example|
|
|
|
|
helper.setup
|
|
|
|
example.run
|
|
|
|
helper.teardown
|
2013-10-02 04:34:51 +00:00
|
|
|
end
|
|
|
|
|
2018-05-22 14:33:24 +00:00
|
|
|
let(:connection) { helper.connection }
|
2018-04-26 21:14:28 +00:00
|
|
|
|
2018-05-22 14:33:24 +00:00
|
|
|
before { subject.db = connection }
|
2013-10-02 04:34:51 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
context 'when several tables have data' do
|
|
|
|
before do
|
2018-05-22 14:33:24 +00:00
|
|
|
connection[:users].insert
|
|
|
|
connection[:agents].insert
|
2018-05-19 16:53:51 +00:00
|
|
|
end
|
2018-04-26 21:14:28 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
context 'by default' do
|
|
|
|
it 'truncates all the tables' do
|
|
|
|
subject.clean
|
2014-11-21 13:42:25 +00:00
|
|
|
|
2018-05-22 14:33:24 +00:00
|
|
|
expect(connection[:users]).to be_empty
|
|
|
|
expect(connection[:agents]).to be_empty
|
2018-05-19 16:53:51 +00:00
|
|
|
end
|
|
|
|
end
|
2014-11-21 13:42:25 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
context 'restricted to "only: [...]" some tables' do
|
2018-05-22 13:11:07 +00:00
|
|
|
subject { described_class.new(only: ['users']) }
|
2014-11-21 13:42:25 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
it 'truncates only the mentioned tables (and leaves the rest alone)' do
|
|
|
|
subject.clean
|
2014-11-21 13:42:25 +00:00
|
|
|
|
2018-05-22 14:33:24 +00:00
|
|
|
expect(connection[:users]).to be_empty
|
|
|
|
expect(connection[:agents].count).to eq(1)
|
2018-05-19 16:53:51 +00:00
|
|
|
end
|
|
|
|
end
|
2014-11-21 13:42:25 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
context 'restricted to "except: [...]" some tables' do
|
2018-05-22 13:11:07 +00:00
|
|
|
subject { described_class.new(except: ['users']) } # XXX: Strings only, symbols are ignored
|
2014-11-21 13:42:25 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
it 'leaves the mentioned tables alone (and truncates the rest)' do
|
|
|
|
subject.clean
|
|
|
|
|
2018-05-22 14:33:24 +00:00
|
|
|
expect(connection[:users].count).to eq(1)
|
|
|
|
expect(connection[:agents]).to be_empty
|
2018-05-19 16:53:51 +00:00
|
|
|
end
|
|
|
|
end
|
2014-11-21 13:42:25 +00:00
|
|
|
end
|
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
describe 'auto increment sequences' do
|
|
|
|
it "resets AUTO_INCREMENT primary key seqeunce" do
|
2018-05-22 14:33:24 +00:00
|
|
|
table = connection[:users]
|
2018-05-19 16:53:51 +00:00
|
|
|
2.times { table.insert }
|
2014-11-21 13:42:25 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
subject.clean
|
2014-11-21 13:42:25 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
id_after_clean = table.insert
|
|
|
|
expect(id_after_clean).to eq 1
|
|
|
|
end
|
2014-11-21 13:42:25 +00:00
|
|
|
end
|
2018-04-26 21:14:28 +00:00
|
|
|
|
2018-05-19 16:53:51 +00:00
|
|
|
describe "with pre_count optimization option" do
|
|
|
|
subject { described_class.new(pre_count: true) }
|
|
|
|
|
2018-05-22 14:33:24 +00:00
|
|
|
before { connection[:users].insert }
|
2018-05-19 16:53:51 +00:00
|
|
|
|
|
|
|
it "only truncates non-empty tables" do
|
2018-05-22 14:33:24 +00:00
|
|
|
sql = case helper.db
|
|
|
|
when :sqlite3 then ["DELETE FROM `users`", anything]
|
|
|
|
when :postgres then ['TRUNCATE TABLE "users" RESTART IDENTITY;', anything]
|
2018-05-22 13:11:07 +00:00
|
|
|
else ["TRUNCATE TABLE `users`", anything]
|
2018-05-19 16:53:51 +00:00
|
|
|
end
|
|
|
|
expect(subject.db).to receive(:execute_ddl).once.with(*sql)
|
|
|
|
subject.clean
|
|
|
|
end
|
2018-04-25 17:55:31 +00:00
|
|
|
end
|
2018-04-26 21:14:28 +00:00
|
|
|
end
|
2013-10-02 04:34:51 +00:00
|
|
|
end
|
|
|
|
end
|
2011-03-30 08:27:03 +00:00
|
|
|
end
|
|
|
|
end
|