From f68782abec00a53b4595e695ccf1f05b85e20c50 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Tue, 22 May 2018 08:09:42 -0700 Subject: [PATCH] extract DatabaseHelper.with_all_dbs. --- .../connection_adapter_monkeypatches_spec.rb | 17 ++++++----------- spec/database_cleaner/sequel/deletion_spec.rb | 6 ++---- spec/database_cleaner/sequel/truncation_spec.rb | 6 ++---- spec/support/database_helper.rb | 6 ++++++ 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/spec/database_cleaner/active_record/truncation/connection_adapter_monkeypatches_spec.rb b/spec/database_cleaner/active_record/truncation/connection_adapter_monkeypatches_spec.rb index a3c6daa..d863d4e 100644 --- a/spec/database_cleaner/active_record/truncation/connection_adapter_monkeypatches_spec.rb +++ b/spec/database_cleaner/active_record/truncation/connection_adapter_monkeypatches_spec.rb @@ -1,20 +1,17 @@ require 'support/active_record_helper' require 'database_cleaner/active_record/truncation' -require 'database_cleaner/active_record/truncation/shared_fast_truncation' RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do - %w[mysql mysql2 sqlite3 postgres].map(&:to_sym).each do |db| - context "using a #{db} connection" do - let(:helper) { ActiveRecordHelper.new(db) } - - let(:connection) { helper.connection } - + ActiveRecordHelper.with_all_dbs do |helper| + context "using a #{helper.db} connection" do around do |example| helper.setup example.run helper.teardown end + let(:connection) { helper.connection } + describe "AR connection adapter monkeypatches" do describe "#truncate_table" do it "truncates the table" do @@ -43,7 +40,7 @@ RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do end end - unless db == :sqlite3 + unless helper.db == :sqlite3 describe "#pre_count_truncate_tables" do context "with :reset_ids set true" do @@ -84,7 +81,7 @@ RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do end end - if db == :postgres + if helper.db == :postgres describe ":except option cleanup" do it "should not truncate the tables specified in the :except option" do 2.times { User.create } @@ -101,8 +98,6 @@ RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do end end - it_behaves_like "an adapter with pre-count truncation" - describe "schema_migrations table" do it "is not truncated" do diff --git a/spec/database_cleaner/sequel/deletion_spec.rb b/spec/database_cleaner/sequel/deletion_spec.rb index 1c02b04..43c3d8d 100644 --- a/spec/database_cleaner/sequel/deletion_spec.rb +++ b/spec/database_cleaner/sequel/deletion_spec.rb @@ -7,10 +7,8 @@ module DatabaseCleaner RSpec.describe Deletion do it_should_behave_like "a generic strategy" - %w[mysql mysql2 sqlite3 postgres].map(&:to_sym).each do |db| - context "using a #{db} connection" do - let(:helper) { SequelHelper.new(db) } - + SequelHelper.with_all_dbs do |helper| + context "using a #{helper.db} connection" do around do |example| helper.setup example.run diff --git a/spec/database_cleaner/sequel/truncation_spec.rb b/spec/database_cleaner/sequel/truncation_spec.rb index f137876..c3278b4 100644 --- a/spec/database_cleaner/sequel/truncation_spec.rb +++ b/spec/database_cleaner/sequel/truncation_spec.rb @@ -8,10 +8,8 @@ module DatabaseCleaner it_should_behave_like "a generic strategy" it_should_behave_like "a generic truncation strategy" - %w[mysql mysql2 sqlite3 postgres].map(&:to_sym).each do |db| - context "using a #{db} connection" do - let(:helper) { SequelHelper.new(db) } - + SequelHelper.with_all_dbs do |helper| + context "using a #{helper.db} connection" do around do |example| helper.setup example.run diff --git a/spec/support/database_helper.rb b/spec/support/database_helper.rb index 764b748..2f8c909 100644 --- a/spec/support/database_helper.rb +++ b/spec/support/database_helper.rb @@ -1,6 +1,12 @@ require 'yaml' class DatabaseHelper < Struct.new(:db) + def self.with_all_dbs &block + %w[mysql mysql2 sqlite3 postgres].map(&:to_sym).each do |db| + yield new(db) + end + end + def setup create_db establish_connection