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'
|
2011-03-30 08:27:03 +00:00
|
|
|
require 'sequel'
|
2018-04-26 21:14:28 +00:00
|
|
|
require 'support/sequel/sequel_setup'
|
2013-10-02 04:34:51 +00:00
|
|
|
# XXX: use ActiveRecord's db_config (`db/config.yml`) for CI/dev convenience
|
|
|
|
require 'support/active_record/database_setup'
|
|
|
|
|
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"
|
|
|
|
end
|
2013-10-02 04:34:51 +00:00
|
|
|
|
2018-05-01 02:31:44 +00:00
|
|
|
RSpec.shared_examples 'a Sequel truncation strategy' do
|
2013-10-02 04:34:51 +00:00
|
|
|
|
|
|
|
# XXX: it'd be really nice if Truncation accepted db: constructor parameter
|
|
|
|
let(:truncation) do
|
|
|
|
t = Truncation.new
|
|
|
|
t.db = db
|
|
|
|
t
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when several tables have data' do
|
2018-04-26 21:14:28 +00:00
|
|
|
before do
|
2013-10-02 04:34:51 +00:00
|
|
|
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 'truncates all the tables' do
|
|
|
|
t = Truncation.new
|
|
|
|
t.db = db
|
|
|
|
t.clean
|
|
|
|
|
2018-04-26 16:29:13 +00:00
|
|
|
expect(db[:replaceable_trifles]).to be_empty
|
|
|
|
expect(db[:worthless_junk]).to be_empty
|
|
|
|
expect(db[:precious_stones]).to be_empty
|
2013-10-02 04:34:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
context 'when the Truncation is restricted to "only: [...]" some tables' do
|
|
|
|
it 'truncates only the mentioned tables (and leaves the rest alone)' do
|
|
|
|
t = Truncation.new(only: ['worthless_junk', 'replaceable_trifles'])
|
|
|
|
t.db = db
|
|
|
|
t.clean
|
|
|
|
|
2018-04-26 16:29:13 +00:00
|
|
|
expect(db[:replaceable_trifles]).to be_empty
|
|
|
|
expect(db[:worthless_junk]).to be_empty
|
|
|
|
expect(db[:precious_stones].count).to eq(1)
|
2013-10-02 04:34:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
context 'when the Truncation is restricted to "except: [...]" some tables' do
|
|
|
|
it 'leaves the mentioned tables alone (and truncates the rest)' do
|
|
|
|
t = Truncation.new(except: ['precious_stones']) # XXX: Strings only, symbols are ignored
|
|
|
|
t.db = db
|
|
|
|
t.clean
|
|
|
|
|
|
|
|
expect(db[:replaceable_trifles]).to be_empty
|
|
|
|
expect(db[:worthless_junk]).to be_empty
|
2018-04-26 16:29:13 +00:00
|
|
|
expect(db[:precious_stones].count).to eq(1)
|
2013-10-02 04:34:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-01 02:31:44 +00:00
|
|
|
RSpec.shared_examples_for 'a truncation strategy without autoincrement resets' do
|
2013-10-02 04:34:51 +00:00
|
|
|
it "leaves AUTO_INCREMENT index alone by default (BUG: it should be reset instead)" do
|
2018-04-26 16:29:13 +00:00
|
|
|
pending
|
2013-10-02 04:34:51 +00:00
|
|
|
# Jordan Hollinger made everything reset auto increment IDs
|
|
|
|
# in commit 6a0104382647e5c06578aeac586c0333c8944492 so I'm pretty sure
|
|
|
|
# everything is meant to reset by default.
|
|
|
|
#
|
|
|
|
# For Postgres, db[:mytable].truncate(restart: true) should work.
|
|
|
|
# For SQLite, db[:sqlite_sequence].where(name: 'mytable').delete
|
|
|
|
|
|
|
|
db.create_table!(:replaceable_trifles) { primary_key :id }
|
|
|
|
table = db[:replaceable_trifles]
|
|
|
|
2.times { table.insert }
|
|
|
|
|
|
|
|
truncation.clean
|
|
|
|
|
|
|
|
id_after_clean = table.insert
|
2018-04-26 16:29:13 +00:00
|
|
|
expect(id_after_clean).to eq 1
|
2013-10-02 04:34:51 +00:00
|
|
|
end
|
|
|
|
# XXX: it'd be really nice if Truncation accepted db: constructor parameter
|
|
|
|
let(:truncation) do
|
|
|
|
t = Truncation.new
|
|
|
|
t.db = db
|
|
|
|
t
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-01 02:31:44 +00:00
|
|
|
RSpec.shared_examples_for 'a truncation strategy that resets autoincrement keys by default' do
|
2013-10-02 04:34:51 +00:00
|
|
|
it "resets AUTO_INCREMENT primary keys" do
|
|
|
|
db.create_table!(:replaceable_trifles) { primary_key :id }
|
|
|
|
table = db[:replaceable_trifles]
|
|
|
|
2.times { table.insert }
|
|
|
|
|
|
|
|
truncation.clean
|
|
|
|
|
|
|
|
id_after_clean = table.insert
|
|
|
|
expect(id_after_clean).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# XXX: it'd be really nice if Truncation accepted db: constructor parameter
|
|
|
|
let(:truncation) do
|
|
|
|
t = Truncation.new
|
|
|
|
t.db = db
|
|
|
|
t
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
half_supported_configurations = [
|
|
|
|
{url: 'sqlite:///', connection_options: db_config['sqlite3']},
|
|
|
|
{url: 'postgres:///', connection_options: db_config['postgres']},
|
|
|
|
]
|
|
|
|
supported_configurations = [
|
|
|
|
{url: 'mysql:///', connection_options: db_config['mysql']},
|
|
|
|
{url: 'mysql2:///', connection_options: db_config['mysql2']}
|
|
|
|
]
|
2018-04-26 21:14:28 +00:00
|
|
|
|
2013-10-02 04:34:51 +00:00
|
|
|
supported_configurations.each do |config|
|
2018-05-01 02:31:44 +00:00
|
|
|
RSpec.describe "Sequel truncation (using a #{config[:url]} connection)" do
|
2018-04-26 21:14:28 +00:00
|
|
|
around do |example|
|
|
|
|
helper = SequelHelper.new(config)
|
|
|
|
helper.setup
|
|
|
|
|
|
|
|
example.run
|
|
|
|
|
|
|
|
helper.teardown
|
|
|
|
end
|
|
|
|
|
2013-10-02 04:34:51 +00:00
|
|
|
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'
|
2014-11-21 13:42:25 +00:00
|
|
|
|
2018-04-26 21:14:28 +00:00
|
|
|
|
2014-11-21 13:42:25 +00:00
|
|
|
describe '#pre_count?' do
|
|
|
|
subject { Truncation.new.tap { |t| t.db = db } }
|
|
|
|
|
2018-04-26 16:29:13 +00:00
|
|
|
it 'should return false initially' do
|
2018-04-26 17:57:18 +00:00
|
|
|
expect(subject.send(:pre_count?)).to eq false
|
2018-04-26 16:29:13 +00:00
|
|
|
end
|
2014-11-21 13:42:25 +00:00
|
|
|
|
|
|
|
it 'should return true if @reset_id is set and non false or nil' do
|
|
|
|
subject.instance_variable_set(:"@pre_count", true)
|
2018-04-26 17:57:18 +00:00
|
|
|
expect(subject.send(:pre_count?)).to eq true
|
2014-11-21 13:42:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return false if @reset_id is set to false' do
|
|
|
|
subject.instance_variable_set(:"@pre_count", false)
|
2018-04-26 17:57:18 +00:00
|
|
|
expect(subject.send(:pre_count?)).to eq false
|
2014-11-21 13:42:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "relying on #pre_count_truncate_tables if asked to" do
|
|
|
|
subject { Truncation.new.tap { |t| t.db = db } }
|
|
|
|
|
|
|
|
it "should rely on #pre_count_truncate_tables if #pre_count? returns true" do
|
|
|
|
subject.instance_variable_set(:"@pre_count", true)
|
|
|
|
|
2018-04-26 17:57:18 +00:00
|
|
|
expect(subject).not_to receive(:truncate_tables)
|
|
|
|
expect(subject).to receive(:pre_count_truncate_tables)
|
2014-11-21 13:42:25 +00:00
|
|
|
|
|
|
|
subject.clean
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not rely on #pre_count_truncate_tables if #pre_count? return false" do
|
|
|
|
subject.instance_variable_set(:"@pre_count", false)
|
|
|
|
|
2018-04-26 17:57:18 +00:00
|
|
|
expect(subject).not_to receive(:pre_count_truncate_tables)
|
|
|
|
expect(subject).to receive(:truncate_tables)
|
2014-11-21 13:42:25 +00:00
|
|
|
|
|
|
|
subject.clean
|
|
|
|
end
|
|
|
|
end
|
2013-10-02 04:34:51 +00:00
|
|
|
end
|
|
|
|
end
|
2018-04-26 21:14:28 +00:00
|
|
|
|
2013-10-02 04:34:51 +00:00
|
|
|
half_supported_configurations.each do |config|
|
2018-05-01 02:31:44 +00:00
|
|
|
RSpec.describe "Sequel truncation (using a #{config[:url]} connection)" do
|
2018-04-26 21:14:28 +00:00
|
|
|
around do |example|
|
|
|
|
helper = SequelHelper.new(config)
|
|
|
|
helper.setup
|
|
|
|
|
|
|
|
example.run
|
|
|
|
|
|
|
|
helper.teardown
|
|
|
|
end
|
|
|
|
|
2013-10-02 04:34:51 +00:00
|
|
|
let(:db) { ::Sequel.connect(config[:url], config[:connection_options]) }
|
|
|
|
|
|
|
|
it_behaves_like 'a Sequel truncation strategy'
|
|
|
|
it_behaves_like 'a truncation strategy without autoincrement resets'
|
|
|
|
end
|
|
|
|
end
|
2011-03-30 08:27:03 +00:00
|
|
|
end
|
|
|
|
end
|