From 467ab08e3533ae28692d0d7a64ded41aa934f909 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 09:29:13 -0700 Subject: [PATCH 01/14] upgrade to rspec 3. --- .rspec | 2 - Gemfile.lock | 94 +++---------------- Guardfile | 9 -- database_cleaner.gemspec | 3 +- .../active_record/base_spec.rb | 3 +- .../active_record/transaction_spec.rb | 4 +- .../active_record/truncation_spec.rb | 12 ++- spec/database_cleaner/base_spec.rb | 4 +- .../generic/truncation_spec.rb | 21 ++--- spec/database_cleaner/sequel/base_spec.rb | 3 +- spec/database_cleaner/sequel/deletion_spec.rb | 6 +- .../sequel/truncation_spec.rb | 23 ++--- spec/spec_helper.rb | 19 ++++ 13 files changed, 76 insertions(+), 127 deletions(-) delete mode 100644 Guardfile diff --git a/.rspec b/.rspec index 660ad19..16f9cdb 100644 --- a/.rspec +++ b/.rspec @@ -1,4 +1,2 @@ --color --format documentation -mtime ---backtrace diff --git a/Gemfile.lock b/Gemfile.lock index f57bdb6..97b30f7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,17 +6,6 @@ PATH GEM remote: https://rubygems.org/ specs: - abstract (1.0.0) - actionpack (3.0.0) - activemodel (= 3.0.0) - activesupport (= 3.0.0) - builder (~> 2.1.2) - erubis (~> 2.6.6) - i18n (~> 0.4.1) - rack (~> 1.2.1) - rack-mount (~> 0.6.12) - rack-test (~> 0.5.4) - tzinfo (~> 0.3.23) activemodel (3.0.0) activesupport (= 3.0.0) builder (~> 2.1.2) @@ -43,7 +32,6 @@ GEM bson_ext (1.12.5) bson (~> 1.12.5) builder (2.1.2) - coderay (1.1.0) couch_potato (1.3.0) activemodel couchrest (~> 1.2.0) @@ -69,7 +57,7 @@ GEM dm-transactions (~> 1.2.0) dm-types (~> 1.2.0) dm-validations (~> 1.2.0) - diff-lcs (1.2.5) + diff-lcs (1.3) dm-aggregates (1.2.0) dm-core (~> 1.2.0) dm-constraints (1.2.0) @@ -112,29 +100,15 @@ GEM data_objects (= 0.10.14) do_jdbc (= 0.10.14) jdbc-sqlite3 (>= 3.5.8) - erubis (2.6.6) - abstract (>= 1.0.0) faraday (0.9.0) multipart-post (>= 1.2, < 3) faraday_middleware (0.9.1) faraday (>= 0.7.4, < 0.10) fastercsv (1.5.5) - ffi (1.9.6) - ffi (1.9.6-java) - formatador (0.2.5) gherkin (2.11.6) json (>= 1.7.6) gherkin (2.11.6-java) json (>= 1.7.6) - guard (1.8.3) - formatador (>= 0.2.4) - listen (~> 1.3) - lumberjack (>= 1.0.2) - pry (>= 0.9.10) - thor (>= 0.14.6) - guard-rspec (2.5.4) - guard (>= 1.1) - rspec (~> 2.11) httparty (0.13.3) json (~> 1.8) multi_xml (>= 0.5.2) @@ -144,12 +118,6 @@ GEM json (1.8.6) json (1.8.6-java) json_pure (1.8.1) - listen (1.3.1) - rb-fsevent (>= 0.9.3) - rb-inotify (>= 0.9) - rb-kqueue (>= 0.2) - lumberjack (1.0.9) - method_source (0.8.2) mime-types (1.25.1) mongo (1.12.5) bson (= 1.12.5) @@ -191,60 +159,29 @@ GEM pg (0.18.2) plucky (0.5.2) mongo (~> 1.5) - pry (0.10.1) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - pry (0.10.1-java) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - spoon (~> 0.0) - rack (1.2.8) - rack-mount (0.6.14) - rack (>= 1.0.0) - rack-test (0.5.7) - rack (>= 1.0) - railties (3.0.0) - actionpack (= 3.0.0) - activesupport (= 3.0.0) - rake (>= 0.8.4) - thor (~> 0.14.0) rake (11.1.2) - rb-fsevent (0.9.4) - rb-inotify (0.9.5) - ffi (>= 0.5.0) - rb-kqueue (0.2.3) - ffi (>= 0.5.0) rdoc (4.1.2) json (~> 1.4) redis (3.1.0) rest-client (1.6.8) mime-types (~> 1.16) rdoc (>= 2.4.2) - rspec (2.14.1) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) - rspec-core (2.14.8) - rspec-expectations (2.14.5) - diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.14.6) - rspec-rails (2.14.2) - actionpack (>= 3.0) - activemodel (>= 3.0) - activesupport (>= 3.0) - railties (>= 3.0) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) + rspec (3.7.0) + rspec-core (~> 3.7.0) + rspec-expectations (~> 3.7.0) + rspec-mocks (~> 3.7.0) + rspec-core (3.7.1) + rspec-support (~> 3.7.0) + rspec-expectations (3.7.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.7.0) + rspec-mocks (3.7.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.7.0) + rspec-support (3.7.1) sequel (3.21.0) - slop (3.6.0) - spoon (0.0.4) - ffi sqlite3 (1.3.10) stringex (1.5.1) - thor (0.14.6) tzinfo (0.3.48) uuidtools (2.1.5) zip (2.0.2) @@ -264,7 +201,6 @@ DEPENDENCIES datamapper dm-migrations dm-sqlite-adapter - guard-rspec json_pure mongo (~> 1.12.0) mongo_ext @@ -278,7 +214,7 @@ DEPENDENCIES ohm (~> 0.1.3) pg rake - rspec-rails (~> 2.14.2) + rspec sequel (~> 3.21.0) sqlite3 tzinfo diff --git a/Guardfile b/Guardfile deleted file mode 100644 index 767287d..0000000 --- a/Guardfile +++ /dev/null @@ -1,9 +0,0 @@ -# A sample Guardfile -# More info at https://github.com/guard/guard#readme - -guard 'rspec', :version => 2 do - watch(%r{^spec/.+_spec\.rb$}) - watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } - watch('spec/spec_helper.rb') { "spec" } -end - diff --git a/database_cleaner.gemspec b/database_cleaner.gemspec index 831af16..90cd42b 100644 --- a/database_cleaner.gemspec +++ b/database_cleaner.gemspec @@ -51,8 +51,7 @@ Gem::Specification.new do |s| s.add_development_dependency "couch_potato" s.add_development_dependency "sequel", "~> 3.21.0" s.add_development_dependency 'ohm', '~> 0.1.3' - s.add_development_dependency 'guard-rspec' - s.add_development_dependency "rspec-rails", "~> 2.14.2" + s.add_development_dependency "rspec" s.add_development_dependency "cucumber" unless RUBY_PLATFORM =~ /java/ diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index e6c1588..9692691 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -172,8 +172,7 @@ my_db: end context "when connection_hash is set" do - let(:hash) { double("hash") } - before { ::ActiveRecord::Base.stub(:respond_to?).and_return(false) } + let(:hash) { {} } before { subject.stub(:connection_hash).and_return(hash) } it "establish a connection using ActiveRecord::Base" do diff --git a/spec/database_cleaner/active_record/transaction_spec.rb b/spec/database_cleaner/active_record/transaction_spec.rb index 8e3515d..6a4002e 100644 --- a/spec/database_cleaner/active_record/transaction_spec.rb +++ b/spec/database_cleaner/active_record/transaction_spec.rb @@ -163,11 +163,11 @@ module DatabaseCleaner describe "#connection_maintains_transaction_count?" do it "should return true if the major active record version is < 4" do stub_const("ActiveRecord::VERSION::MAJOR", 3) - Transaction.new.connection_maintains_transaction_count?.should be_true + Transaction.new.connection_maintains_transaction_count?.should be_truthy end it "should return false if the major active record version is > 3" do stub_const("ActiveRecord::VERSION::MAJOR", 4) - Transaction.new.connection_maintains_transaction_count?.should be_false + Transaction.new.connection_maintains_transaction_count?.should be_falsey end end diff --git a/spec/database_cleaner/active_record/truncation_spec.rb b/spec/database_cleaner/active_record/truncation_spec.rb index 2ff665f..22b35e0 100644 --- a/spec/database_cleaner/active_record/truncation_spec.rb +++ b/spec/database_cleaner/active_record/truncation_spec.rb @@ -142,7 +142,10 @@ module DatabaseCleaner end subject { Truncation.new } - its(:pre_count?) { should eq false } + + it 'should return false initially' do + subject.send(:pre_count?).should eq false + end it 'should return true if @reset_id is set and non false or nil' do subject.instance_variable_set(:"@pre_count", true) @@ -163,9 +166,12 @@ module DatabaseCleaner end subject { Truncation.new } - its(:reset_ids?) { should eq true } - it 'should return true if @reset_id is set and non false or nil' do + it 'should return true initially' do + subject.send(:reset_ids?).should eq true + end + + it 'should return true if @reset_id is set and non falsey' do subject.instance_variable_set(:"@reset_ids", 'Something') subject.send(:reset_ids?).should eq true end diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index d422b42..d077ed1 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -508,12 +508,12 @@ module DatabaseCleaner describe "auto_detected?" do it "should return true unless @autodetected is nil" do subject.instance_variable_set("@autodetected","not nil") - subject.auto_detected?.should be_true + subject.auto_detected?.should be_truthy end it "should return false if @autodetect is nil" do subject.instance_variable_set("@autodetected",nil) - subject.auto_detected?.should be_false + subject.auto_detected?.should be_falsey end end diff --git a/spec/database_cleaner/generic/truncation_spec.rb b/spec/database_cleaner/generic/truncation_spec.rb index ad9ad40..c5d995d 100644 --- a/spec/database_cleaner/generic/truncation_spec.rb +++ b/spec/database_cleaner/generic/truncation_spec.rb @@ -48,7 +48,6 @@ module ::DatabaseCleaner end it { should_not respond_to(:migration_storage_names) } - its(:migration_storage_names) { should be_empty } end describe "initialize" do @@ -67,40 +66,40 @@ module ::DatabaseCleaner context "" do subject { TruncationExample.new( { :only => ["something"] } ) } - its(:only) { should eq ["something"] } - its(:except) { should eq [] } + it { subject.only.should eq ["something"] } + it { subject.except.should eq [] } end context "" do subject { TruncationExample.new( { :except => ["something"] } ) } - its(:only) { should eq nil } - its(:except) { should include("something") } + it { subject.only.should eq nil } + it { subject.except.should include("something") } end context "" do subject { TruncationExample.new( { :reset_ids => ["something"] } ) } - its(:reset_ids?) { should eq true } + it { subject.reset_ids?.should eq true } end context "" do subject { TruncationExample.new( { :reset_ids => nil } ) } - its(:reset_ids?) { should eq false } + it { subject.reset_ids?.should eq false } end context "" do subject { TruncationExample.new( { :pre_count => ["something"] } ) } - its(:pre_count?) { should eq true } + it { subject.pre_count?.should eq true } end context "" do subject { TruncationExample.new( { :pre_count => nil } ) } - its(:pre_count?) { should eq false } + it { subject.pre_count?.should eq false } end context "" do subject { MigrationExample.new } - its(:only) { should eq nil } - its(:except) { should eq %w[migration_storage_name] } + it { subject.only.should eq nil } + it { subject.except.should eq %w[migration_storage_name] } end context "" do diff --git a/spec/database_cleaner/sequel/base_spec.rb b/spec/database_cleaner/sequel/base_spec.rb index 956afa8..f912d71 100644 --- a/spec/database_cleaner/sequel/base_spec.rb +++ b/spec/database_cleaner/sequel/base_spec.rb @@ -24,9 +24,10 @@ module DatabaseCleaner end it "should default to :default" do - pending "I figure out how to use Sequel and write some real tests for it..." subject.db.should eq :default end + + pending "I figure out how to use Sequel and write some real tests for it..." end end end diff --git a/spec/database_cleaner/sequel/deletion_spec.rb b/spec/database_cleaner/sequel/deletion_spec.rb index ff6c565..27f9f7c 100644 --- a/spec/database_cleaner/sequel/deletion_spec.rb +++ b/spec/database_cleaner/sequel/deletion_spec.rb @@ -34,9 +34,9 @@ module DatabaseCleaner 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 + expect(db[:replaceable_trifles]).to be_empty + expect(db[:worthless_junk]).to be_empty + expect(db[:precious_stones]).to be_empty end end end diff --git a/spec/database_cleaner/sequel/truncation_spec.rb b/spec/database_cleaner/sequel/truncation_spec.rb index 003201f..e4cc075 100644 --- a/spec/database_cleaner/sequel/truncation_spec.rb +++ b/spec/database_cleaner/sequel/truncation_spec.rb @@ -38,9 +38,9 @@ module DatabaseCleaner t.db = db t.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 + expect(db[:replaceable_trifles]).to be_empty + expect(db[:worthless_junk]).to be_empty + expect(db[:precious_stones]).to be_empty end end context 'when the Truncation is restricted to "only: [...]" some tables' do @@ -49,9 +49,9 @@ module DatabaseCleaner t.db = db t.clean - expect(db[:replaceable_trifles]).to have(0).rows - expect(db[:worthless_junk]).to have(0).rows - expect(db[:precious_stones]).to have(1).rows + expect(db[:replaceable_trifles]).to be_empty + expect(db[:worthless_junk]).to be_empty + expect(db[:precious_stones].count).to eq(1) end end context 'when the Truncation is restricted to "except: [...]" some tables' do @@ -62,7 +62,7 @@ module DatabaseCleaner expect(db[:replaceable_trifles]).to be_empty expect(db[:worthless_junk]).to be_empty - expect(db[:precious_stones]).to have(1).item + expect(db[:precious_stones].count).to eq(1) end end end @@ -70,6 +70,7 @@ module DatabaseCleaner shared_examples_for 'a truncation strategy without autoincrement resets' do it "leaves AUTO_INCREMENT index alone by default (BUG: it should be reset instead)" do + pending # Jordan Hollinger made everything reset auto increment IDs # in commit 6a0104382647e5c06578aeac586c0333c8944492 so I'm pretty sure # everything is meant to reset by default. @@ -84,9 +85,7 @@ module DatabaseCleaner truncation.clean id_after_clean = table.insert - pending('the bug being fixed') do - expect(id_after_clean).to eq 1 - end + expect(id_after_clean).to eq 1 end # XXX: it'd be really nice if Truncation accepted db: constructor parameter let(:truncation) do @@ -134,7 +133,9 @@ module DatabaseCleaner describe '#pre_count?' do subject { Truncation.new.tap { |t| t.db = db } } - its(:pre_count?) { should eq false } + it 'should return false initially' do + subject.send(:pre_count?).should eq false + end it 'should return true if @reset_id is set and non false or nil' do subject.instance_variable_set(:"@pre_count", true) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 80d0a99..0d64961 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,7 +15,26 @@ $:.unshift(File.dirname(__FILE__) + '/../lib') require 'database_cleaner' RSpec.configure do |config| + # These two settings work together to allow you to limit a spec run + # to individual examples or groups you care about by tagging them with + # `:focus` metadata. When nothing is tagged with `:focus`, all examples + # get run. + config.filter_run :focus + config.run_all_when_everything_filtered = true + config.expect_with :rspec do |expectations| + # Enable only the newer, non-monkey-patching expect syntax. + # For more details, see: + # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax + expectations.syntax = [:should, :expect] + end + + config.mock_with :rspec do |mocks| + # Enable only the newer, non-monkey-patching expect syntax. + # For more details, see: + # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + mocks.syntax = [:should, :expect] + end end alias running lambda From ae234c4e1a18fc639a21a1871376b147b5212aff Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 09:59:07 -0700 Subject: [PATCH 02/14] prevent specs from leaking global state modifications. --- lib/database_cleaner/active_record/base.rb | 2 +- .../active_record/base_spec.rb | 1 + .../active_record/transaction_spec.rb | 21 ------------------- .../active_record/truncation/mysql2_spec.rb | 6 +++--- .../active_record/truncation/mysql_spec.rb | 6 +++--- .../truncation/postgresql_spec.rb | 10 ++++----- .../active_record/truncation/sqlite3_spec.rb | 4 ++-- spec/database_cleaner/base_spec.rb | 6 ------ .../data_mapper/truncation/sqlite3_spec.rb | 6 +++--- spec/spec_helper.rb | 6 ------ spec/support/active_record/mysql2_setup.rb | 5 ++--- spec/support/active_record/mysql_setup.rb | 5 ++--- .../support/active_record/postgresql_setup.rb | 5 ++--- spec/support/active_record/sqlite3_setup.rb | 5 ++--- spec/support/data_mapper/sqlite3_setup.rb | 4 +--- 15 files changed, 27 insertions(+), 65 deletions(-) diff --git a/lib/database_cleaner/active_record/base.rb b/lib/database_cleaner/active_record/base.rb index 0c7d439..434ed55 100644 --- a/lib/database_cleaner/active_record/base.rb +++ b/lib/database_cleaner/active_record/base.rb @@ -79,7 +79,7 @@ module DatabaseCleaner if ::ActiveRecord::Base.respond_to?(:descendants) database_name = connection_hash["database"] || connection_hash[:database] models = ::ActiveRecord::Base.descendants - models.detect { |m| m.connection_pool.spec.config[:database] == database_name } + models.select(&:connection_pool).detect { |m| m.connection_pool.spec.config[:database] == database_name } end end diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index 9692691..a0829e9 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' require 'active_record' require 'database_cleaner/active_record/base' require 'database_cleaner/shared_strategy' +require 'support/active_record/schema_setup' class FakeModel def self.connection diff --git a/spec/database_cleaner/active_record/transaction_spec.rb b/spec/database_cleaner/active_record/transaction_spec.rb index 6a4002e..c91e3bd 100644 --- a/spec/database_cleaner/active_record/transaction_spec.rb +++ b/spec/database_cleaner/active_record/transaction_spec.rb @@ -21,23 +21,19 @@ module DatabaseCleaner before do connection.stub(:transaction) connection.stub(begin_transaction_method) - connection.stub(:respond_to?).with(:begin_transaction).and_return(:begin_transaction == begin_transaction_method) end it "should increment open transactions if possible" do - connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true) connection.should_receive(:increment_open_transactions) Transaction.new.start end it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do - connection.stub(:respond_to?).with(:increment_open_transactions).and_return(false) ::ActiveRecord::Base.should_receive(:increment_open_transactions) Transaction.new.start end it "should start a transaction" do - connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true) connection.stub(:increment_open_transactions) connection.should_receive(begin_transaction_method) connection.should_receive(:transaction) @@ -61,9 +57,6 @@ module DatabaseCleaner it "should decrement open transactions if possible" do connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(true) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) connection.should_receive(:decrement_open_transactions) @@ -78,9 +71,6 @@ module DatabaseCleaner it "should decrement connection via ActiveRecord::Base if connection won't" do connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) ::ActiveRecord::Base.should_receive(:decrement_open_transactions) @@ -91,15 +81,9 @@ module DatabaseCleaner connection_pool.stub(:connections).and_return([connection, connection_2]) connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) connection_2.should_receive(:open_transactions).and_return(1) - connection_2.stub(:respond_to?).with(:decrement_open_transactions).and_return(false) - connection_2.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection_2.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection_2.stub(:rollback_db_transaction) ::ActiveRecord::Base.should_receive(:decrement_open_transactions).twice @@ -110,9 +94,6 @@ module DatabaseCleaner connection_pool.stub(:connections).and_return([connection, connection_2]) connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) connection_2.should_receive(:open_transactions).and_return(0) @@ -150,8 +131,6 @@ module DatabaseCleaner it "should decrement connection via ActiveRecord::Base if connection won't" do connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(true) connection.stub(:rollback_transaction) ::ActiveRecord::Base.should_not_receive(:decrement_open_transactions) diff --git a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb index 0a36ab8..e0aab62 100644 --- a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb @@ -7,9 +7,9 @@ require 'database_cleaner/active_record/truncation/shared_fast_truncation' module ActiveRecord module ConnectionAdapters describe do - before(:all) { active_record_mysql2_setup } + before(:all) { MySQL2Helper.active_record_mysql2_setup } - let(:connection) { active_record_mysql2_connection } + let(:connection) { MySQL2Helper.active_record_mysql2_connection } describe "#truncate_table" do it "should truncate the table" do @@ -30,7 +30,7 @@ module ActiveRecord end it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { active_record_mysql2_connection } + let(:connection) { MySQL2Helper.active_record_mysql2_connection } end end end diff --git a/spec/database_cleaner/active_record/truncation/mysql_spec.rb b/spec/database_cleaner/active_record/truncation/mysql_spec.rb index a6eb8e9..4865c62 100644 --- a/spec/database_cleaner/active_record/truncation/mysql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql_spec.rb @@ -7,9 +7,9 @@ require 'database_cleaner/active_record/truncation/shared_fast_truncation' module ActiveRecord module ConnectionAdapters describe do - before(:all) { active_record_mysql_setup } + before(:all) { MySQLHelper.active_record_mysql_setup } - let(:connection) { active_record_mysql_connection } + let(:connection) { MySQLHelper.active_record_mysql_connection } describe "#truncate_table" do it "should truncate the table" do @@ -30,7 +30,7 @@ module ActiveRecord end it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { active_record_mysql_connection } + let(:connection) { MySQLHelper.active_record_mysql_connection } end end end diff --git a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb index 15baf49..41ed1d6 100644 --- a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb @@ -8,18 +8,18 @@ module ActiveRecord module ConnectionAdapters describe "schema_migrations table" do it "is not truncated" do - active_record_pg_migrate + PostgreSQLHelper.active_record_pg_migrate DatabaseCleaner::ActiveRecord::Truncation.new.clean - result = active_record_pg_connection.execute("select count(*) from schema_migrations;") + result = PostgreSQLHelper.active_record_pg_connection.execute("select count(*) from schema_migrations;") result.values.first.should eq ["2"] end end describe do - before(:all) { active_record_pg_setup } + before(:all) { PostgreSQLHelper.active_record_pg_setup } let(:connection) do - active_record_pg_connection + PostgreSQLHelper.active_record_pg_connection end before(:each) do @@ -67,7 +67,7 @@ module ActiveRecord end it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { active_record_pg_connection } + let(:connection) { PostgreSQLHelper.active_record_pg_connection } end end diff --git a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb index 75c212b..82e0678 100644 --- a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb @@ -6,10 +6,10 @@ require 'database_cleaner/active_record/truncation' module ActiveRecord module ConnectionAdapters describe do - before(:all) { active_record_sqlite3_setup } + before(:all) { SQLite3Helper.active_record_sqlite3_setup } let(:connection) do - active_record_sqlite3_connection + SQLite3Helper.active_record_sqlite3_connection end before(:each) do diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index d077ed1..a8b8585 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -302,14 +302,11 @@ module DatabaseCleaner end it "should check that strategy supports db specification" do - strategy.should_receive(:respond_to?).with(:db=).and_return(true) strategy.stub(:db=) subject.strategy_db = :a_db end context "when strategy supports db specification" do - before(:each) { strategy.stub(:respond_to?).with(:db=).and_return true } - it "should pass db to the strategy" do strategy.should_receive(:db=).with(:a_db) subject.strategy_db = :a_db @@ -317,8 +314,6 @@ module DatabaseCleaner end context "when strategy doesn't supports db specification" do - before(:each) { strategy.stub(:respond_to?).with(:db=).and_return false } - it "should check to see if db is :default" do db = double("default") db.should_receive(:==).with(:default).and_return(true) @@ -540,7 +535,6 @@ module DatabaseCleaner end it "should use available_strategies (for the error message) if its available" do - strategy_class.stub(:respond_to?).with(:available_strategies).and_return(true) strategy_class.should_receive(:available_strategies).and_return([]) subject.stub(:orm_module).and_return(strategy_class) diff --git a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb index e294169..c5863a5 100644 --- a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb @@ -1,18 +1,18 @@ require 'spec_helper' require 'dm-core' require 'dm-sqlite-adapter' -require File.expand_path('../../../../support/data_mapper/sqlite3_setup', __FILE__) +require 'support/data_mapper/sqlite3_setup' require 'database_cleaner/data_mapper/truncation' module DataMapper module ConnectionAdapters describe do - before(:all) { data_mapper_sqlite3_setup } + before(:all) { DataMapperSQLite3Helper.data_mapper_sqlite3_setup } let(:adapter) { DataMapperSQLite3Adapter } let(:connection) do - data_mapper_sqlite3_connection + DataMapperSQLite3Helper.data_mapper_sqlite3_connection end before(:each) do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0d64961..9558e58 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,16 +23,10 @@ RSpec.configure do |config| config.run_all_when_everything_filtered = true config.expect_with :rspec do |expectations| - # Enable only the newer, non-monkey-patching expect syntax. - # For more details, see: - # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax expectations.syntax = [:should, :expect] end config.mock_with :rspec do |mocks| - # Enable only the newer, non-monkey-patching expect syntax. - # For more details, see: - # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ mocks.syntax = [:should, :expect] end end diff --git a/spec/support/active_record/mysql2_setup.rb b/spec/support/active_record/mysql2_setup.rb index d0caf24..6e76ad6 100644 --- a/spec/support/active_record/mysql2_setup.rb +++ b/spec/support/active_record/mysql2_setup.rb @@ -2,6 +2,8 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' module MySQL2Helper + extend self + puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2" # require 'logger' @@ -39,6 +41,3 @@ module MySQL2Helper end end -RSpec.configure do |c| - c.include MySQL2Helper -end diff --git a/spec/support/active_record/mysql_setup.rb b/spec/support/active_record/mysql_setup.rb index 7e3d20a..e0ade73 100644 --- a/spec/support/active_record/mysql_setup.rb +++ b/spec/support/active_record/mysql_setup.rb @@ -2,6 +2,8 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' module MySQLHelper + extend self + puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql" # require 'logger' @@ -39,6 +41,3 @@ module MySQLHelper end end -RSpec.configure do |c| - c.include MySQLHelper -end diff --git a/spec/support/active_record/postgresql_setup.rb b/spec/support/active_record/postgresql_setup.rb index 26ec12b..fb851ef 100644 --- a/spec/support/active_record/postgresql_setup.rb +++ b/spec/support/active_record/postgresql_setup.rb @@ -2,6 +2,8 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' module PostgreSQLHelper + extend self + puts "Active Record #{ActiveRecord::VERSION::STRING}, pg" # ActiveRecord::Base.logger = Logger.new(STDERR) @@ -43,6 +45,3 @@ module PostgreSQLHelper end end -RSpec.configure do |c| - c.include PostgreSQLHelper -end diff --git a/spec/support/active_record/sqlite3_setup.rb b/spec/support/active_record/sqlite3_setup.rb index 0ba264a..8047c83 100644 --- a/spec/support/active_record/sqlite3_setup.rb +++ b/spec/support/active_record/sqlite3_setup.rb @@ -2,6 +2,8 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' module SQLite3Helper + extend self + puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3" # ActiveRecord::Base.logger = Logger.new(STDERR) @@ -35,6 +37,3 @@ module SQLite3Helper end end -RSpec.configure do |c| - c.include SQLite3Helper -end diff --git a/spec/support/data_mapper/sqlite3_setup.rb b/spec/support/data_mapper/sqlite3_setup.rb index a133210..98b29be 100644 --- a/spec/support/data_mapper/sqlite3_setup.rb +++ b/spec/support/data_mapper/sqlite3_setup.rb @@ -2,6 +2,7 @@ require 'support/active_record/database_setup' require 'support/data_mapper/schema_setup' module DataMapperSQLite3Helper + extend self puts "DataMapper #{DataMapper::VERSION}, sqlite3" @@ -34,6 +35,3 @@ module DataMapperSQLite3Helper end end -RSpec.configure do |c| - c.include(DataMapperSQLite3Helper) -end From 2fff7ab3f4e768815efa96b47f598a89eb36e430 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 10:57:18 -0700 Subject: [PATCH 03/14] update to rspec 3 syntax. --- .../active_record/base_spec.rb | 66 +++--- .../active_record/transaction_spec.rb | 88 ++++---- .../active_record/truncation/mysql2_spec.rb | 4 +- .../active_record/truncation/mysql_spec.rb | 4 +- .../truncation/postgresql_spec.rb | 10 +- .../truncation/shared_fast_truncation.rb | 8 +- .../active_record/truncation/sqlite3_spec.rb | 4 +- .../active_record/truncation_spec.rb | 92 ++++----- spec/database_cleaner/base_spec.rb | 190 +++++++++--------- spec/database_cleaner/configuration_spec.rb | 166 +++++++-------- .../couch_potato/truncation_spec.rb | 16 +- .../database_cleaner/data_mapper/base_spec.rb | 10 +- .../data_mapper/truncation/sqlite3_spec.rb | 4 +- spec/database_cleaner/generic/base_spec.rb | 12 +- .../generic/truncation_spec.rb | 28 +-- .../database_cleaner/mongo/truncation_spec.rb | 2 +- .../mongo_mapper/base_spec.rb | 8 +- .../mongo_mapper/truncation_spec.rb | 2 +- .../database_cleaner/moped/truncation_spec.rb | 2 +- spec/database_cleaner/neo4j/base_spec.rb | 14 +- spec/database_cleaner/null_strategy_spec.rb | 4 +- spec/database_cleaner/ohm/truncation_spec.rb | 16 +- spec/database_cleaner/redis/base_spec.rb | 12 +- .../database_cleaner/redis/truncation_spec.rb | 16 +- spec/database_cleaner/sequel/base_spec.rb | 10 +- .../sequel/truncation_spec.rb | 14 +- spec/database_cleaner/shared_strategy.rb | 14 +- spec/spec_helper.rb | 8 - 28 files changed, 408 insertions(+), 416 deletions(-) diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index a0829e9..240fd70 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -12,15 +12,15 @@ end module DatabaseCleaner describe ActiveRecord do - it { should respond_to(:available_strategies) } + it { is_expected.to respond_to(:available_strategies) } describe "config_file_location" do subject { ActiveRecord.config_file_location } it "should default to DatabaseCleaner.root / config / database.yml" do ActiveRecord.config_file_location=nil - DatabaseCleaner.should_receive(:app_root).and_return("/path/to") - subject.should eq '/path/to/config/database.yml' + expect(DatabaseCleaner).to receive(:app_root).and_return("/path/to") + expect(subject).to eq '/path/to/config/database.yml' end end @@ -36,25 +36,25 @@ module DatabaseCleaner '/path/to/config/database.yml' end - before { ::DatabaseCleaner::ActiveRecord.stub(:config_file_location).and_return(config_location) } + before { allow(::DatabaseCleaner::ActiveRecord).to receive(:config_file_location).and_return(config_location) } it_should_behave_like "a generic strategy" describe "db" do it "should store my desired db" do - subject.stub(:load_config) + allow(subject).to receive(:load_config) subject.db = :my_db - subject.db.should eq :my_db + expect(subject.db).to eq :my_db end it "should default to :default" do - subject.db.should eq :default + expect(subject.db).to eq :default end it "should load_config when I set db" do - subject.should_receive(:load_config) + expect(subject).to receive(:load_config) subject.db = :my_db end end @@ -67,12 +67,12 @@ module DatabaseCleaner my_db: database: <%= "ONE".downcase %> Y - File.stub(:file?).with(config_location).and_return(true) - IO.stub(:read).with(config_location).and_return(yaml) + allow(File).to receive(:file?).with(config_location).and_return(true) + allow(IO).to receive(:read).with(config_location).and_return(yaml) end it "should parse the config" do - YAML.should_receive(:load).and_return({ :nil => nil }) + expect(YAML).to receive(:load).and_return({ :nil => nil }) subject.load_config end @@ -81,62 +81,62 @@ my_db: my_db: database: one Y - YAML.should_receive(:load).with(transformed).and_return({ "my_db" => { "database" => "one" } }) + expect(YAML).to receive(:load).with(transformed).and_return({ "my_db" => { "database" => "one" } }) subject.load_config end context 'use ActiveRecord::Base.configuration' do it 'when config file different with it' do - ::ActiveRecord::Base.stub(:configurations).and_return({ "my_db" =>{ "database" => "two"} }) + allow(::ActiveRecord::Base).to receive(:configurations).and_return({ "my_db" =>{ "database" => "two"} }) subject.load_config - subject.connection_hash.should eq({ "database" => "two"}) + expect(subject.connection_hash).to eq({ "database" => "two"}) end end context 'use config file' do it 'when config file same with it' do - ::ActiveRecord::Base.stub(:configurations).and_return({ "my_db" =>{ "database" => "one"} }) + allow(::ActiveRecord::Base).to receive(:configurations).and_return({ "my_db" =>{ "database" => "one"} }) subject.load_config - subject.connection_hash.should eq({ "database" => "one"}) + expect(subject.connection_hash).to eq({ "database" => "one"}) end it 'when ::ActiveRecord::Base.configurations nil' do - ::ActiveRecord::Base.stub(:configurations).and_return(nil) + allow(::ActiveRecord::Base).to receive(:configurations).and_return(nil) subject.load_config - subject.connection_hash.should eq({ "database" => "one"}) + expect(subject.connection_hash).to eq({ "database" => "one"}) end it 'when ::ActiveRecord::Base.configurations empty' do - ::ActiveRecord::Base.stub(:configurations).and_return({}) + allow(::ActiveRecord::Base).to receive(:configurations).and_return({}) subject.load_config - subject.connection_hash.should eq({ "database" => "one"}) + expect(subject.connection_hash).to eq({ "database" => "one"}) end end it "should store the relevant config in connection_hash" do subject.load_config - subject.connection_hash.should eq( "database" => "one" ) + expect(subject.connection_hash).to eq( "database" => "one" ) end it "should skip config if config file is not available" do - File.should_receive(:file?).with(config_location).and_return(false) + expect(File).to receive(:file?).with(config_location).and_return(false) subject.load_config - subject.connection_hash.should_not be + expect(subject.connection_hash).not_to be end it "skips the file when the model is set" do subject.db = FakeModel - YAML.should_not_receive(:load) + expect(YAML).not_to receive(:load) subject.load_config - subject.connection_hash.should_not be + expect(subject.connection_hash).not_to be end it "skips the file when the db is set to :default" do # to avoid https://github.com/bmabey/database_cleaner/issues/72 subject.db = :default - YAML.should_not_receive(:load) + expect(YAML).not_to receive(:load) subject.load_config - subject.connection_hash.should_not be + expect(subject.connection_hash).not_to be end end @@ -144,14 +144,14 @@ my_db: describe "connection_hash" do it "should store connection_hash" do subject.connection_hash = { :key => "value" } - subject.connection_hash.should eq( :key => "value" ) + expect(subject.connection_hash).to eq( :key => "value" ) end end describe "connection_class" do it { expect { subject.connection_class }.to_not raise_error } it "should default to ActiveRecord::Base" do - subject.connection_class.should eq ::ActiveRecord::Base + expect(subject.connection_class).to eq ::ActiveRecord::Base end context "with database models" do @@ -160,24 +160,24 @@ my_db: subject.db = FakeModel subject.connection_hash = { } subject.load_config - subject.connection_class.should eq FakeModel + expect(subject.connection_class).to eq FakeModel end end context "connection_hash is not set" do it "allows for database models to be passed in" do subject.db = FakeModel - subject.connection_class.should eq FakeModel + expect(subject.connection_class).to eq FakeModel end end end context "when connection_hash is set" do let(:hash) { {} } - before { subject.stub(:connection_hash).and_return(hash) } + before { allow(subject).to receive(:connection_hash).and_return(hash) } it "establish a connection using ActiveRecord::Base" do - ::ActiveRecord::Base.should_receive(:establish_connection).with(hash) + expect(::ActiveRecord::Base).to receive(:establish_connection).with(hash) expect(subject.connection_class).to eq ::ActiveRecord::Base end diff --git a/spec/database_cleaner/active_record/transaction_spec.rb b/spec/database_cleaner/active_record/transaction_spec.rb index c91e3bd..2816048 100644 --- a/spec/database_cleaner/active_record/transaction_spec.rb +++ b/spec/database_cleaner/active_record/transaction_spec.rb @@ -10,33 +10,33 @@ module DatabaseCleaner let (:connection_2) { double("connection") } let (:connection_pool) { double("connection_pool")} before(:each) do - ::ActiveRecord::Base.stub(:connection_pool).and_return(connection_pool) - connection_pool.stub(:connections).and_return([connection]) - ::ActiveRecord::Base.stub(:connection).and_return(connection) + allow(::ActiveRecord::Base).to receive(:connection_pool).and_return(connection_pool) + allow(connection_pool).to receive(:connections).and_return([connection]) + allow(::ActiveRecord::Base).to receive(:connection).and_return(connection) end describe "#start" do [:begin_transaction, :begin_db_transaction].each do |begin_transaction_method| context "using #{begin_transaction_method}" do before do - connection.stub(:transaction) - connection.stub(begin_transaction_method) + allow(connection).to receive(:transaction) + allow(connection).to receive(begin_transaction_method) end it "should increment open transactions if possible" do - connection.should_receive(:increment_open_transactions) + expect(connection).to receive(:increment_open_transactions) Transaction.new.start end it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do - ::ActiveRecord::Base.should_receive(:increment_open_transactions) + expect(::ActiveRecord::Base).to receive(:increment_open_transactions) Transaction.new.start end it "should start a transaction" do - connection.stub(:increment_open_transactions) - connection.should_receive(begin_transaction_method) - connection.should_receive(:transaction) + allow(connection).to receive(:increment_open_transactions) + expect(connection).to receive(begin_transaction_method) + expect(connection).to receive(:transaction) Transaction.new.start end end @@ -46,59 +46,59 @@ module DatabaseCleaner describe "#clean" do context "manual accounting of transaction count" do it "should start a transaction" do - connection.should_receive(:open_transactions).and_return(1) + expect(connection).to receive(:open_transactions).and_return(1) - connection.stub(:decrement_open_transactions) + allow(connection).to receive(:decrement_open_transactions) - connection.should_receive(:rollback_db_transaction) + expect(connection).to receive(:rollback_db_transaction) Transaction.new.clean end it "should decrement open transactions if possible" do - connection.should_receive(:open_transactions).and_return(1) + expect(connection).to receive(:open_transactions).and_return(1) - connection.stub(:rollback_db_transaction) + allow(connection).to receive(:rollback_db_transaction) - connection.should_receive(:decrement_open_transactions) + expect(connection).to receive(:decrement_open_transactions) Transaction.new.clean end it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do - connection.should_receive(:open_transactions).and_return(0) + expect(connection).to receive(:open_transactions).and_return(0) Transaction.new.clean end it "should decrement connection via ActiveRecord::Base if connection won't" do - connection.should_receive(:open_transactions).and_return(1) - connection.stub(:rollback_db_transaction) + expect(connection).to receive(:open_transactions).and_return(1) + allow(connection).to receive(:rollback_db_transaction) - ::ActiveRecord::Base.should_receive(:decrement_open_transactions) + expect(::ActiveRecord::Base).to receive(:decrement_open_transactions) Transaction.new.clean end it "should rollback open transactions in all connections" do - connection_pool.stub(:connections).and_return([connection, connection_2]) + allow(connection_pool).to receive(:connections).and_return([connection, connection_2]) - connection.should_receive(:open_transactions).and_return(1) - connection.stub(:rollback_db_transaction) + expect(connection).to receive(:open_transactions).and_return(1) + allow(connection).to receive(:rollback_db_transaction) - connection_2.should_receive(:open_transactions).and_return(1) - connection_2.stub(:rollback_db_transaction) + expect(connection_2).to receive(:open_transactions).and_return(1) + allow(connection_2).to receive(:rollback_db_transaction) - ::ActiveRecord::Base.should_receive(:decrement_open_transactions).twice + expect(::ActiveRecord::Base).to receive(:decrement_open_transactions).twice Transaction.new.clean end it "should rollback open transactions in all connections with an open transaction" do - connection_pool.stub(:connections).and_return([connection, connection_2]) + allow(connection_pool).to receive(:connections).and_return([connection, connection_2]) - connection.should_receive(:open_transactions).and_return(1) - connection.stub(:rollback_db_transaction) + expect(connection).to receive(:open_transactions).and_return(1) + allow(connection).to receive(:rollback_db_transaction) - connection_2.should_receive(:open_transactions).and_return(0) + expect(connection_2).to receive(:open_transactions).and_return(0) - ::ActiveRecord::Base.should_receive(:decrement_open_transactions).exactly(1).times + expect(::ActiveRecord::Base).to receive(:decrement_open_transactions).exactly(1).times Transaction.new.clean end end @@ -107,33 +107,33 @@ module DatabaseCleaner before {stub_const("ActiveRecord::VERSION::MAJOR", 4) } it "should start a transaction" do - connection.stub(:rollback_db_transaction) - connection.should_receive(:open_transactions).and_return(1) + allow(connection).to receive(:rollback_db_transaction) + expect(connection).to receive(:open_transactions).and_return(1) - connection.should_not_receive(:decrement_open_transactions) - connection.should_receive(:rollback_transaction) + expect(connection).not_to receive(:decrement_open_transactions) + expect(connection).to receive(:rollback_transaction) Transaction.new.clean end it "should decrement open transactions if possible" do - connection.stub(:rollback_transaction) - connection.should_receive(:open_transactions).and_return(1) + allow(connection).to receive(:rollback_transaction) + expect(connection).to receive(:open_transactions).and_return(1) - connection.should_not_receive(:decrement_open_transactions) + expect(connection).not_to receive(:decrement_open_transactions) Transaction.new.clean end it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do - connection.should_receive(:open_transactions).and_return(0) + expect(connection).to receive(:open_transactions).and_return(0) Transaction.new.clean end it "should decrement connection via ActiveRecord::Base if connection won't" do - connection.should_receive(:open_transactions).and_return(1) - connection.stub(:rollback_transaction) + expect(connection).to receive(:open_transactions).and_return(1) + allow(connection).to receive(:rollback_transaction) - ::ActiveRecord::Base.should_not_receive(:decrement_open_transactions) + expect(::ActiveRecord::Base).not_to receive(:decrement_open_transactions) Transaction.new.clean end end @@ -142,11 +142,11 @@ module DatabaseCleaner describe "#connection_maintains_transaction_count?" do it "should return true if the major active record version is < 4" do stub_const("ActiveRecord::VERSION::MAJOR", 3) - Transaction.new.connection_maintains_transaction_count?.should be_truthy + expect(Transaction.new.connection_maintains_transaction_count?).to be_truthy end it "should return false if the major active record version is > 3" do stub_const("ActiveRecord::VERSION::MAJOR", 4) - Transaction.new.connection_maintains_transaction_count?.should be_falsey + expect(Transaction.new.connection_maintains_transaction_count?).to be_falsey end end diff --git a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb index e0aab62..62355d3 100644 --- a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb @@ -16,7 +16,7 @@ module ActiveRecord 2.times { User.create } connection.truncate_table('users') - User.count.should eq 0 + expect(User.count).to eq 0 end it "should reset AUTO_INCREMENT index of table" do @@ -25,7 +25,7 @@ module ActiveRecord connection.truncate_table('users') - User.create.id.should eq 1 + expect(User.create.id).to eq 1 end end diff --git a/spec/database_cleaner/active_record/truncation/mysql_spec.rb b/spec/database_cleaner/active_record/truncation/mysql_spec.rb index 4865c62..d03d00e 100644 --- a/spec/database_cleaner/active_record/truncation/mysql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql_spec.rb @@ -16,7 +16,7 @@ module ActiveRecord 2.times { User.create } connection.truncate_table('users') - User.count.should eq 0 + expect(User.count).to eq 0 end it "should reset AUTO_INCREMENT index of table" do @@ -25,7 +25,7 @@ module ActiveRecord connection.truncate_table('users') - User.create.id.should eq 1 + expect(User.create.id).to eq 1 end end diff --git a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb index 41ed1d6..9ff25b9 100644 --- a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb @@ -11,7 +11,7 @@ module ActiveRecord PostgreSQLHelper.active_record_pg_migrate DatabaseCleaner::ActiveRecord::Truncation.new.clean result = PostgreSQLHelper.active_record_pg_connection.execute("select count(*) from schema_migrations;") - result.values.first.should eq ["2"] + expect(result.values.first).to eq ["2"] end end @@ -31,14 +31,14 @@ module ActiveRecord 2.times { User.create } connection.truncate_table('users') - User.count.should eq 0 + expect(User.count).to eq 0 end it "truncates the table without id sequence" do 2.times { Agent.create } connection.truncate_table('agents') - Agent.count.should eq 0 + expect(Agent.count).to eq 0 end it "resets AUTO_INCREMENT index of table" do @@ -47,7 +47,7 @@ module ActiveRecord connection.truncate_table('users') - User.create.id.should eq 1 + expect(User.create.id).to eq 1 end end @@ -62,7 +62,7 @@ module ActiveRecord describe '#database_cleaner_table_cache' do it 'should default to the list of tables with their schema' do - connection.database_cleaner_table_cache.first.should match(/^public\./) + expect(connection.database_cleaner_table_cache.first).to match(/^public\./) end end diff --git a/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb b/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb index 2f2e9b3..dda580c 100644 --- a/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb +++ b/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb @@ -6,7 +6,7 @@ shared_examples_for "an adapter with pre-count truncation" do 2.times { User.create } connection.pre_count_truncate_tables(%w[users], :reset_ids => true) - User.count.should be_zero + expect(User.count).to be_zero end it "resets AUTO_INCREMENT index of table" do @@ -14,7 +14,7 @@ shared_examples_for "an adapter with pre-count truncation" do User.delete_all connection.pre_count_truncate_tables(%w[users]) # true is also the default - User.create.id.should eq 1 + expect(User.create.id).to eq 1 end end @@ -24,7 +24,7 @@ shared_examples_for "an adapter with pre-count truncation" do 2.times { User.create } connection.pre_count_truncate_tables(%w[users], :reset_ids => false) - User.count.should be_zero + expect(User.count).to be_zero end it "does not reset AUTO_INCREMENT index of table" do @@ -33,7 +33,7 @@ shared_examples_for "an adapter with pre-count truncation" do connection.pre_count_truncate_tables(%w[users], :reset_ids => false) - User.create.id.should eq 3 + expect(User.create.id).to eq 3 end end end diff --git a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb index 82e0678..2388f47 100644 --- a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb @@ -21,7 +21,7 @@ module ActiveRecord 2.times { User.create } connection.truncate_table('users') - User.count.should eq 0 + expect(User.count).to eq 0 end it "resets AUTO_INCREMENT index of table" do @@ -30,7 +30,7 @@ module ActiveRecord connection.truncate_table('users') - User.create.id.should eq 1 + expect(User.create.id).to eq 1 end end diff --git a/spec/database_cleaner/active_record/truncation_spec.rb b/spec/database_cleaner/active_record/truncation_spec.rb index 22b35e0..b29387f 100644 --- a/spec/database_cleaner/active_record/truncation_spec.rb +++ b/spec/database_cleaner/active_record/truncation_spec.rb @@ -13,7 +13,7 @@ module ActiveRecord [ MysqlAdapter, Mysql2Adapter, SQLite3Adapter, PostgreSQLAdapter ].each do |adapter| describe adapter, "#truncate_table" do it "responds" do - adapter.instance_methods.should include(:truncate_table) + expect(adapter.instance_methods).to include(:truncate_table) end end end @@ -27,60 +27,60 @@ module DatabaseCleaner let(:connection) { double('connection') } before(:each) do - connection.stub(:disable_referential_integrity).and_yield - connection.stub(:database_cleaner_view_cache).and_return([]) - ::ActiveRecord::Base.stub(:connection).and_return(connection) + allow(connection).to receive(:disable_referential_integrity).and_yield + allow(connection).to receive(:database_cleaner_view_cache).and_return([]) + allow(::ActiveRecord::Base).to receive(:connection).and_return(connection) end describe '#clean' do it "should truncate all tables except for schema_migrations" do - connection.stub(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs]) + allow(connection).to receive(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs]) - connection.should_receive(:truncate_tables).with(['widgets', 'dogs']) + expect(connection).to receive(:truncate_tables).with(['widgets', 'dogs']) Truncation.new.clean end it "should use ActiveRecord's SchemaMigration.table_name" do - connection.stub(:database_cleaner_table_cache).and_return(%w[pre_schema_migrations_suf widgets dogs]) - ::ActiveRecord::Base.stub(:table_name_prefix).and_return('pre_') - ::ActiveRecord::Base.stub(:table_name_suffix).and_return('_suf') + allow(connection).to receive(:database_cleaner_table_cache).and_return(%w[pre_schema_migrations_suf widgets dogs]) + allow(::ActiveRecord::Base).to receive(:table_name_prefix).and_return('pre_') + allow(::ActiveRecord::Base).to receive(:table_name_suffix).and_return('_suf') - connection.should_receive(:truncate_tables).with(['widgets', 'dogs']) + expect(connection).to receive(:truncate_tables).with(['widgets', 'dogs']) Truncation.new.clean end it "should only truncate the tables specified in the :only option when provided" do - connection.stub(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs]) + allow(connection).to receive(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs]) - connection.should_receive(:truncate_tables).with(['widgets']) + expect(connection).to receive(:truncate_tables).with(['widgets']) Truncation.new(:only => ['widgets']).clean end it "should not truncate the tables specified in the :except option" do - connection.stub(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs]) + allow(connection).to receive(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs]) - connection.should_receive(:truncate_tables).with(['dogs']) + expect(connection).to receive(:truncate_tables).with(['dogs']) Truncation.new(:except => ['widgets']).clean end it "should raise an error when :only and :except options are used" do - running { + expect(running { Truncation.new(:except => ['widgets'], :only => ['widgets']) - }.should raise_error(ArgumentError) + }).to raise_error(ArgumentError) end it "should raise an error when invalid options are provided" do - running { Truncation.new(:foo => 'bar') }.should raise_error(ArgumentError) + expect(running { Truncation.new(:foo => 'bar') }).to raise_error(ArgumentError) end it "should not truncate views" do - connection.stub(:database_cleaner_table_cache).and_return(%w[widgets dogs]) - connection.stub(:database_cleaner_view_cache).and_return(["widgets"]) + allow(connection).to receive(:database_cleaner_table_cache).and_return(%w[widgets dogs]) + allow(connection).to receive(:database_cleaner_view_cache).and_return(["widgets"]) - connection.should_receive(:truncate_tables).with(['dogs']) + expect(connection).to receive(:truncate_tables).with(['dogs']) Truncation.new.clean end @@ -89,25 +89,25 @@ module DatabaseCleaner subject { Truncation.new } it "should rely on #pre_count_truncate_tables if #pre_count? returns true" do - connection.stub(:database_cleaner_table_cache).and_return(%w[widgets dogs]) - connection.stub(:database_cleaner_view_cache).and_return(["widgets"]) + allow(connection).to receive(:database_cleaner_table_cache).and_return(%w[widgets dogs]) + allow(connection).to receive(:database_cleaner_view_cache).and_return(["widgets"]) subject.instance_variable_set(:"@pre_count", true) - connection.should_not_receive(:truncate_tables).with(['dogs']) - connection.should_receive(:pre_count_truncate_tables).with(['dogs'], :reset_ids => true) + expect(connection).not_to receive(:truncate_tables).with(['dogs']) + expect(connection).to receive(:pre_count_truncate_tables).with(['dogs'], :reset_ids => true) subject.clean end it "should not rely on #pre_count_truncate_tables if #pre_count? return false" do - connection.stub(:database_cleaner_table_cache).and_return(%w[widgets dogs]) - connection.stub(:database_cleaner_view_cache).and_return(["widgets"]) + allow(connection).to receive(:database_cleaner_table_cache).and_return(%w[widgets dogs]) + allow(connection).to receive(:database_cleaner_view_cache).and_return(["widgets"]) subject.instance_variable_set(:"@pre_count", false) - connection.should_not_receive(:pre_count_truncate_tables).with(['dogs'], :reset_ids => true) - connection.should_receive(:truncate_tables).with(['dogs']) + expect(connection).not_to receive(:pre_count_truncate_tables).with(['dogs'], :reset_ids => true) + expect(connection).to receive(:truncate_tables).with(['dogs']) subject.clean end @@ -115,20 +115,20 @@ module DatabaseCleaner context 'when :cache_tables is set to true' do it 'caches the list of tables to be truncated' do - connection.should_receive(:database_cleaner_table_cache).and_return([]) - connection.should_not_receive(:tables) + expect(connection).to receive(:database_cleaner_table_cache).and_return([]) + expect(connection).not_to receive(:tables) - connection.stub(:truncate_tables) + allow(connection).to receive(:truncate_tables) Truncation.new({ :cache_tables => true }).clean end end context 'when :cache_tables is set to false' do it 'does not cache the list of tables to be truncated' do - connection.should_not_receive(:database_cleaner_table_cache) - connection.should_receive(:tables).and_return([]) + expect(connection).not_to receive(:database_cleaner_table_cache) + expect(connection).to receive(:tables).and_return([]) - connection.stub(:truncate_tables) + allow(connection).to receive(:truncate_tables) Truncation.new({ :cache_tables => false }).clean end end @@ -136,49 +136,49 @@ module DatabaseCleaner describe '#pre_count?' do before(:each) do - connection.stub(:disable_referential_integrity).and_yield - connection.stub(:database_cleaner_view_cache).and_return([]) - ::ActiveRecord::Base.stub(:connection).and_return(connection) + allow(connection).to receive(:disable_referential_integrity).and_yield + allow(connection).to receive(:database_cleaner_view_cache).and_return([]) + allow(::ActiveRecord::Base).to receive(:connection).and_return(connection) end subject { Truncation.new } it 'should return false initially' do - subject.send(:pre_count?).should eq false + expect(subject.send(:pre_count?)).to eq false end it 'should return true if @reset_id is set and non false or nil' do subject.instance_variable_set(:"@pre_count", true) - subject.send(:pre_count?).should eq true + expect(subject.send(:pre_count?)).to eq true end it 'should return false if @reset_id is set to false' do subject.instance_variable_set(:"@pre_count", false) - subject.send(:pre_count?).should eq false + expect(subject.send(:pre_count?)).to eq false end end describe '#reset_ids?' do before(:each) do - connection.stub(:disable_referential_integrity).and_yield - connection.stub(:database_cleaner_view_cache).and_return([]) - ::ActiveRecord::Base.stub(:connection).and_return(connection) + allow(connection).to receive(:disable_referential_integrity).and_yield + allow(connection).to receive(:database_cleaner_view_cache).and_return([]) + allow(::ActiveRecord::Base).to receive(:connection).and_return(connection) end subject { Truncation.new } it 'should return true initially' do - subject.send(:reset_ids?).should eq true + expect(subject.send(:reset_ids?)).to eq true end it 'should return true if @reset_id is set and non falsey' do subject.instance_variable_set(:"@reset_ids", 'Something') - subject.send(:reset_ids?).should eq true + expect(subject.send(:reset_ids?)).to eq true end it 'should return false if @reset_id is set to false' do subject.instance_variable_set(:"@reset_ids", false) - subject.send(:reset_ids?).should eq false + expect(subject.send(:reset_ids?)).to eq false end end end diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index a8b8585..a43806a 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -11,7 +11,7 @@ module DatabaseCleaner let(:mock_strategy) { double("strategy").tap{|strategy| - strategy.stub(:to_ary => [strategy]) + allow(strategy).to receive_messages(:to_ary => [strategy]) } } @@ -75,7 +75,7 @@ module DatabaseCleaner let(:cleaner) { DatabaseCleaner::Base.new :autodetect } it "should raise an error when no ORM is detected" do - running { cleaner }.should raise_error(DatabaseCleaner::NoORMDetected) + expect(running { cleaner }).to raise_error(DatabaseCleaner::NoORMDetected) end it "should detect ActiveRecord first" do @@ -90,8 +90,8 @@ module DatabaseCleaner Object.const_set('Redis', 'Redis mock') Object.const_set('Neo4j', 'Neo4j mock') - cleaner.orm.should eq :active_record - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :active_record + expect(cleaner).to be_auto_detected end it "should detect DataMapper second" do @@ -105,8 +105,8 @@ module DatabaseCleaner Object.const_set('Redis', 'Redis mock') Object.const_set('Neo4j', 'Neo4j mock') - cleaner.orm.should eq :data_mapper - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :data_mapper + expect(cleaner).to be_auto_detected end it "should detect MongoMapper third" do @@ -119,8 +119,8 @@ module DatabaseCleaner Object.const_set('Redis', 'Redis mock') Object.const_set('Neo4j', 'Neo4j mock') - cleaner.orm.should eq :mongo_mapper - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :mongo_mapper + expect(cleaner).to be_auto_detected end it "should detect Mongoid fourth" do @@ -132,8 +132,8 @@ module DatabaseCleaner Object.const_set('Redis', 'Redis mock') Object.const_set('Neo4j', 'Neo4j mock') - cleaner.orm.should eq :mongoid - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :mongoid + expect(cleaner).to be_auto_detected end it "should detect CouchPotato fifth" do @@ -144,8 +144,8 @@ module DatabaseCleaner Object.const_set('Redis', 'Redis mock') Object.const_set('Neo4j', 'Neo4j mock') - cleaner.orm.should eq :couch_potato - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :couch_potato + expect(cleaner).to be_auto_detected end it "should detect Sequel sixth" do @@ -155,15 +155,15 @@ module DatabaseCleaner Object.const_set('Redis', 'Redis mock') Object.const_set('Neo4j', 'Neo4j mock') - cleaner.orm.should eq :sequel - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :sequel + expect(cleaner).to be_auto_detected end it 'detects Moped seventh' do Object.const_set('Moped', 'Moped mock') - cleaner.orm.should eq :moped - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :moped + expect(cleaner).to be_auto_detected end it 'detects Ohm eighth' do @@ -171,23 +171,23 @@ module DatabaseCleaner Object.const_set('Redis', 'Redis mock') Object.const_set('Neo4j', 'Neo4j mock') - cleaner.orm.should eq :ohm - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :ohm + expect(cleaner).to be_auto_detected end it 'detects Redis ninth' do Object.const_set('Redis', 'Redis mock') Object.const_set('Neo4j', 'Neo4j mock') - cleaner.orm.should eq :redis - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :redis + expect(cleaner).to be_auto_detected end it 'detects Neo4j tenth' do Object.const_set('Neo4j', 'Neo4j mock') - cleaner.orm.should eq :neo4j - cleaner.should be_auto_detected + expect(cleaner.orm).to eq :neo4j + expect(cleaner).to be_auto_detected end end @@ -197,11 +197,11 @@ module DatabaseCleaner mockule = double("module") cleaner = ::DatabaseCleaner::Base.new - cleaner.should_receive(:orm).and_return(orm) + expect(cleaner).to receive(:orm).and_return(orm) - ::DatabaseCleaner.should_receive(:orm_module).with(orm).and_return(mockule) + expect(::DatabaseCleaner).to receive(:orm_module).with(orm).and_return(mockule) - cleaner.send(:orm_module).should eq mockule + expect(cleaner.send(:orm_module)).to eq mockule end end @@ -213,8 +213,8 @@ module DatabaseCleaner two = DatabaseCleaner::Base.new(:active_record,:connection => :default) two.strategy = mock_strategy - one.should eq two - two.should eq one + expect(one).to eq two + expect(two).to eq one end it "should not be equal if orm are not the same" do @@ -224,8 +224,8 @@ module DatabaseCleaner two = DatabaseCleaner::Base.new(:active_record, :connection => :default) two.strategy = mock_strategy - one.should_not eq two - two.should_not eq one + expect(one).not_to eq two + expect(two).not_to eq one end it "should not be equal if connection are not the same" do @@ -236,8 +236,8 @@ module DatabaseCleaner two = DatabaseCleaner::Base.new(:active_record, :connection => :other) two.strategy = :truncation - one.should_not eq two - two.should_not eq one + expect(one).not_to eq two + expect(two).not_to eq one end end @@ -246,50 +246,50 @@ module DatabaseCleaner subject { ::DatabaseCleaner::Base.new(:active_record,:connection => :my_db) } it "should store db from :connection in params hash" do - subject.db.should eq :my_db + expect(subject.db).to eq :my_db end end describe "orm" do it "should store orm" do cleaner = ::DatabaseCleaner::Base.new :a_orm - cleaner.orm.should eq :a_orm + expect(cleaner.orm).to eq :a_orm end it "converts string to symbols" do cleaner = ::DatabaseCleaner::Base.new "mongoid" - cleaner.orm.should eq :mongoid + expect(cleaner.orm).to eq :mongoid end it "is autodetected if orm is not provided" do cleaner = ::DatabaseCleaner::Base.new - cleaner.should be_auto_detected + expect(cleaner).to be_auto_detected end it "is autodetected if you specify :autodetect" do cleaner = ::DatabaseCleaner::Base.new "autodetect" - cleaner.should be_auto_detected + expect(cleaner).to be_auto_detected end it "should default to autodetect upon initalisation" do - subject.should be_auto_detected + expect(subject).to be_auto_detected end end end describe "db" do it "should default to :default" do - subject.db.should eq :default + expect(subject.db).to eq :default end it "should return any stored db value" do - subject.stub(:strategy_db=) + allow(subject).to receive(:strategy_db=) subject.db = :test_db - subject.db.should eq :test_db + expect(subject.db).to eq :test_db end it "should pass db to any specified strategy" do - subject.should_receive(:strategy_db=).with(:a_new_db) + expect(subject).to receive(:strategy_db=).with(:a_new_db) subject.db = :a_new_db end end @@ -302,13 +302,13 @@ module DatabaseCleaner end it "should check that strategy supports db specification" do - strategy.stub(:db=) + allow(strategy).to receive(:db=) subject.strategy_db = :a_db end context "when strategy supports db specification" do it "should pass db to the strategy" do - strategy.should_receive(:db=).with(:a_db) + expect(strategy).to receive(:db=).with(:a_db) subject.strategy_db = :a_db end end @@ -316,7 +316,7 @@ module DatabaseCleaner context "when strategy doesn't supports db specification" do it "should check to see if db is :default" do db = double("default") - db.should_receive(:==).with(:default).and_return(true) + expect(db).to receive(:==).with(:default).and_return(true) subject.strategy_db = db end @@ -331,40 +331,40 @@ module DatabaseCleaner describe "clean_with" do let (:strategy) { double("strategy",:clean => true) } - before(:each) { subject.stub(:create_strategy).with(anything).and_return(strategy) } + before(:each) { allow(subject).to receive(:create_strategy).with(anything).and_return(strategy) } it "should pass all arguments to create_strategy" do - subject.should_receive(:create_strategy).with(:lorum, :dollar, :amet, :ipsum => "random").and_return(strategy) + expect(subject).to receive(:create_strategy).with(:lorum, :dollar, :amet, :ipsum => "random").and_return(strategy) subject.clean_with :lorum, :dollar, :amet, { :ipsum => "random" } end it "should invoke clean on the created strategy" do - strategy.should_receive(:clean) + expect(strategy).to receive(:clean) subject.clean_with :strategy end it "should return the strategy" do - subject.clean_with( :strategy ).should eq strategy + expect(subject.clean_with( :strategy )).to eq strategy end end describe "clean_with!" do let (:strategy) { double("strategy",:clean => true) } - before(:each) { subject.stub(:create_strategy).with(anything).and_return(strategy) } + before(:each) { allow(subject).to receive(:create_strategy).with(anything).and_return(strategy) } it "should pass all arguments to create_strategy" do - subject.should_receive(:create_strategy).with(:lorum, :dollar, :amet, :ipsum => "random").and_return(strategy) + expect(subject).to receive(:create_strategy).with(:lorum, :dollar, :amet, :ipsum => "random").and_return(strategy) subject.clean_with! :lorum, :dollar, :amet, { :ipsum => "random" } end it "should invoke clean on the created strategy" do - strategy.should_receive(:clean) + expect(strategy).to receive(:clean) subject.clean_with! :strategy end it "should return the strategy" do - subject.clean_with!( :strategy ).should eq strategy + expect(subject.clean_with!( :strategy )).to eq strategy end end @@ -372,31 +372,31 @@ module DatabaseCleaner let(:strategy_class) { double("strategy_class",:new => double("instance")) } before :each do - subject.stub(:orm_strategy).and_return(strategy_class) + allow(subject).to receive(:orm_strategy).and_return(strategy_class) end it "should pass the first argument to orm_strategy" do - subject.should_receive(:orm_strategy).with(:strategy).and_return(Object) + expect(subject).to receive(:orm_strategy).with(:strategy).and_return(Object) subject.create_strategy :strategy end it "should pass the remainding argument to orm_strategy.new" do - strategy_class.should_receive(:new).with(:params => {:lorum => "ipsum"}) + expect(strategy_class).to receive(:new).with(:params => {:lorum => "ipsum"}) subject.create_strategy :strategy, {:params => {:lorum => "ipsum"}} end it "should return the resulting strategy" do - subject.create_strategy( :strategy ).should eq strategy_class.new + expect(subject.create_strategy( :strategy )).to eq strategy_class.new end end describe "strategy=" do it "should proxy symbolised strategies to create_strategy" do - subject.should_receive(:create_strategy).with(:symbol) + expect(subject).to receive(:create_strategy).with(:symbol) subject.strategy = :symbol end it "should proxy params with symbolised strategies" do - subject.should_receive(:create_strategy).with(:symbol,:param => "one") + expect(subject).to receive(:create_strategy).with(:symbol,:param => "one") subject.strategy= :symbol, {:param => "one"} end @@ -409,14 +409,14 @@ module DatabaseCleaner end it "should attempt to set strategy db" do - subject.stub(:db).and_return(:my_db) - subject.should_receive(:set_strategy_db).with(mock_strategy, :my_db) + allow(subject).to receive(:db).and_return(:my_db) + expect(subject).to receive(:set_strategy_db).with(mock_strategy, :my_db) subject.strategy = mock_strategy end it "should return the stored strategy" do result = subject.strategy = mock_strategy - result.should eq mock_strategy + expect(result).to eq mock_strategy end end @@ -424,20 +424,20 @@ module DatabaseCleaner subject { ::DatabaseCleaner::Base.new :a_orm } it "returns a null strategy when strategy is not set and undetectable" do - subject.strategy.should eq DatabaseCleaner::NullStrategy + expect(subject.strategy).to eq DatabaseCleaner::NullStrategy end it "returns the set strategy" do subject.strategy = mock_strategy - subject.strategy.should eq mock_strategy + expect(subject.strategy).to eq mock_strategy end end describe "orm=" do it "should stored the desired orm" do - subject.orm.should_not eq :desired_orm + expect(subject.orm).not_to eq :desired_orm subject.orm = :desired_orm - subject.orm.should eq :desired_orm + expect(subject.orm).to eq :desired_orm end end @@ -446,20 +446,20 @@ module DatabaseCleaner it "should return orm if orm set" do subject.instance_variable_set "@orm", mock_orm - subject.orm.should eq mock_orm + expect(subject.orm).to eq mock_orm end context "orm isn't set" do before(:each) { subject.instance_variable_set "@orm", nil } it "should run autodetect if orm isn't set" do - subject.should_receive(:autodetect) + expect(subject).to receive(:autodetect) subject.orm end it "should return the result of autodetect if orm isn't set" do - subject.stub(:autodetect).and_return(mock_orm) - subject.orm.should eq mock_orm + allow(subject).to receive(:autodetect).and_return(mock_orm) + expect(subject.orm).to eq mock_orm end end end @@ -468,33 +468,33 @@ module DatabaseCleaner let (:strategy) { double("strategy") } before(:each) do - subject.stub(:strategy).and_return(strategy) + allow(subject).to receive(:strategy).and_return(strategy) end describe "start" do it "should proxy start to the strategy" do - strategy.should_receive(:start) + expect(strategy).to receive(:start) subject.start end end describe "clean" do it "should proxy clean to the strategy" do - strategy.should_receive(:clean) + expect(strategy).to receive(:clean) subject.clean end end describe "clean!" do it "should proxy clean! to the strategy clean" do - strategy.should_receive(:clean) + expect(strategy).to receive(:clean) subject.clean! end end describe "cleaning" do it "should proxy cleaning to the strategy" do - strategy.should_receive(:cleaning) + expect(strategy).to receive(:cleaning) subject.cleaning { } end end @@ -503,12 +503,12 @@ module DatabaseCleaner describe "auto_detected?" do it "should return true unless @autodetected is nil" do subject.instance_variable_set("@autodetected","not nil") - subject.auto_detected?.should be_truthy + expect(subject.auto_detected?).to be_truthy end it "should return false if @autodetect is nil" do subject.instance_variable_set("@autodetected",nil) - subject.auto_detected?.should be_falsey + expect(subject.auto_detected?).to be_falsey end end @@ -516,28 +516,28 @@ module DatabaseCleaner let (:strategy_class) { double("strategy_class") } before(:each) do - subject.stub(:orm_module).and_return(strategy_class) + allow(subject).to receive(:orm_module).and_return(strategy_class) end context "in response to a LoadError" do - before(:each) { subject.should_receive(:require).with(anything).and_raise(LoadError) } + before(:each) { expect(subject).to receive(:require).with(anything).and_raise(LoadError) } it "should raise UnknownStrategySpecified" do expect { subject.send(:orm_strategy,:a_strategy) }.to raise_error UnknownStrategySpecified end it "should ask orm_module if it will list available_strategies" do - strategy_class.should_receive(:respond_to?).with(:available_strategies) + expect(strategy_class).to receive(:respond_to?).with(:available_strategies) - subject.stub(:orm_module).and_return(strategy_class) + allow(subject).to receive(:orm_module).and_return(strategy_class) expect { subject.send(:orm_strategy,:a_strategy) }.to raise_error UnknownStrategySpecified end it "should use available_strategies (for the error message) if its available" do - strategy_class.should_receive(:available_strategies).and_return([]) + expect(strategy_class).to receive(:available_strategies).and_return([]) - subject.stub(:orm_module).and_return(strategy_class) + allow(subject).to receive(:orm_module).and_return(strategy_class) expect { subject.send(:orm_strategy,:a_strategy) }.to raise_error UnknownStrategySpecified end @@ -546,11 +546,11 @@ module DatabaseCleaner it "should return the constant of the Strategy class requested" do strategy_strategy_class = double("strategy strategy_class") - subject.stub(:require).with(anything).and_return(true) + allow(subject).to receive(:require).with(anything).and_return(true) - strategy_class.should_receive(:const_get).with("Cunningplan").and_return(strategy_strategy_class) + expect(strategy_class).to receive(:const_get).with("Cunningplan").and_return(strategy_strategy_class) - subject.send(:orm_strategy, :cunningplan).should eq strategy_strategy_class + expect(subject.send(:orm_strategy, :cunningplan)).to eq strategy_strategy_class end end @@ -558,52 +558,52 @@ module DatabaseCleaner describe 'set_default_orm_strategy' do it 'sets strategy to :transaction for ActiveRecord' do cleaner = DatabaseCleaner::Base.new(:active_record) - cleaner.strategy.should be_instance_of DatabaseCleaner::ActiveRecord::Transaction + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::ActiveRecord::Transaction end it 'sets strategy to :transaction for DataMapper' do cleaner = DatabaseCleaner::Base.new(:data_mapper) - cleaner.strategy.should be_instance_of DatabaseCleaner::DataMapper::Transaction + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::DataMapper::Transaction end it 'sets strategy to :truncation for MongoMapper' do cleaner = DatabaseCleaner::Base.new(:mongo_mapper) - cleaner.strategy.should be_instance_of DatabaseCleaner::MongoMapper::Truncation + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::MongoMapper::Truncation end it 'sets strategy to :truncation for Mongoid' do cleaner = DatabaseCleaner::Base.new(:mongoid) - cleaner.strategy.should be_instance_of DatabaseCleaner::Mongoid::Truncation + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Mongoid::Truncation end it 'sets strategy to :truncation for CouchPotato' do cleaner = DatabaseCleaner::Base.new(:couch_potato) - cleaner.strategy.should be_instance_of DatabaseCleaner::CouchPotato::Truncation + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::CouchPotato::Truncation end it 'sets strategy to :transaction for Sequel' do cleaner = DatabaseCleaner::Base.new(:sequel) - cleaner.strategy.should be_instance_of DatabaseCleaner::Sequel::Transaction + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Sequel::Transaction end it 'sets strategy to :truncation for Moped' do cleaner = DatabaseCleaner::Base.new(:moped) - cleaner.strategy.should be_instance_of DatabaseCleaner::Moped::Truncation + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Moped::Truncation end it 'sets strategy to :truncation for Ohm' do cleaner = DatabaseCleaner::Base.new(:ohm) - cleaner.strategy.should be_instance_of DatabaseCleaner::Ohm::Truncation + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Ohm::Truncation end it 'sets strategy to :truncation for Redis' do cleaner = DatabaseCleaner::Base.new(:redis) - cleaner.strategy.should be_instance_of DatabaseCleaner::Redis::Truncation + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Redis::Truncation end it 'sets strategy to :transaction for Neo4j' do cleaner = DatabaseCleaner::Base.new(:neo4j) - cleaner.strategy.should be_instance_of DatabaseCleaner::Neo4j::Transaction + expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Neo4j::Transaction end end diff --git a/spec/database_cleaner/configuration_spec.rb b/spec/database_cleaner/configuration_spec.rb index 8b7f501..ce1c6cc 100644 --- a/spec/database_cleaner/configuration_spec.rb +++ b/spec/database_cleaner/configuration_spec.rb @@ -30,71 +30,71 @@ describe ::DatabaseCleaner do it "should accept :active_record" do cleaner = ::DatabaseCleaner[:active_record] - cleaner.should be_a(::DatabaseCleaner::Base) - cleaner.orm.should eq :active_record - ::DatabaseCleaner.connections.size.should eq 1 + expect(cleaner).to be_a(::DatabaseCleaner::Base) + expect(cleaner.orm).to eq :active_record + expect(::DatabaseCleaner.connections.size).to eq 1 end it "should accept :data_mapper" do cleaner = ::DatabaseCleaner[:data_mapper] - cleaner.should be_a(::DatabaseCleaner::Base) - cleaner.orm.should eq :data_mapper - ::DatabaseCleaner.connections.size.should eq 1 + expect(cleaner).to be_a(::DatabaseCleaner::Base) + expect(cleaner.orm).to eq :data_mapper + expect(::DatabaseCleaner.connections.size).to eq 1 end it "should accept :mongo_mapper" do cleaner = ::DatabaseCleaner[:mongo_mapper] - cleaner.should be_a(::DatabaseCleaner::Base) - cleaner.orm.should eq :mongo_mapper - ::DatabaseCleaner.connections.size.should eq 1 + expect(cleaner).to be_a(::DatabaseCleaner::Base) + expect(cleaner.orm).to eq :mongo_mapper + expect(::DatabaseCleaner.connections.size).to eq 1 end it "should accept :couch_potato" do cleaner = ::DatabaseCleaner[:couch_potato] - cleaner.should be_a(::DatabaseCleaner::Base) - cleaner.orm.should eq :couch_potato - ::DatabaseCleaner.connections.size.should eq 1 + expect(cleaner).to be_a(::DatabaseCleaner::Base) + expect(cleaner.orm).to eq :couch_potato + expect(::DatabaseCleaner.connections.size).to eq 1 end it "should accept :moped" do cleaner = ::DatabaseCleaner[:moped] - cleaner.should be_a(::DatabaseCleaner::Base) - cleaner.orm.should eq :moped - ::DatabaseCleaner.connections.size.should eq 1 + expect(cleaner).to be_a(::DatabaseCleaner::Base) + expect(cleaner.orm).to eq :moped + expect(::DatabaseCleaner.connections.size).to eq 1 end it 'accepts :ohm' do cleaner = ::DatabaseCleaner[:ohm] - cleaner.should be_a(::DatabaseCleaner::Base) - cleaner.orm.should eq :ohm - ::DatabaseCleaner.connections.size.should eq 1 + expect(cleaner).to be_a(::DatabaseCleaner::Base) + expect(cleaner.orm).to eq :ohm + expect(::DatabaseCleaner.connections.size).to eq 1 end end it "should accept multiple orm's" do ::DatabaseCleaner[:couch_potato] ::DatabaseCleaner[:data_mapper] - ::DatabaseCleaner.connections.size.should eq 2 - ::DatabaseCleaner.connections[0].orm.should eq :couch_potato - ::DatabaseCleaner.connections[1].orm.should eq :data_mapper + expect(::DatabaseCleaner.connections.size).to eq 2 + expect(::DatabaseCleaner.connections[0].orm).to eq :couch_potato + expect(::DatabaseCleaner.connections[1].orm).to eq :data_mapper end context "connection/db specification" do it "should accept a connection parameter and store it" do cleaner = ::DatabaseCleaner[:active_record, {:connection => :first_connection}] - cleaner.should be_a(::DatabaseCleaner::Base) - cleaner.orm.should eq :active_record - cleaner.db.should eq :first_connection + expect(cleaner).to be_a(::DatabaseCleaner::Base) + expect(cleaner.orm).to eq :active_record + expect(cleaner.db).to eq :first_connection end it "should accept multiple connections for a single orm" do ::DatabaseCleaner[:data_mapper,{:connection => :first_db}] ::DatabaseCleaner[:data_mapper,{:connection => :second_db}] - ::DatabaseCleaner.connections.size.should eq 2 - ::DatabaseCleaner.connections[0].orm.should eq :data_mapper - ::DatabaseCleaner.connections[0].db.should eq :first_db - ::DatabaseCleaner.connections[1].orm.should eq :data_mapper - ::DatabaseCleaner.connections[1].db.should eq :second_db + expect(::DatabaseCleaner.connections.size).to eq 2 + expect(::DatabaseCleaner.connections[0].orm).to eq :data_mapper + expect(::DatabaseCleaner.connections[0].db).to eq :first_db + expect(::DatabaseCleaner.connections[1].orm).to eq :data_mapper + expect(::DatabaseCleaner.connections[1].db).to eq :second_db end it "should accept multiple connections and multiple orms" do @@ -103,19 +103,19 @@ describe ::DatabaseCleaner do ::DatabaseCleaner[:active_record,{:connection => :first_db} ] ::DatabaseCleaner[:data_mapper, {:connection => :second_db}] - ::DatabaseCleaner.connections.size.should eq 4 + expect(::DatabaseCleaner.connections.size).to eq 4 - ::DatabaseCleaner.connections[0].orm.should eq :data_mapper - ::DatabaseCleaner.connections[0].db.should eq :first_db + expect(::DatabaseCleaner.connections[0].orm).to eq :data_mapper + expect(::DatabaseCleaner.connections[0].db).to eq :first_db - ::DatabaseCleaner.connections[1].orm.should eq :active_record - ::DatabaseCleaner.connections[1].db.should eq :second_db + expect(::DatabaseCleaner.connections[1].orm).to eq :active_record + expect(::DatabaseCleaner.connections[1].db).to eq :second_db - ::DatabaseCleaner.connections[2].orm.should eq :active_record - ::DatabaseCleaner.connections[2].db.should eq :first_db + expect(::DatabaseCleaner.connections[2].orm).to eq :active_record + expect(::DatabaseCleaner.connections[2].db).to eq :first_db - ::DatabaseCleaner.connections[3].orm.should eq :data_mapper - ::DatabaseCleaner.connections[3].db.should eq :second_db + expect(::DatabaseCleaner.connections[3].orm).to eq :data_mapper + expect(::DatabaseCleaner.connections[3].db).to eq :second_db end end @@ -123,7 +123,7 @@ describe ::DatabaseCleaner do it "should retrieve a db rather than create a new one" do pending connection = ::DatabaseCleaner[:active_record].strategy = :truncation - ::DatabaseCleaner[:active_record].should eq connection + expect(::DatabaseCleaner[:active_record]).to eq connection end end @@ -132,9 +132,9 @@ describe ::DatabaseCleaner do it "should give me a default (autodetection) databasecleaner by default" do cleaner = double("cleaner").as_null_object - ::DatabaseCleaner::Base.stub(:new).and_return(cleaner) + allow(::DatabaseCleaner::Base).to receive(:new).and_return(cleaner) - ::DatabaseCleaner.connections.should eq [cleaner] + expect(::DatabaseCleaner.connections).to eq [cleaner] end end @@ -143,34 +143,34 @@ describe ::DatabaseCleaner do it "should proxy strategy=" do stratagum = double("stratagum") - connection.should_receive(:strategy=).with(stratagum) + expect(connection).to receive(:strategy=).with(stratagum) ::DatabaseCleaner.strategy = stratagum end it "should proxy orm=" do orm = double("orm") - connection.should_receive(:orm=).with(orm) + expect(connection).to receive(:orm=).with(orm) ::DatabaseCleaner.orm = orm end it "should proxy start" do - connection.should_receive(:start) + expect(connection).to receive(:start) ::DatabaseCleaner.start end it "should proxy clean" do - connection.should_receive(:clean) + expect(connection).to receive(:clean) ::DatabaseCleaner.clean end it 'should proxy cleaning' do - connection.should_receive(:cleaning) + expect(connection).to receive(:cleaning) ::DatabaseCleaner.cleaning { } end it "should proxy clean_with" do stratagem = double("stratgem") - connection.should_receive(:clean_with).with(stratagem, {}) + expect(connection).to receive(:clean_with).with(stratagem, {}) ::DatabaseCleaner.clean_with stratagem, {} end end @@ -184,26 +184,26 @@ describe ::DatabaseCleaner do let(:data_mapper) { double("data_mock") } before(:each) do - ::DatabaseCleaner.stub(:connections).and_return([active_record,data_mapper]) + allow(::DatabaseCleaner).to receive(:connections).and_return([active_record,data_mapper]) end it "should proxy orm to all connections" do - active_record.should_receive(:orm=) - data_mapper.should_receive(:orm=) + expect(active_record).to receive(:orm=) + expect(data_mapper).to receive(:orm=) ::DatabaseCleaner.orm = double("orm") end it "should proxy start to all connections" do - active_record.should_receive(:start) - data_mapper.should_receive(:start) + expect(active_record).to receive(:start) + expect(data_mapper).to receive(:start) ::DatabaseCleaner.start end it "should proxy clean to all connections" do - active_record.should_receive(:clean) - data_mapper.should_receive(:clean) + expect(active_record).to receive(:clean) + expect(data_mapper).to receive(:clean) ::DatabaseCleaner.clean end @@ -220,20 +220,20 @@ describe ::DatabaseCleaner do end ::DatabaseCleaner.cleaning do - active_record.started.should == true - data_mapper.started.should == true - active_record.cleaned.should == nil - data_mapper.cleaned.should == nil + expect(active_record.started).to eq(true) + expect(data_mapper.started).to eq(true) + expect(active_record.cleaned).to eq(nil) + expect(data_mapper.cleaned).to eq(nil) @yielded = true end - active_record.cleaned.should == true - data_mapper.cleaned.should == true + expect(active_record.cleaned).to eq(true) + expect(data_mapper.cleaned).to eq(true) end it "should proxy clean_with to all connections" do stratagem = double("stratgem") - active_record.should_receive(:clean_with).with(stratagem) - data_mapper.should_receive(:clean_with).with(stratagem) + expect(active_record).to receive(:clean_with).with(stratagem) + expect(data_mapper).to receive(:clean_with).with(stratagem) ::DatabaseCleaner.clean_with stratagem end @@ -250,15 +250,15 @@ describe ::DatabaseCleaner do ::DatabaseCleaner.connections_stub [active_record_1,active_record_2,data_mapper_1] - active_record_1.should_receive(:orm=).with(:data_mapper) - active_record_2.should_receive(:orm=).with(:data_mapper) - data_mapper_1.should_receive(:orm=).with(:data_mapper) + expect(active_record_1).to receive(:orm=).with(:data_mapper) + expect(active_record_2).to receive(:orm=).with(:data_mapper) + expect(data_mapper_1).to receive(:orm=).with(:data_mapper) - active_record_1.should_receive(:==).with(data_mapper_1).and_return(true) + expect(active_record_1).to receive(:==).with(data_mapper_1).and_return(true) - ::DatabaseCleaner.connections.size.should eq 3 + expect(::DatabaseCleaner.connections.size).to eq 3 ::DatabaseCleaner.orm = :data_mapper - ::DatabaseCleaner.connections.size.should eq 2 + expect(::DatabaseCleaner.connections.size).to eq 2 end it "should proxy strategy to all connections and remove duplicate connections" do @@ -268,14 +268,14 @@ describe ::DatabaseCleaner do ::DatabaseCleaner.connections_stub [active_record_1,active_record_2] - active_record_1.should_receive(:strategy=).with(strategy) - active_record_2.should_receive(:strategy=).with(strategy) + expect(active_record_1).to receive(:strategy=).with(strategy) + expect(active_record_2).to receive(:strategy=).with(strategy) - active_record_1.should_receive(:==).with(active_record_2).and_return(true) + expect(active_record_1).to receive(:==).with(active_record_2).and_return(true) - ::DatabaseCleaner.connections.size.should eq 2 + expect(::DatabaseCleaner.connections.size).to eq 2 ::DatabaseCleaner.strategy = strategy - ::DatabaseCleaner.connections.size.should eq 1 + expect(::DatabaseCleaner.connections.size).to eq 1 end end end @@ -288,18 +288,18 @@ describe ::DatabaseCleaner do ::DatabaseCleaner.connections_stub [connection,connection,connection] ::DatabaseCleaner.remove_duplicates - ::DatabaseCleaner.connections.size.should eq 1 + expect(::DatabaseCleaner.connections.size).to eq 1 end end describe "app_root" do it "should default to Dir.pwd" do - DatabaseCleaner.app_root.should eq Dir.pwd + expect(DatabaseCleaner.app_root).to eq Dir.pwd end it "should store specific paths" do DatabaseCleaner.app_root = '/path/to' - DatabaseCleaner.app_root.should eq '/path/to' + expect(DatabaseCleaner.app_root).to eq '/path/to' end end @@ -308,37 +308,37 @@ describe ::DatabaseCleaner do it "should return DatabaseCleaner::ActiveRecord for :active_record" do ::DatabaseCleaner::ActiveRecord = double("ar module") unless defined? ::DatabaseCleaner::ActiveRecord - subject.orm_module(:active_record).should eq DatabaseCleaner::ActiveRecord + expect(subject.orm_module(:active_record)).to eq DatabaseCleaner::ActiveRecord end it "should return DatabaseCleaner::DataMapper for :data_mapper" do ::DatabaseCleaner::DataMapper = double("dm module") unless defined? ::DatabaseCleaner::DataMapper - subject.orm_module(:data_mapper).should eq DatabaseCleaner::DataMapper + expect(subject.orm_module(:data_mapper)).to eq DatabaseCleaner::DataMapper end it "should return DatabaseCleaner::MongoMapper for :mongo_mapper" do ::DatabaseCleaner::MongoMapper = double("mm module") unless defined? ::DatabaseCleaner::MongoMapper - subject.orm_module(:mongo_mapper).should eq DatabaseCleaner::MongoMapper + expect(subject.orm_module(:mongo_mapper)).to eq DatabaseCleaner::MongoMapper end it "should return DatabaseCleaner::Mongoid for :mongoid" do ::DatabaseCleaner::Mongoid = double("mongoid module") unless defined? ::DatabaseCleaner::Mongoid - subject.orm_module(:mongoid).should eq DatabaseCleaner::Mongoid + expect(subject.orm_module(:mongoid)).to eq DatabaseCleaner::Mongoid end it "should return DatabaseCleaner::Mongo for :mongo" do ::DatabaseCleaner::Mongo = double("mongo module") unless defined? ::DatabaseCleaner::Mongo - subject.orm_module(:mongo).should eq DatabaseCleaner::Mongo + expect(subject.orm_module(:mongo)).to eq DatabaseCleaner::Mongo end it "should return DatabaseCleaner::CouchPotato for :couch_potato" do ::DatabaseCleaner::CouchPotato = double("cp module") unless defined? ::DatabaseCleaner::CouchPotato - subject.orm_module(:couch_potato).should eq DatabaseCleaner::CouchPotato + expect(subject.orm_module(:couch_potato)).to eq DatabaseCleaner::CouchPotato end it "should return DatabaseCleaner::Neo4j for :neo4j" do ::DatabaseCleaner::Neo4j = double("nj module") unless defined? ::DatabaseCleaner::Neo4j - subject.orm_module(:neo4j).should eq DatabaseCleaner::Neo4j + expect(subject.orm_module(:neo4j)).to eq DatabaseCleaner::Neo4j end end diff --git a/spec/database_cleaner/couch_potato/truncation_spec.rb b/spec/database_cleaner/couch_potato/truncation_spec.rb index 883adc0..1cebd87 100644 --- a/spec/database_cleaner/couch_potato/truncation_spec.rb +++ b/spec/database_cleaner/couch_potato/truncation_spec.rb @@ -9,31 +9,31 @@ module DatabaseCleaner let(:database) { double('database') } before(:each) do - ::CouchPotato.stub(:couchrest_database).and_return(database) + allow(::CouchPotato).to receive(:couchrest_database).and_return(database) end it "should re-create the database" do - database.should_receive(:recreate!) + expect(database).to receive(:recreate!) Truncation.new.clean end it "should raise an error when the :only option is used" do - running { + expect(running { Truncation.new(:only => ['document-type']) - }.should raise_error(ArgumentError) + }).to raise_error(ArgumentError) end it "should raise an error when the :except option is used" do - running { + expect(running { Truncation.new(:except => ['document-type']) - }.should raise_error(ArgumentError) + }).to raise_error(ArgumentError) end it "should raise an error when invalid options are provided" do - running { + expect(running { Truncation.new(:foo => 'bar') - }.should raise_error(ArgumentError) + }).to raise_error(ArgumentError) end end diff --git a/spec/database_cleaner/data_mapper/base_spec.rb b/spec/database_cleaner/data_mapper/base_spec.rb index cb15f4a..fc7de3a 100644 --- a/spec/database_cleaner/data_mapper/base_spec.rb +++ b/spec/database_cleaner/data_mapper/base_spec.rb @@ -4,7 +4,7 @@ require 'database_cleaner/shared_strategy' module DatabaseCleaner describe DataMapper do - it { should respond_to(:available_strategies) } + it { is_expected.to respond_to(:available_strategies) } end module DataMapper @@ -14,16 +14,16 @@ module DatabaseCleaner describe ExampleStrategy do it_should_behave_like "a generic strategy" - it { should respond_to(:db) } - it { should respond_to(:db=) } + it { is_expected.to respond_to(:db) } + it { is_expected.to respond_to(:db=) } it "should store my desired db" do subject.db = :my_db - subject.db.should eq :my_db + expect(subject.db).to eq :my_db end it "should default to :default" do - subject.db.should eq :default + expect(subject.db).to eq :default end end end diff --git a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb index c5863a5..bee8dae 100644 --- a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb @@ -24,7 +24,7 @@ module DataMapper 2.times { DmUser.create } connection.truncate_table(DmUser.storage_names[:default]) - DmUser.count.should eq 0 + expect(DmUser.count).to eq 0 end it "resets AUTO_INCREMENT index of table" do @@ -33,7 +33,7 @@ module DataMapper connection.truncate_table(DmUser.storage_names[:default]) - DmUser.create.id.should eq 1 + expect(DmUser.create.id).to eq 1 end end end diff --git a/spec/database_cleaner/generic/base_spec.rb b/spec/database_cleaner/generic/base_spec.rb index 6c223d1..57f7bdf 100644 --- a/spec/database_cleaner/generic/base_spec.rb +++ b/spec/database_cleaner/generic/base_spec.rb @@ -35,14 +35,14 @@ module ::DatabaseCleaner let (:strategy) { ExampleStrategy.new } before do # DatabaseCleaner.strategy = :truncation - connection.stub(:disable_referential_integrity).and_yield - connection.stub(:database_cleaner_view_cache).and_return([]) - connection.stub(:database_cleaner_table_cache).and_return([]) - ::ActiveRecord::Base.stub(:connection).and_return(connection) + allow(connection).to receive(:disable_referential_integrity).and_yield + allow(connection).to receive(:database_cleaner_view_cache).and_return([]) + allow(connection).to receive(:database_cleaner_table_cache).and_return([]) + allow(::ActiveRecord::Base).to receive(:connection).and_return(connection) end it "calls #clean even if there is an exception" do - strategy.should_receive :clean + expect(strategy).to receive :clean expect do strategy.cleaning do raise NoMethodError @@ -51,7 +51,7 @@ module ::DatabaseCleaner end it "calls #clean after processing the block" do - strategy.should_receive :clean + expect(strategy).to receive :clean strategy.cleaning do end end diff --git a/spec/database_cleaner/generic/truncation_spec.rb b/spec/database_cleaner/generic/truncation_spec.rb index c5d995d..60cfe6e 100644 --- a/spec/database_cleaner/generic/truncation_spec.rb +++ b/spec/database_cleaner/generic/truncation_spec.rb @@ -41,13 +41,13 @@ module ::DatabaseCleaner end context "private methods" do - it { should_not respond_to(:tables_to_truncate) } + it { is_expected.not_to respond_to(:tables_to_truncate) } it 'expects #tables_to_truncate to be implemented later' do expect{ truncation_example.send :tables_to_truncate }.to raise_error(NotImplementedError) end - it { should_not respond_to(:migration_storage_names) } + it { is_expected.not_to respond_to(:migration_storage_names) } end describe "initialize" do @@ -66,40 +66,40 @@ module ::DatabaseCleaner context "" do subject { TruncationExample.new( { :only => ["something"] } ) } - it { subject.only.should eq ["something"] } - it { subject.except.should eq [] } + it { expect(subject.only).to eq ["something"] } + it { expect(subject.except).to eq [] } end context "" do subject { TruncationExample.new( { :except => ["something"] } ) } - it { subject.only.should eq nil } - it { subject.except.should include("something") } + it { expect(subject.only).to eq nil } + it { expect(subject.except).to include("something") } end context "" do subject { TruncationExample.new( { :reset_ids => ["something"] } ) } - it { subject.reset_ids?.should eq true } + it { expect(subject.reset_ids?).to eq true } end context "" do subject { TruncationExample.new( { :reset_ids => nil } ) } - it { subject.reset_ids?.should eq false } + it { expect(subject.reset_ids?).to eq false } end context "" do subject { TruncationExample.new( { :pre_count => ["something"] } ) } - it { subject.pre_count?.should eq true } + it { expect(subject.pre_count?).to eq true } end context "" do subject { TruncationExample.new( { :pre_count => nil } ) } - it { subject.pre_count?.should eq false } + it { expect(subject.pre_count?).to eq false } end context "" do subject { MigrationExample.new } - it { subject.only.should eq nil } - it { subject.except.should eq %w[migration_storage_name] } + it { expect(subject.only).to eq nil } + it { expect(subject.except).to eq %w[migration_storage_name] } end context "" do @@ -107,8 +107,8 @@ module ::DatabaseCleaner subject { MigrationExample.new( { :except => EXCEPT_TABLES } ) } it "should not modify the array of excepted tables" do - subject.except.should include("migration_storage_name") - EXCEPT_TABLES.should_not include("migration_storage_name") + expect(subject.except).to include("migration_storage_name") + expect(EXCEPT_TABLES).not_to include("migration_storage_name") end end end diff --git a/spec/database_cleaner/mongo/truncation_spec.rb b/spec/database_cleaner/mongo/truncation_spec.rb index 6736eeb..5cef370 100644 --- a/spec/database_cleaner/mongo/truncation_spec.rb +++ b/spec/database_cleaner/mongo/truncation_spec.rb @@ -25,7 +25,7 @@ module DatabaseCleaner # very odd and disconcerting... expected_counts.each do |model_class, expected_count| actual_count = model_class.count - actual_count.should eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}" + expect(actual_count).to eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}" end end diff --git a/spec/database_cleaner/mongo_mapper/base_spec.rb b/spec/database_cleaner/mongo_mapper/base_spec.rb index 9db2a8b..aa8735e 100644 --- a/spec/database_cleaner/mongo_mapper/base_spec.rb +++ b/spec/database_cleaner/mongo_mapper/base_spec.rb @@ -4,7 +4,7 @@ require 'database_cleaner/shared_strategy' module DatabaseCleaner describe MongoMapper do - it { should respond_to(:available_strategies) } + it { is_expected.to respond_to(:available_strategies) } end module MongoMapper @@ -17,15 +17,15 @@ module DatabaseCleaner it_should_behave_like "a generic strategy" describe "db" do - it { should respond_to(:db=) } + it { is_expected.to respond_to(:db=) } it "should store my desired db" do subject.db = :my_db - subject.db.should eq :my_db + expect(subject.db).to eq :my_db end it "should default to :default" do - subject.db.should eq :default + expect(subject.db).to eq :default end end end diff --git a/spec/database_cleaner/mongo_mapper/truncation_spec.rb b/spec/database_cleaner/mongo_mapper/truncation_spec.rb index fc26734..7e8030a 100644 --- a/spec/database_cleaner/mongo_mapper/truncation_spec.rb +++ b/spec/database_cleaner/mongo_mapper/truncation_spec.rb @@ -26,7 +26,7 @@ module DatabaseCleaner begin expected_counts.each do |model_class, expected_count| actual_count = model_class.count - actual_count.should eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}" + expect(actual_count).to eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}" end rescue RSpec::Expectations::ExpectationNotMetError => e raise !sanity_check ? e : RSpec::ExpectationNotMetError::ExpectationNotMetError.new("SANITY CHECK FAILURE! This should never happen here: #{e.message}") diff --git a/spec/database_cleaner/moped/truncation_spec.rb b/spec/database_cleaner/moped/truncation_spec.rb index e7220ba..3c07977 100644 --- a/spec/database_cleaner/moped/truncation_spec.rb +++ b/spec/database_cleaner/moped/truncation_spec.rb @@ -29,7 +29,7 @@ module DatabaseCleaner # very odd and disconcerting... expected_counts.each do |model_class, expected_count| actual_count = model_class.count - actual_count.should eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}" + expect(actual_count).to eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}" end end diff --git a/spec/database_cleaner/neo4j/base_spec.rb b/spec/database_cleaner/neo4j/base_spec.rb index 50c7d18..b7502dd 100644 --- a/spec/database_cleaner/neo4j/base_spec.rb +++ b/spec/database_cleaner/neo4j/base_spec.rb @@ -4,7 +4,7 @@ require 'database_cleaner/shared_strategy' module DatabaseCleaner describe Neo4j do - it { should respond_to(:available_strategies) } + it { is_expected.to respond_to(:available_strategies) } end module Neo4j @@ -15,28 +15,28 @@ module DatabaseCleaner describe ExampleStrategy do it_should_behave_like "a generic strategy" - it { should respond_to(:db) } - it { should respond_to(:db=) } + it { is_expected.to respond_to(:db) } + it { is_expected.to respond_to(:db=) } it "should store my describe db" do db_conf = {:connection => {:type => :server_db, :path => 'http://localhost:7474'}} subject.db = db_conf - subject.db.should eq db_conf + expect(subject.db).to eq db_conf end it "should respect additional connection parameters" do db_conf = {:type => :server_db, :path => 'http://localhost:7474', basic_auth: {username: 'user', password: 'pass'}} subject.db = db_conf - stub_const("Neo4j::Session", double()).should_receive(:open).with(:server_db, 'http://localhost:7474', {basic_auth: {username: 'user', password: 'pass'}}) { true } + expect(stub_const("Neo4j::Session", double())).to receive(:open).with(:server_db, 'http://localhost:7474', {basic_auth: {username: 'user', password: 'pass'}}) { true } subject.start end it "should default to nil" do - subject.db.should be_nil + expect(subject.db).to be_nil end it "should return default configuration" do - subject.database.should eq(:type => :server_db, :path => 'http://localhost:7475/') + expect(subject.database).to eq(:type => :server_db, :path => 'http://localhost:7475/') end end end diff --git a/spec/database_cleaner/null_strategy_spec.rb b/spec/database_cleaner/null_strategy_spec.rb index 3fb1d14..7fb9332 100644 --- a/spec/database_cleaner/null_strategy_spec.rb +++ b/spec/database_cleaner/null_strategy_spec.rb @@ -3,11 +3,11 @@ require 'spec_helper' module DatabaseCleaner describe NullStrategy do it 'responds to .start' do - expect { NullStrategy.start }.not_to raise_error(NoMethodError) + expect { NullStrategy.start }.not_to raise_error end it 'responds to .clean' do - expect { NullStrategy.clean }.not_to raise_error(NoMethodError) + expect { NullStrategy.clean }.not_to raise_error end describe '.cleaning' do diff --git a/spec/database_cleaner/ohm/truncation_spec.rb b/spec/database_cleaner/ohm/truncation_spec.rb index 0b21773..8530a12 100644 --- a/spec/database_cleaner/ohm/truncation_spec.rb +++ b/spec/database_cleaner/ohm/truncation_spec.rb @@ -39,19 +39,19 @@ module DatabaseCleaner it "truncates all keys by default" do create_widget create_gadget - @redis.keys.size.should eq 6 + expect(@redis.keys.size).to eq 6 Truncation.new.clean - @redis.keys.size.should eq 0 + expect(@redis.keys.size).to eq 0 end context "when keys are provided to the :only option" do it "only truncates the specified keys" do create_widget create_gadget - @redis.keys.size.should eq 6 + expect(@redis.keys.size).to eq 6 Truncation.new(:only => ['*Widget*']).clean - @redis.keys.size.should eq 3 - @redis.get('DatabaseCleaner::Ohm::Gadget:id').should eq '1' + expect(@redis.keys.size).to eq 3 + expect(@redis.get('DatabaseCleaner::Ohm::Gadget:id')).to eq '1' end end @@ -59,10 +59,10 @@ module DatabaseCleaner it "truncates all but the specified keys" do create_widget create_gadget - @redis.keys.size.should eq 6 + expect(@redis.keys.size).to eq 6 Truncation.new(:except => ['*Widget*']).clean - @redis.keys.size.should eq 3 - @redis.get('DatabaseCleaner::Ohm::Widget:id').should eq '1' + expect(@redis.keys.size).to eq 3 + expect(@redis.get('DatabaseCleaner::Ohm::Widget:id')).to eq '1' end end end diff --git a/spec/database_cleaner/redis/base_spec.rb b/spec/database_cleaner/redis/base_spec.rb index d1aec4a..dafe8da 100644 --- a/spec/database_cleaner/redis/base_spec.rb +++ b/spec/database_cleaner/redis/base_spec.rb @@ -5,7 +5,7 @@ require 'database_cleaner/shared_strategy' module DatabaseCleaner describe Redis do - it { should respond_to(:available_strategies) } + it { is_expected.to respond_to(:available_strategies) } end module Redis @@ -16,14 +16,14 @@ module DatabaseCleaner describe ExampleStrategy do it_should_behave_like "a generic strategy" - it { should respond_to(:db) } - it { should respond_to(:db=) } + it { is_expected.to respond_to(:db) } + it { is_expected.to respond_to(:db=) } context "when passing url" do it "should store my describe db" do url = 'redis://localhost:6379/2' subject.db = 'redis://localhost:6379/2' - subject.db.should eq url + expect(subject.db).to eq url end end @@ -31,12 +31,12 @@ module DatabaseCleaner it "should store my describe db" do connection = ::Redis.new :url => 'redis://localhost:6379/2' subject.db = connection - subject.db.should eq connection + expect(subject.db).to eq connection end end it "should default to :default" do - subject.db.should eq :default + expect(subject.db).to eq :default end end end diff --git a/spec/database_cleaner/redis/truncation_spec.rb b/spec/database_cleaner/redis/truncation_spec.rb index 9083126..0e7bfb7 100644 --- a/spec/database_cleaner/redis/truncation_spec.rb +++ b/spec/database_cleaner/redis/truncation_spec.rb @@ -31,19 +31,19 @@ module DatabaseCleaner it "truncates all keys by default" do create_widget create_gadget - @redis.keys.size.should eq 2 + expect(@redis.keys.size).to eq 2 Truncation.new.clean - @redis.keys.size.should eq 0 + expect(@redis.keys.size).to eq 0 end context "when keys are provided to the :only option" do it "only truncates the specified keys" do create_widget create_gadget - @redis.keys.size.should eq 2 + expect(@redis.keys.size).to eq 2 Truncation.new(:only => ['Widge*']).clean - @redis.keys.size.should eq 1 - @redis.get('Gadget').should eq '1' + expect(@redis.keys.size).to eq 1 + expect(@redis.get('Gadget')).to eq '1' end end @@ -51,10 +51,10 @@ module DatabaseCleaner it "truncates all but the specified keys" do create_widget create_gadget - @redis.keys.size.should eq 2 + expect(@redis.keys.size).to eq 2 Truncation.new(:except => ['Widg*']).clean - @redis.keys.size.should eq 1 - @redis.get('Widget').should eq '1' + expect(@redis.keys.size).to eq 1 + expect(@redis.get('Widget')).to eq '1' end end end diff --git a/spec/database_cleaner/sequel/base_spec.rb b/spec/database_cleaner/sequel/base_spec.rb index f912d71..9ebc53b 100644 --- a/spec/database_cleaner/sequel/base_spec.rb +++ b/spec/database_cleaner/sequel/base_spec.rb @@ -5,7 +5,7 @@ require 'sequel' module DatabaseCleaner describe Sequel do - it { should respond_to(:available_strategies) } + it { is_expected.to respond_to(:available_strategies) } end module Sequel @@ -15,16 +15,16 @@ module DatabaseCleaner describe ExampleStrategy do it_should_behave_like "a generic strategy" - it { should respond_to(:db) } - it { should respond_to(:db=) } + it { is_expected.to respond_to(:db) } + it { is_expected.to respond_to(:db=) } it "should store my desired db" do subject.db = :my_db - subject.db.should eq :my_db + expect(subject.db).to eq :my_db end it "should default to :default" do - subject.db.should eq :default + expect(subject.db).to eq :default end pending "I figure out how to use Sequel and write some real tests for it..." diff --git a/spec/database_cleaner/sequel/truncation_spec.rb b/spec/database_cleaner/sequel/truncation_spec.rb index e4cc075..1845569 100644 --- a/spec/database_cleaner/sequel/truncation_spec.rb +++ b/spec/database_cleaner/sequel/truncation_spec.rb @@ -134,17 +134,17 @@ module DatabaseCleaner subject { Truncation.new.tap { |t| t.db = db } } it 'should return false initially' do - subject.send(:pre_count?).should eq false + expect(subject.send(:pre_count?)).to eq false end it 'should return true if @reset_id is set and non false or nil' do subject.instance_variable_set(:"@pre_count", true) - subject.send(:pre_count?).should eq true + expect(subject.send(:pre_count?)).to eq true end it 'should return false if @reset_id is set to false' do subject.instance_variable_set(:"@pre_count", false) - subject.send(:pre_count?).should eq false + expect(subject.send(:pre_count?)).to eq false end end @@ -154,8 +154,8 @@ module DatabaseCleaner it "should rely on #pre_count_truncate_tables if #pre_count? returns true" do subject.instance_variable_set(:"@pre_count", true) - subject.should_not_receive(:truncate_tables) - subject.should_receive(:pre_count_truncate_tables) + expect(subject).not_to receive(:truncate_tables) + expect(subject).to receive(:pre_count_truncate_tables) subject.clean end @@ -163,8 +163,8 @@ module DatabaseCleaner it "should not rely on #pre_count_truncate_tables if #pre_count? return false" do subject.instance_variable_set(:"@pre_count", false) - subject.should_not_receive(:pre_count_truncate_tables) - subject.should_receive(:truncate_tables) + expect(subject).not_to receive(:pre_count_truncate_tables) + expect(subject).to receive(:truncate_tables) subject.clean end diff --git a/spec/database_cleaner/shared_strategy.rb b/spec/database_cleaner/shared_strategy.rb index 62680d1..1c8eb7c 100644 --- a/spec/database_cleaner/shared_strategy.rb +++ b/spec/database_cleaner/shared_strategy.rb @@ -1,15 +1,15 @@ shared_examples_for "a generic strategy" do - it { should respond_to(:db) } + it { is_expected.to respond_to(:db) } end shared_examples_for "a generic truncation strategy" do - it { should respond_to(:start) } - it { should respond_to(:clean) } - it { should respond_to(:cleaning) } + it { is_expected.to respond_to(:start) } + it { is_expected.to respond_to(:clean) } + it { is_expected.to respond_to(:cleaning) } end shared_examples_for "a generic transaction strategy" do - it { should respond_to(:start) } - it { should respond_to(:clean) } - it { should respond_to(:cleaning) } + it { is_expected.to respond_to(:start) } + it { is_expected.to respond_to(:clean) } + it { is_expected.to respond_to(:cleaning) } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9558e58..6d6f2da 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,14 +21,6 @@ RSpec.configure do |config| # get run. config.filter_run :focus config.run_all_when_everything_filtered = true - - config.expect_with :rspec do |expectations| - expectations.syntax = [:should, :expect] - end - - config.mock_with :rspec do |mocks| - mocks.syntax = [:should, :expect] - end end alias running lambda From f7f8ef2cd3044b656407613e5a3a4abd955b301b Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 11:02:29 -0700 Subject: [PATCH 04/14] simplify spec loading. --- .rspec | 1 + spec/database_cleaner/active_record/base_spec.rb | 1 - .../active_record/transaction_spec.rb | 1 - .../active_record/truncation/mysql2_spec.rb | 1 - .../active_record/truncation/mysql_spec.rb | 1 - .../active_record/truncation/postgresql_spec.rb | 1 - .../active_record/truncation/sqlite3_spec.rb | 1 - .../active_record/truncation_spec.rb | 1 - spec/database_cleaner/base_spec.rb | 1 - spec/database_cleaner/configuration_spec.rb | 1 - .../couch_potato/truncation_spec.rb | 1 - spec/database_cleaner/data_mapper/base_spec.rb | 1 - .../data_mapper/transaction_spec.rb | 1 - .../data_mapper/truncation/sqlite3_spec.rb | 1 - spec/database_cleaner/generic/base_spec.rb | 1 - spec/database_cleaner/generic/truncation_spec.rb | 1 - spec/database_cleaner/mongo/truncation_spec.rb | 1 - spec/database_cleaner/mongo_mapper/base_spec.rb | 1 - .../mongo_mapper/truncation_spec.rb | 1 - spec/database_cleaner/moped/truncation_spec.rb | 1 - spec/database_cleaner/neo4j/base_spec.rb | 1 - spec/database_cleaner/neo4j/transaction_spec.rb | 1 - spec/database_cleaner/null_strategy_spec.rb | 2 +- spec/database_cleaner/ohm/truncation_spec.rb | 1 - spec/database_cleaner/redis/base_spec.rb | 1 - spec/database_cleaner/redis/truncation_spec.rb | 1 - spec/database_cleaner/safeguard_spec.rb | 1 - spec/database_cleaner/sequel/base_spec.rb | 1 - spec/database_cleaner/sequel/deletion_spec.rb | 1 - spec/database_cleaner/sequel/transaction_spec.rb | 1 - spec/database_cleaner/sequel/truncation_spec.rb | 1 - spec/spec_helper.rb | 15 +-------------- 32 files changed, 3 insertions(+), 44 deletions(-) diff --git a/.rspec b/.rspec index 16f9cdb..b83d9b7 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1,3 @@ --color --format documentation +--require spec_helper diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index 240fd70..56673b2 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'active_record' require 'database_cleaner/active_record/base' require 'database_cleaner/shared_strategy' diff --git a/spec/database_cleaner/active_record/transaction_spec.rb b/spec/database_cleaner/active_record/transaction_spec.rb index 2816048..db7524d 100644 --- a/spec/database_cleaner/active_record/transaction_spec.rb +++ b/spec/database_cleaner/active_record/transaction_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'database_cleaner/active_record/transaction' require 'active_record' diff --git a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb index 62355d3..306a8bb 100644 --- a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'active_record' require 'support/active_record/mysql2_setup' require 'database_cleaner/active_record/truncation' diff --git a/spec/database_cleaner/active_record/truncation/mysql_spec.rb b/spec/database_cleaner/active_record/truncation/mysql_spec.rb index d03d00e..20e946c 100644 --- a/spec/database_cleaner/active_record/truncation/mysql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'active_record' require 'support/active_record/mysql_setup' require 'database_cleaner/active_record/truncation' diff --git a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb index 9ff25b9..fb495ca 100644 --- a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'active_record' require 'support/active_record/postgresql_setup' require 'database_cleaner/active_record/truncation' diff --git a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb index 2388f47..0bf005c 100644 --- a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'active_record' require 'support/active_record/sqlite3_setup' require 'database_cleaner/active_record/truncation' diff --git a/spec/database_cleaner/active_record/truncation_spec.rb b/spec/database_cleaner/active_record/truncation_spec.rb index b29387f..03e683c 100644 --- a/spec/database_cleaner/active_record/truncation_spec.rb +++ b/spec/database_cleaner/active_record/truncation_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'active_record' require 'active_record/connection_adapters/mysql_adapter' require 'active_record/connection_adapters/mysql2_adapter' diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index a43806a..95fcd25 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../spec_helper' require 'database_cleaner/active_record/transaction' require 'database_cleaner/data_mapper/transaction' require 'database_cleaner/mongo_mapper/truncation' diff --git a/spec/database_cleaner/configuration_spec.rb b/spec/database_cleaner/configuration_spec.rb index ce1c6cc..2ed7ada 100644 --- a/spec/database_cleaner/configuration_spec.rb +++ b/spec/database_cleaner/configuration_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' module ArrayHelper def zipmap(array, vals) Hash[*(array.zip(vals).flatten)] diff --git a/spec/database_cleaner/couch_potato/truncation_spec.rb b/spec/database_cleaner/couch_potato/truncation_spec.rb index 1cebd87..e566a6b 100644 --- a/spec/database_cleaner/couch_potato/truncation_spec.rb +++ b/spec/database_cleaner/couch_potato/truncation_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'database_cleaner/couch_potato/truncation' require 'couch_potato' diff --git a/spec/database_cleaner/data_mapper/base_spec.rb b/spec/database_cleaner/data_mapper/base_spec.rb index fc7de3a..61b819d 100644 --- a/spec/database_cleaner/data_mapper/base_spec.rb +++ b/spec/database_cleaner/data_mapper/base_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'database_cleaner/data_mapper/base' require 'database_cleaner/shared_strategy' diff --git a/spec/database_cleaner/data_mapper/transaction_spec.rb b/spec/database_cleaner/data_mapper/transaction_spec.rb index e4c1a97..a7350e2 100644 --- a/spec/database_cleaner/data_mapper/transaction_spec.rb +++ b/spec/database_cleaner/data_mapper/transaction_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'database_cleaner/data_mapper/transaction' require 'database_cleaner/shared_strategy' #require 'data_mapper' diff --git a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb index bee8dae..0dc8c98 100644 --- a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'dm-core' require 'dm-sqlite-adapter' require 'support/data_mapper/sqlite3_setup' diff --git a/spec/database_cleaner/generic/base_spec.rb b/spec/database_cleaner/generic/base_spec.rb index 57f7bdf..61aa98b 100644 --- a/spec/database_cleaner/generic/base_spec.rb +++ b/spec/database_cleaner/generic/base_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'database_cleaner/shared_strategy' require 'database_cleaner/generic/base' require 'active_record' diff --git a/spec/database_cleaner/generic/truncation_spec.rb b/spec/database_cleaner/generic/truncation_spec.rb index 60cfe6e..d155a9d 100644 --- a/spec/database_cleaner/generic/truncation_spec.rb +++ b/spec/database_cleaner/generic/truncation_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'database_cleaner/generic/truncation' module ::DatabaseCleaner diff --git a/spec/database_cleaner/mongo/truncation_spec.rb b/spec/database_cleaner/mongo/truncation_spec.rb index 5cef370..9cb1d3e 100644 --- a/spec/database_cleaner/mongo/truncation_spec.rb +++ b/spec/database_cleaner/mongo/truncation_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'mongo' require 'database_cleaner/mongo/truncation' require File.dirname(__FILE__) + '/mongo_examples' diff --git a/spec/database_cleaner/mongo_mapper/base_spec.rb b/spec/database_cleaner/mongo_mapper/base_spec.rb index aa8735e..03a93ad 100644 --- a/spec/database_cleaner/mongo_mapper/base_spec.rb +++ b/spec/database_cleaner/mongo_mapper/base_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'database_cleaner/mongo_mapper/base' require 'database_cleaner/shared_strategy' diff --git a/spec/database_cleaner/mongo_mapper/truncation_spec.rb b/spec/database_cleaner/mongo_mapper/truncation_spec.rb index 7e8030a..e59c147 100644 --- a/spec/database_cleaner/mongo_mapper/truncation_spec.rb +++ b/spec/database_cleaner/mongo_mapper/truncation_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'mongo_mapper' require 'database_cleaner/mongo_mapper/truncation' require File.dirname(__FILE__) + '/mongo_examples' diff --git a/spec/database_cleaner/moped/truncation_spec.rb b/spec/database_cleaner/moped/truncation_spec.rb index 3c07977..98dbd4e 100644 --- a/spec/database_cleaner/moped/truncation_spec.rb +++ b/spec/database_cleaner/moped/truncation_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'moped' require 'database_cleaner/moped/truncation' require File.dirname(__FILE__) + '/moped_examples' diff --git a/spec/database_cleaner/neo4j/base_spec.rb b/spec/database_cleaner/neo4j/base_spec.rb index b7502dd..4d0c97c 100644 --- a/spec/database_cleaner/neo4j/base_spec.rb +++ b/spec/database_cleaner/neo4j/base_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'database_cleaner/neo4j/base' require 'database_cleaner/shared_strategy' diff --git a/spec/database_cleaner/neo4j/transaction_spec.rb b/spec/database_cleaner/neo4j/transaction_spec.rb index 95eb806..e567f13 100644 --- a/spec/database_cleaner/neo4j/transaction_spec.rb +++ b/spec/database_cleaner/neo4j/transaction_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'neo4j-core' require 'database_cleaner/neo4j/transaction' diff --git a/spec/database_cleaner/null_strategy_spec.rb b/spec/database_cleaner/null_strategy_spec.rb index 7fb9332..9ad974b 100644 --- a/spec/database_cleaner/null_strategy_spec.rb +++ b/spec/database_cleaner/null_strategy_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "database_cleaner/null_strategy" module DatabaseCleaner describe NullStrategy do diff --git a/spec/database_cleaner/ohm/truncation_spec.rb b/spec/database_cleaner/ohm/truncation_spec.rb index 8530a12..7ec4ebd 100644 --- a/spec/database_cleaner/ohm/truncation_spec.rb +++ b/spec/database_cleaner/ohm/truncation_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'ohm' require 'database_cleaner/ohm/truncation' diff --git a/spec/database_cleaner/redis/base_spec.rb b/spec/database_cleaner/redis/base_spec.rb index dafe8da..81f90c2 100644 --- a/spec/database_cleaner/redis/base_spec.rb +++ b/spec/database_cleaner/redis/base_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'redis' require 'database_cleaner/redis/base' require 'database_cleaner/shared_strategy' diff --git a/spec/database_cleaner/redis/truncation_spec.rb b/spec/database_cleaner/redis/truncation_spec.rb index 0e7bfb7..9899e9d 100644 --- a/spec/database_cleaner/redis/truncation_spec.rb +++ b/spec/database_cleaner/redis/truncation_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'redis' require 'database_cleaner/redis/truncation' diff --git a/spec/database_cleaner/safeguard_spec.rb b/spec/database_cleaner/safeguard_spec.rb index fa280a8..911b377 100644 --- a/spec/database_cleaner/safeguard_spec.rb +++ b/spec/database_cleaner/safeguard_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'active_record' require 'database_cleaner/active_record/transaction' diff --git a/spec/database_cleaner/sequel/base_spec.rb b/spec/database_cleaner/sequel/base_spec.rb index 9ebc53b..b2b3b73 100644 --- a/spec/database_cleaner/sequel/base_spec.rb +++ b/spec/database_cleaner/sequel/base_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'database_cleaner/sequel/base' require 'database_cleaner/shared_strategy' require 'sequel' diff --git a/spec/database_cleaner/sequel/deletion_spec.rb b/spec/database_cleaner/sequel/deletion_spec.rb index 27f9f7c..08cfade 100644 --- a/spec/database_cleaner/sequel/deletion_spec.rb +++ b/spec/database_cleaner/sequel/deletion_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'database_cleaner/sequel/deletion' require 'database_cleaner/shared_strategy' require 'sequel' diff --git a/spec/database_cleaner/sequel/transaction_spec.rb b/spec/database_cleaner/sequel/transaction_spec.rb index 794481e..85636ac 100644 --- a/spec/database_cleaner/sequel/transaction_spec.rb +++ b/spec/database_cleaner/sequel/transaction_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'database_cleaner/sequel/transaction' require 'database_cleaner/shared_strategy' require 'sequel' diff --git a/spec/database_cleaner/sequel/truncation_spec.rb b/spec/database_cleaner/sequel/truncation_spec.rb index 1845569..975a42e 100644 --- a/spec/database_cleaner/sequel/truncation_spec.rb +++ b/spec/database_cleaner/sequel/truncation_spec.rb @@ -1,4 +1,3 @@ -require 'spec_helper' require 'database_cleaner/sequel/truncation' require 'database_cleaner/shared_strategy' require 'sequel' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6d6f2da..c0ccf24 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,17 +1,4 @@ -require "rubygems" - -require "bundler" -Bundler.setup - -require 'rspec/core' -require 'rspec/mocks' - -#require 'active_record' -#require 'mongo_mapper' - -$:.unshift(File.dirname(__FILE__)) -$:.unshift(File.dirname(__FILE__) + '/../lib') - +require "bundler/setup" require 'database_cleaner' RSpec.configure do |config| From 42052800998de241b7d90815c99aa1ab0c6111ef Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 11:03:58 -0700 Subject: [PATCH 05/14] rspec's new `expect {}` syntax replaces this nicely. --- .../active_record/truncation_spec.rb | 6 +++--- spec/database_cleaner/base_spec.rb | 2 +- .../database_cleaner/couch_potato/truncation_spec.rb | 12 ++++++------ spec/spec_helper.rb | 1 - 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/spec/database_cleaner/active_record/truncation_spec.rb b/spec/database_cleaner/active_record/truncation_spec.rb index 03e683c..65d83e0 100644 --- a/spec/database_cleaner/active_record/truncation_spec.rb +++ b/spec/database_cleaner/active_record/truncation_spec.rb @@ -66,13 +66,13 @@ module DatabaseCleaner end it "should raise an error when :only and :except options are used" do - expect(running { + expect { Truncation.new(:except => ['widgets'], :only => ['widgets']) - }).to raise_error(ArgumentError) + }.to raise_error(ArgumentError) end it "should raise an error when invalid options are provided" do - expect(running { Truncation.new(:foo => 'bar') }).to raise_error(ArgumentError) + expect { Truncation.new(:foo => 'bar') }.to raise_error(ArgumentError) end it "should not truncate views" do diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index 95fcd25..5c9a615 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -74,7 +74,7 @@ module DatabaseCleaner let(:cleaner) { DatabaseCleaner::Base.new :autodetect } it "should raise an error when no ORM is detected" do - expect(running { cleaner }).to raise_error(DatabaseCleaner::NoORMDetected) + expect { cleaner }.to raise_error(DatabaseCleaner::NoORMDetected) end it "should detect ActiveRecord first" do diff --git a/spec/database_cleaner/couch_potato/truncation_spec.rb b/spec/database_cleaner/couch_potato/truncation_spec.rb index e566a6b..c69be10 100644 --- a/spec/database_cleaner/couch_potato/truncation_spec.rb +++ b/spec/database_cleaner/couch_potato/truncation_spec.rb @@ -18,21 +18,21 @@ module DatabaseCleaner end it "should raise an error when the :only option is used" do - expect(running { + expect { Truncation.new(:only => ['document-type']) - }).to raise_error(ArgumentError) + }.to raise_error(ArgumentError) end it "should raise an error when the :except option is used" do - expect(running { + expect { Truncation.new(:except => ['document-type']) - }).to raise_error(ArgumentError) + }.to raise_error(ArgumentError) end it "should raise an error when invalid options are provided" do - expect(running { + expect { Truncation.new(:foo => 'bar') - }).to raise_error(ArgumentError) + }.to raise_error(ArgumentError) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c0ccf24..0725357 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,4 +10,3 @@ RSpec.configure do |config| config.run_all_when_everything_filtered = true end -alias running lambda From 0fbddcc03b00381ca40fc601146f6d4012ef26be Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 11:15:44 -0700 Subject: [PATCH 06/14] replace ad-hoc `ensure_counts` with rspec's `expect({}).to change({})`. --- .../database_cleaner/mongo/truncation_spec.rb | 27 ++++++---------- .../mongo_mapper/truncation_spec.rb | 32 ++++++------------- .../database_cleaner/moped/truncation_spec.rb | 27 ++++++---------- 3 files changed, 27 insertions(+), 59 deletions(-) diff --git a/spec/database_cleaner/mongo/truncation_spec.rb b/spec/database_cleaner/mongo/truncation_spec.rb index 9cb1d3e..1ac3ab5 100644 --- a/spec/database_cleaner/mongo/truncation_spec.rb +++ b/spec/database_cleaner/mongo/truncation_spec.rb @@ -19,15 +19,6 @@ module DatabaseCleaner @connection.drop_database(@test_db) end - def ensure_counts(expected_counts) - # I had to add this sanity_check garbage because I was getting non-determinisc results from mongo at times.. - # very odd and disconcerting... - expected_counts.each do |model_class, expected_count| - actual_count = model_class.count - expect(actual_count).to eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}" - end - end - def create_widget(attrs={}) MongoTest::Widget.new({:name => 'some widget'}.merge(attrs)).save! end @@ -39,9 +30,9 @@ module DatabaseCleaner it "truncates all collections by default" do create_widget create_gadget - ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1) - truncation.clean - ensure_counts(MongoTest::Widget => 0, MongoTest::Gadget => 0) + expect { truncation.clean }.to change { + [MongoTest::Widget.count, MongoTest::Gadget.count] + }.from([1,1]).to([0,0]) end context "when collections are provided to the :only option" do @@ -49,9 +40,9 @@ module DatabaseCleaner it "only truncates the specified collections" do create_widget create_gadget - ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1) - truncation.clean - ensure_counts(MongoTest::Widget => 0, MongoTest::Gadget => 1) + expect { truncation.clean }.to change { + [MongoTest::Widget.count, MongoTest::Gadget.count] + }.from([1,1]).to([0,1]) end end @@ -60,9 +51,9 @@ module DatabaseCleaner it "truncates all but the specified collections" do create_widget create_gadget - ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1) - truncation.clean - ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 0) + expect { truncation.clean }.to change { + [MongoTest::Widget.count, MongoTest::Gadget.count] + }.from([1,1]).to([1,0]) end end diff --git a/spec/database_cleaner/mongo_mapper/truncation_spec.rb b/spec/database_cleaner/mongo_mapper/truncation_spec.rb index e59c147..e65207b 100644 --- a/spec/database_cleaner/mongo_mapper/truncation_spec.rb +++ b/spec/database_cleaner/mongo_mapper/truncation_spec.rb @@ -18,20 +18,6 @@ module DatabaseCleaner ::MongoMapper.connection.drop_database(@test_db) end - def ensure_counts(expected_counts) - # I had to add this sanity_check garbage because I was getting non-determinisc results from mongomapper at times.. - # very odd and disconcerting... - sanity_check = expected_counts.delete(:sanity_check) - begin - expected_counts.each do |model_class, expected_count| - actual_count = model_class.count - expect(actual_count).to eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}" - end - rescue RSpec::Expectations::ExpectationNotMetError => e - raise !sanity_check ? e : RSpec::ExpectationNotMetError::ExpectationNotMetError.new("SANITY CHECK FAILURE! This should never happen here: #{e.message}") - end - end - def create_widget(attrs={}) Widget.new({:name => 'some widget'}.merge(attrs)).save! end @@ -43,18 +29,18 @@ module DatabaseCleaner it "truncates all collections by default" do create_widget create_gadget - ensure_counts(Widget => 1, Gadget => 1, :sanity_check => true) - Truncation.new.clean - ensure_counts(Widget => 0, Gadget => 0) + expect { Truncation.new.clean }.to change { + [Widget.count, Gadget.count] + }.from([1,1]).to([0,0]) end context "when collections are provided to the :only option" do it "only truncates the specified collections" do create_widget create_gadget - ensure_counts(Widget => 1, Gadget => 1, :sanity_check => true) - Truncation.new(:only => ['widgets']).clean - ensure_counts(Widget => 0, Gadget => 1) + expect { Truncation.new(only: ['widgets']).clean }.to change { + [Widget.count, Gadget.count] + }.from([1,1]).to([0,1]) end end @@ -62,9 +48,9 @@ module DatabaseCleaner it "truncates all but the specified collections" do create_widget create_gadget - ensure_counts(Widget => 1, Gadget => 1, :sanity_check => true) - Truncation.new(:except => ['widgets']).clean - ensure_counts(Widget => 1, Gadget => 0) + expect { Truncation.new(except: ['widgets']).clean }.to change { + [Widget.count, Gadget.count] + }.from([1,1]).to([1,0]) end end diff --git a/spec/database_cleaner/moped/truncation_spec.rb b/spec/database_cleaner/moped/truncation_spec.rb index 98dbd4e..956fc67 100644 --- a/spec/database_cleaner/moped/truncation_spec.rb +++ b/spec/database_cleaner/moped/truncation_spec.rb @@ -23,15 +23,6 @@ module DatabaseCleaner @session.command(getlasterror: 1) end - def ensure_counts(expected_counts) - # I had to add this sanity_check garbage because I was getting non-deterministic results from mongo at times.. - # very odd and disconcerting... - expected_counts.each do |model_class, expected_count| - actual_count = model_class.count - expect(actual_count).to eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}" - end - end - def create_widget(attrs={}) MopedTest::Widget.new({:name => 'some widget'}.merge(attrs)).save! end @@ -48,9 +39,9 @@ module DatabaseCleaner create_widget create_gadget create_system - ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1, MopedTest::System => 1) - truncation.clean - ensure_counts(MopedTest::Widget => 0, MopedTest::Gadget => 0, MopedTest::System => 0) + expect { truncation.clean }.to change { + [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] + }.from([1,1,1]).to([0,0,0]) end context "when collections are provided to the :only option" do @@ -58,9 +49,9 @@ module DatabaseCleaner it "only truncates the specified collections" do create_widget create_gadget - ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1) - truncation.clean - ensure_counts(MopedTest::Widget => 0, MopedTest::Gadget => 1) + expect { truncation.clean }.to change { + [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] + }.from([1,1,0]).to([0,1,0]) end end @@ -69,9 +60,9 @@ module DatabaseCleaner it "truncates all but the specified collections" do create_widget create_gadget - ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1) - truncation.clean - ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 0) + expect { truncation.clean }.to change { + [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] + }.from([1,1,0]).to([1,0,0]) end end From 0c44b931a2abab8d8f1727906703a4120ceb9cc2 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 12:06:01 -0700 Subject: [PATCH 07/14] fix more global state spec leakage. --- .../active_record/base_spec.rb | 34 +-- .../truncation/postgresql_spec.rb | 2 +- spec/database_cleaner/base_spec.rb | 169 ++++++--------- spec/database_cleaner/configuration_spec.rb | 194 +++++++++--------- spec/database_cleaner/null_strategy_spec.rb | 34 +-- 5 files changed, 197 insertions(+), 236 deletions(-) diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index 56673b2..ab27d03 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -9,33 +9,35 @@ class FakeModel end end -module DatabaseCleaner - describe ActiveRecord do - it { is_expected.to respond_to(:available_strategies) } +describe DatabaseCleaner::ActiveRecord do + it { is_expected.to respond_to(:available_strategies) } - describe "config_file_location" do - subject { ActiveRecord.config_file_location } - - it "should default to DatabaseCleaner.root / config / database.yml" do - ActiveRecord.config_file_location=nil - expect(DatabaseCleaner).to receive(:app_root).and_return("/path/to") - expect(subject).to eq '/path/to/config/database.yml' - end + describe "config_file_location" do + after do + # prevent global state leakage + DatabaseCleaner::ActiveRecord.config_file_location=nil end + it "should default to DatabaseCleaner.root / config / database.yml" do + DatabaseCleaner::ActiveRecord.config_file_location=nil + expect(DatabaseCleaner).to receive(:app_root).and_return("/path/to") + expect(DatabaseCleaner::ActiveRecord.config_file_location).to eq '/path/to/config/database.yml' + end end +end +module DatabaseCleaner module ActiveRecord class ExampleStrategy - include ::DatabaseCleaner::ActiveRecord::Base + include DatabaseCleaner::ActiveRecord::Base end describe ExampleStrategy do - let :config_location do - '/path/to/config/database.yml' - end + let(:config_location) { '/path/to/config/database.yml' } - before { allow(::DatabaseCleaner::ActiveRecord).to receive(:config_file_location).and_return(config_location) } + before do + allow(DatabaseCleaner::ActiveRecord).to receive(:config_file_location).and_return(config_location) + end it_should_behave_like "a generic strategy" diff --git a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb index fb495ca..316bb98 100644 --- a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb @@ -54,7 +54,7 @@ module ActiveRecord it "should not truncate the tables specified in the :except option" do 2.times { User.create } - ::DatabaseCleaner::ActiveRecord::Truncation.new(:except => ['users']).clean + DatabaseCleaner::ActiveRecord::Truncation.new(:except => ['users']).clean expect( User.count ).to eq 2 end end diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index 5c9a615..ba3f9c6 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -15,60 +15,17 @@ module DatabaseCleaner } describe "autodetect" do - - #Cache all ORMs, we'll need them later but not now. - before(:all) do - Temp_AR = ::ActiveRecord if defined?(::ActiveRecord) and not defined?(Temp_AR) - Temp_DM = ::DataMapper if defined?(::DataMapper) and not defined?(Temp_DM) - Temp_MM = ::MongoMapper if defined?(::MongoMapper) and not defined?(Temp_MM) - Temp_MO = ::Mongoid if defined?(::Mongoid) and not defined?(Temp_MO) - Temp_CP = ::CouchPotato if defined?(::CouchPotato) and not defined?(Temp_CP) - Temp_SQ = ::Sequel if defined?(::Sequel) and not defined?(Temp_SQ) - Temp_MP = ::Moped if defined?(::Moped) and not defined?(Temp_MP) - Temp_RS = ::Redis if defined?(::Redis) and not defined?(Temp_RS) - Temp_OH = ::Ohm if defined?(::Ohm) and not defined?(Temp_OH) - Temp_NJ = ::Neo4j if defined?(::Neo4j) and not defined?(Temp_NJ) - end - - #Remove all ORM mocks and restore from cache - after(:all) do - Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord) - Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper) - Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper) - Object.send(:remove_const, 'Mongoid') if defined?(::Mongoid) - Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato) - Object.send(:remove_const, 'Sequel') if defined?(::Sequel) - Object.send(:remove_const, 'Moped') if defined?(::Moped) - Object.send(:remove_const, 'Ohm') if defined?(::Ohm) - Object.send(:remove_const, 'Redis') if defined?(::Redis) - Object.send(:remove_const, 'Neo4j') if defined?(::Neo4j) - - - # Restore ORMs - ::ActiveRecord = Temp_AR if defined? Temp_AR - ::DataMapper = Temp_DM if defined? Temp_DM - ::MongoMapper = Temp_MM if defined? Temp_MM - ::Mongoid = Temp_MO if defined? Temp_MO - ::CouchPotato = Temp_CP if defined? Temp_CP - ::Sequel = Temp_SQ if defined? Temp_SQ - ::Moped = Temp_MP if defined? Temp_MP - ::Ohm = Temp_OH if defined? Temp_OH - ::Redis = Temp_RS if defined? Temp_RS - ::Neo4j = Temp_NJ if defined? Temp_NJ - end - - #reset the orm mocks - before(:each) do - Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord) - Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper) - Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper) - Object.send(:remove_const, 'Mongoid') if defined?(::Mongoid) - Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato) - Object.send(:remove_const, 'Sequel') if defined?(::Sequel) - Object.send(:remove_const, 'Moped') if defined?(::Moped) - Object.send(:remove_const, 'Ohm') if defined?(::Ohm) - Object.send(:remove_const, 'Redis') if defined?(::Redis) - Object.send(:remove_const, 'Neo4j') if defined?(::Neo4j) + before do + hide_const("ActiveRecord") + hide_const("DataMapper") + hide_const("MongoMapper") + hide_const("Mongoid") + hide_const("CouchPotato") + hide_const("Sequel") + hide_const("Moped") + hide_const("Redis") + hide_const("Ohm") + hide_const("Neo4j") end let(:cleaner) { DatabaseCleaner::Base.new :autodetect } @@ -78,112 +35,112 @@ module DatabaseCleaner end it "should detect ActiveRecord first" do - Object.const_set('ActiveRecord','Actively mocking records.') - Object.const_set('DataMapper', 'Mapping data mocks') - Object.const_set('MongoMapper', 'Mapping mock mongos') - Object.const_set('Mongoid', 'Mongoid mock') - Object.const_set('CouchPotato', 'Couching mock potatos') - Object.const_set('Sequel', 'Sequel mock') - Object.const_set('Moped', 'Moped mock') - Object.const_set('Ohm', 'Ohm mock') - Object.const_set('Redis', 'Redis mock') - Object.const_set('Neo4j', 'Neo4j mock') + stub_const('ActiveRecord','Actively mocking records.') + stub_const('DataMapper', 'Mapping data mocks') + stub_const('MongoMapper', 'Mapping mock mongos') + stub_const('Mongoid', 'Mongoid mock') + stub_const('CouchPotato', 'Couching mock potatos') + stub_const('Sequel', 'Sequel mock') + stub_const('Moped', 'Moped mock') + stub_const('Ohm', 'Ohm mock') + stub_const('Redis', 'Redis mock') + stub_const('Neo4j', 'Neo4j mock') expect(cleaner.orm).to eq :active_record expect(cleaner).to be_auto_detected end it "should detect DataMapper second" do - Object.const_set('DataMapper', 'Mapping data mocks') - Object.const_set('MongoMapper', 'Mapping mock mongos') - Object.const_set('Mongoid', 'Mongoid mock') - Object.const_set('CouchPotato', 'Couching mock potatos') - Object.const_set('Sequel', 'Sequel mock') - Object.const_set('Moped', 'Moped mock') - Object.const_set('Ohm', 'Ohm mock') - Object.const_set('Redis', 'Redis mock') - Object.const_set('Neo4j', 'Neo4j mock') + stub_const('DataMapper', 'Mapping data mocks') + stub_const('MongoMapper', 'Mapping mock mongos') + stub_const('Mongoid', 'Mongoid mock') + stub_const('CouchPotato', 'Couching mock potatos') + stub_const('Sequel', 'Sequel mock') + stub_const('Moped', 'Moped mock') + stub_const('Ohm', 'Ohm mock') + stub_const('Redis', 'Redis mock') + stub_const('Neo4j', 'Neo4j mock') expect(cleaner.orm).to eq :data_mapper expect(cleaner).to be_auto_detected end it "should detect MongoMapper third" do - Object.const_set('MongoMapper', 'Mapping mock mongos') - Object.const_set('Mongoid', 'Mongoid mock') - Object.const_set('CouchPotato', 'Couching mock potatos') - Object.const_set('Sequel', 'Sequel mock') - Object.const_set('Moped', 'Moped mock') - Object.const_set('Ohm', 'Ohm mock') - Object.const_set('Redis', 'Redis mock') - Object.const_set('Neo4j', 'Neo4j mock') + stub_const('MongoMapper', 'Mapping mock mongos') + stub_const('Mongoid', 'Mongoid mock') + stub_const('CouchPotato', 'Couching mock potatos') + stub_const('Sequel', 'Sequel mock') + stub_const('Moped', 'Moped mock') + stub_const('Ohm', 'Ohm mock') + stub_const('Redis', 'Redis mock') + stub_const('Neo4j', 'Neo4j mock') expect(cleaner.orm).to eq :mongo_mapper expect(cleaner).to be_auto_detected end it "should detect Mongoid fourth" do - Object.const_set('Mongoid', 'Mongoid mock') - Object.const_set('CouchPotato', 'Couching mock potatos') - Object.const_set('Sequel', 'Sequel mock') - Object.const_set('Moped', 'Moped mock') - Object.const_set('Ohm', 'Ohm mock') - Object.const_set('Redis', 'Redis mock') - Object.const_set('Neo4j', 'Neo4j mock') + stub_const('Mongoid', 'Mongoid mock') + stub_const('CouchPotato', 'Couching mock potatos') + stub_const('Sequel', 'Sequel mock') + stub_const('Moped', 'Moped mock') + stub_const('Ohm', 'Ohm mock') + stub_const('Redis', 'Redis mock') + stub_const('Neo4j', 'Neo4j mock') expect(cleaner.orm).to eq :mongoid expect(cleaner).to be_auto_detected end it "should detect CouchPotato fifth" do - Object.const_set('CouchPotato', 'Couching mock potatos') - Object.const_set('Sequel', 'Sequel mock') - Object.const_set('Moped', 'Moped mock') - Object.const_set('Ohm', 'Ohm mock') - Object.const_set('Redis', 'Redis mock') - Object.const_set('Neo4j', 'Neo4j mock') + stub_const('CouchPotato', 'Couching mock potatos') + stub_const('Sequel', 'Sequel mock') + stub_const('Moped', 'Moped mock') + stub_const('Ohm', 'Ohm mock') + stub_const('Redis', 'Redis mock') + stub_const('Neo4j', 'Neo4j mock') expect(cleaner.orm).to eq :couch_potato expect(cleaner).to be_auto_detected end it "should detect Sequel sixth" do - Object.const_set('Sequel', 'Sequel mock') - Object.const_set('Moped', 'Moped mock') - Object.const_set('Ohm', 'Ohm mock') - Object.const_set('Redis', 'Redis mock') - Object.const_set('Neo4j', 'Neo4j mock') + stub_const('Sequel', 'Sequel mock') + stub_const('Moped', 'Moped mock') + stub_const('Ohm', 'Ohm mock') + stub_const('Redis', 'Redis mock') + stub_const('Neo4j', 'Neo4j mock') expect(cleaner.orm).to eq :sequel expect(cleaner).to be_auto_detected end it 'detects Moped seventh' do - Object.const_set('Moped', 'Moped mock') + stub_const('Moped', 'Moped mock') expect(cleaner.orm).to eq :moped expect(cleaner).to be_auto_detected end it 'detects Ohm eighth' do - Object.const_set('Ohm', 'Ohm mock') - Object.const_set('Redis', 'Redis mock') - Object.const_set('Neo4j', 'Neo4j mock') + stub_const('Ohm', 'Ohm mock') + stub_const('Redis', 'Redis mock') + stub_const('Neo4j', 'Neo4j mock') expect(cleaner.orm).to eq :ohm expect(cleaner).to be_auto_detected end it 'detects Redis ninth' do - Object.const_set('Redis', 'Redis mock') - Object.const_set('Neo4j', 'Neo4j mock') + stub_const('Redis', 'Redis mock') + stub_const('Neo4j', 'Neo4j mock') expect(cleaner.orm).to eq :redis expect(cleaner).to be_auto_detected end it 'detects Neo4j tenth' do - Object.const_set('Neo4j', 'Neo4j mock') + stub_const('Neo4j', 'Neo4j mock') expect(cleaner.orm).to eq :neo4j expect(cleaner).to be_auto_detected diff --git a/spec/database_cleaner/configuration_spec.rb b/spec/database_cleaner/configuration_spec.rb index 2ed7ada..072b135 100644 --- a/spec/database_cleaner/configuration_spec.rb +++ b/spec/database_cleaner/configuration_spec.rb @@ -10,6 +10,7 @@ module DatabaseCleaner def reset @cleaners = nil @connections = nil + @app_root = nil end # hackey, hack.. connections needs to stick around until I can properly deprecate the API def connections_stub(array) @@ -19,192 +20,192 @@ module DatabaseCleaner end end -describe ::DatabaseCleaner do - before(:each) { ::DatabaseCleaner.reset } +describe DatabaseCleaner do + before(:each) { DatabaseCleaner.reset } context "orm specification" do it "should not accept unrecognised orms" do - expect { ::DatabaseCleaner[nil] }.to raise_error(::DatabaseCleaner::NoORMDetected) + expect { DatabaseCleaner[nil] }.to raise_error(DatabaseCleaner::NoORMDetected) end it "should accept :active_record" do - cleaner = ::DatabaseCleaner[:active_record] - expect(cleaner).to be_a(::DatabaseCleaner::Base) + cleaner = DatabaseCleaner[:active_record] + expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner.orm).to eq :active_record - expect(::DatabaseCleaner.connections.size).to eq 1 + expect(DatabaseCleaner.connections.size).to eq 1 end it "should accept :data_mapper" do - cleaner = ::DatabaseCleaner[:data_mapper] - expect(cleaner).to be_a(::DatabaseCleaner::Base) + cleaner = DatabaseCleaner[:data_mapper] + expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner.orm).to eq :data_mapper - expect(::DatabaseCleaner.connections.size).to eq 1 + expect(DatabaseCleaner.connections.size).to eq 1 end it "should accept :mongo_mapper" do - cleaner = ::DatabaseCleaner[:mongo_mapper] - expect(cleaner).to be_a(::DatabaseCleaner::Base) + cleaner = DatabaseCleaner[:mongo_mapper] + expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner.orm).to eq :mongo_mapper - expect(::DatabaseCleaner.connections.size).to eq 1 + expect(DatabaseCleaner.connections.size).to eq 1 end it "should accept :couch_potato" do - cleaner = ::DatabaseCleaner[:couch_potato] - expect(cleaner).to be_a(::DatabaseCleaner::Base) + cleaner = DatabaseCleaner[:couch_potato] + expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner.orm).to eq :couch_potato - expect(::DatabaseCleaner.connections.size).to eq 1 + expect(DatabaseCleaner.connections.size).to eq 1 end it "should accept :moped" do - cleaner = ::DatabaseCleaner[:moped] - expect(cleaner).to be_a(::DatabaseCleaner::Base) + cleaner = DatabaseCleaner[:moped] + expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner.orm).to eq :moped - expect(::DatabaseCleaner.connections.size).to eq 1 + expect(DatabaseCleaner.connections.size).to eq 1 end it 'accepts :ohm' do - cleaner = ::DatabaseCleaner[:ohm] - expect(cleaner).to be_a(::DatabaseCleaner::Base) + cleaner = DatabaseCleaner[:ohm] + expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner.orm).to eq :ohm - expect(::DatabaseCleaner.connections.size).to eq 1 + expect(DatabaseCleaner.connections.size).to eq 1 end end it "should accept multiple orm's" do - ::DatabaseCleaner[:couch_potato] - ::DatabaseCleaner[:data_mapper] - expect(::DatabaseCleaner.connections.size).to eq 2 - expect(::DatabaseCleaner.connections[0].orm).to eq :couch_potato - expect(::DatabaseCleaner.connections[1].orm).to eq :data_mapper + DatabaseCleaner[:couch_potato] + DatabaseCleaner[:data_mapper] + expect(DatabaseCleaner.connections.size).to eq 2 + expect(DatabaseCleaner.connections[0].orm).to eq :couch_potato + expect(DatabaseCleaner.connections[1].orm).to eq :data_mapper end context "connection/db specification" do it "should accept a connection parameter and store it" do - cleaner = ::DatabaseCleaner[:active_record, {:connection => :first_connection}] - expect(cleaner).to be_a(::DatabaseCleaner::Base) + cleaner = DatabaseCleaner[:active_record, {:connection => :first_connection}] + expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner.orm).to eq :active_record expect(cleaner.db).to eq :first_connection end it "should accept multiple connections for a single orm" do - ::DatabaseCleaner[:data_mapper,{:connection => :first_db}] - ::DatabaseCleaner[:data_mapper,{:connection => :second_db}] - expect(::DatabaseCleaner.connections.size).to eq 2 - expect(::DatabaseCleaner.connections[0].orm).to eq :data_mapper - expect(::DatabaseCleaner.connections[0].db).to eq :first_db - expect(::DatabaseCleaner.connections[1].orm).to eq :data_mapper - expect(::DatabaseCleaner.connections[1].db).to eq :second_db + DatabaseCleaner[:data_mapper,{:connection => :first_db}] + DatabaseCleaner[:data_mapper,{:connection => :second_db}] + expect(DatabaseCleaner.connections.size).to eq 2 + expect(DatabaseCleaner.connections[0].orm).to eq :data_mapper + expect(DatabaseCleaner.connections[0].db).to eq :first_db + expect(DatabaseCleaner.connections[1].orm).to eq :data_mapper + expect(DatabaseCleaner.connections[1].db).to eq :second_db end it "should accept multiple connections and multiple orms" do - ::DatabaseCleaner[:data_mapper, {:connection => :first_db} ] - ::DatabaseCleaner[:active_record,{:connection => :second_db}] - ::DatabaseCleaner[:active_record,{:connection => :first_db} ] - ::DatabaseCleaner[:data_mapper, {:connection => :second_db}] + DatabaseCleaner[:data_mapper, {:connection => :first_db} ] + DatabaseCleaner[:active_record,{:connection => :second_db}] + DatabaseCleaner[:active_record,{:connection => :first_db} ] + DatabaseCleaner[:data_mapper, {:connection => :second_db}] - expect(::DatabaseCleaner.connections.size).to eq 4 + expect(DatabaseCleaner.connections.size).to eq 4 - expect(::DatabaseCleaner.connections[0].orm).to eq :data_mapper - expect(::DatabaseCleaner.connections[0].db).to eq :first_db + expect(DatabaseCleaner.connections[0].orm).to eq :data_mapper + expect(DatabaseCleaner.connections[0].db).to eq :first_db - expect(::DatabaseCleaner.connections[1].orm).to eq :active_record - expect(::DatabaseCleaner.connections[1].db).to eq :second_db + expect(DatabaseCleaner.connections[1].orm).to eq :active_record + expect(DatabaseCleaner.connections[1].db).to eq :second_db - expect(::DatabaseCleaner.connections[2].orm).to eq :active_record - expect(::DatabaseCleaner.connections[2].db).to eq :first_db + expect(DatabaseCleaner.connections[2].orm).to eq :active_record + expect(DatabaseCleaner.connections[2].db).to eq :first_db - expect(::DatabaseCleaner.connections[3].orm).to eq :data_mapper - expect(::DatabaseCleaner.connections[3].db).to eq :second_db + expect(DatabaseCleaner.connections[3].orm).to eq :data_mapper + expect(DatabaseCleaner.connections[3].db).to eq :second_db end end context "connection/db retrieval" do it "should retrieve a db rather than create a new one" do pending - connection = ::DatabaseCleaner[:active_record].strategy = :truncation - expect(::DatabaseCleaner[:active_record]).to eq connection + connection = DatabaseCleaner[:active_record].strategy = :truncation + expect(DatabaseCleaner[:active_record]).to eq connection end end context "class methods" do - subject { ::DatabaseCleaner } + subject { DatabaseCleaner } it "should give me a default (autodetection) databasecleaner by default" do cleaner = double("cleaner").as_null_object - allow(::DatabaseCleaner::Base).to receive(:new).and_return(cleaner) + allow(DatabaseCleaner::Base).to receive(:new).and_return(cleaner) - expect(::DatabaseCleaner.connections).to eq [cleaner] + expect(DatabaseCleaner.connections).to eq [cleaner] end end context "single orm single connection" do - let(:connection) { ::DatabaseCleaner.connections.first } + let(:connection) { DatabaseCleaner.connections.first } it "should proxy strategy=" do - stratagum = double("stratagum") - expect(connection).to receive(:strategy=).with(stratagum) - ::DatabaseCleaner.strategy = stratagum + stratagem = double("stratagem") + expect(connection).to receive(:strategy=).with(stratagem) + DatabaseCleaner.strategy = stratagem end it "should proxy orm=" do orm = double("orm") expect(connection).to receive(:orm=).with(orm) - ::DatabaseCleaner.orm = orm + DatabaseCleaner.orm = orm end it "should proxy start" do expect(connection).to receive(:start) - ::DatabaseCleaner.start + DatabaseCleaner.start end it "should proxy clean" do expect(connection).to receive(:clean) - ::DatabaseCleaner.clean + DatabaseCleaner.clean end it 'should proxy cleaning' do expect(connection).to receive(:cleaning) - ::DatabaseCleaner.cleaning { } + DatabaseCleaner.cleaning { } end it "should proxy clean_with" do stratagem = double("stratgem") expect(connection).to receive(:clean_with).with(stratagem, {}) - ::DatabaseCleaner.clean_with stratagem, {} + DatabaseCleaner.clean_with stratagem, {} end end context "multiple connections" do - #these are relativly simple, all we need to do is make sure all connections are cleaned/started/cleaned_with appropriatly. + # these are relatively simple, all we need to do is make sure all connections are cleaned/started/cleaned_with appropriately. context "simple proxy methods" do let(:active_record) { double("active_mock") } let(:data_mapper) { double("data_mock") } before(:each) do - allow(::DatabaseCleaner).to receive(:connections).and_return([active_record,data_mapper]) + allow(DatabaseCleaner).to receive(:connections).and_return([active_record,data_mapper]) end it "should proxy orm to all connections" do expect(active_record).to receive(:orm=) expect(data_mapper).to receive(:orm=) - ::DatabaseCleaner.orm = double("orm") + DatabaseCleaner.orm = double("orm") end it "should proxy start to all connections" do expect(active_record).to receive(:start) expect(data_mapper).to receive(:start) - ::DatabaseCleaner.start + DatabaseCleaner.start end it "should proxy clean to all connections" do expect(active_record).to receive(:clean) expect(data_mapper).to receive(:clean) - ::DatabaseCleaner.clean + DatabaseCleaner.clean end it "should initiate cleaning on each connection, yield, and finish cleaning each connection" do @@ -218,7 +219,7 @@ describe ::DatabaseCleaner do end end - ::DatabaseCleaner.cleaning do + DatabaseCleaner.cleaning do expect(active_record.started).to eq(true) expect(data_mapper.started).to eq(true) expect(active_record.cleaned).to eq(nil) @@ -234,7 +235,7 @@ describe ::DatabaseCleaner do expect(active_record).to receive(:clean_with).with(stratagem) expect(data_mapper).to receive(:clean_with).with(stratagem) - ::DatabaseCleaner.clean_with stratagem + DatabaseCleaner.clean_with stratagem end end @@ -247,7 +248,7 @@ describe ::DatabaseCleaner do active_record_2 = double("active_mock_on_db_two").as_null_object data_mapper_1 = double("data_mock_on_db_one").as_null_object - ::DatabaseCleaner.connections_stub [active_record_1,active_record_2,data_mapper_1] + DatabaseCleaner.connections_stub [active_record_1,active_record_2,data_mapper_1] expect(active_record_1).to receive(:orm=).with(:data_mapper) expect(active_record_2).to receive(:orm=).with(:data_mapper) @@ -255,9 +256,9 @@ describe ::DatabaseCleaner do expect(active_record_1).to receive(:==).with(data_mapper_1).and_return(true) - expect(::DatabaseCleaner.connections.size).to eq 3 - ::DatabaseCleaner.orm = :data_mapper - expect(::DatabaseCleaner.connections.size).to eq 2 + expect(DatabaseCleaner.connections.size).to eq 3 + DatabaseCleaner.orm = :data_mapper + expect(DatabaseCleaner.connections.size).to eq 2 end it "should proxy strategy to all connections and remove duplicate connections" do @@ -265,16 +266,16 @@ describe ::DatabaseCleaner do active_record_2 = double("active_mock_strategy_two").as_null_object strategy = double("strategy") - ::DatabaseCleaner.connections_stub [active_record_1,active_record_2] + DatabaseCleaner.connections_stub [active_record_1,active_record_2] expect(active_record_1).to receive(:strategy=).with(strategy) expect(active_record_2).to receive(:strategy=).with(strategy) expect(active_record_1).to receive(:==).with(active_record_2).and_return(true) - expect(::DatabaseCleaner.connections.size).to eq 2 - ::DatabaseCleaner.strategy = strategy - expect(::DatabaseCleaner.connections.size).to eq 1 + expect(DatabaseCleaner.connections.size).to eq 2 + DatabaseCleaner.strategy = strategy + expect(DatabaseCleaner.connections.size).to eq 1 end end end @@ -284,10 +285,10 @@ describe ::DatabaseCleaner do orm = double("orm") connection = double("a datamapper connection", :orm => orm ) - ::DatabaseCleaner.connections_stub [connection,connection,connection] + DatabaseCleaner.connections_stub [connection,connection,connection] - ::DatabaseCleaner.remove_duplicates - expect(::DatabaseCleaner.connections.size).to eq 1 + DatabaseCleaner.remove_duplicates + expect(DatabaseCleaner.connections.size).to eq 1 end end @@ -303,42 +304,43 @@ describe ::DatabaseCleaner do end describe "orm_module" do - subject { ::DatabaseCleaner } + subject { DatabaseCleaner } + + let(:mod) { double } it "should return DatabaseCleaner::ActiveRecord for :active_record" do - ::DatabaseCleaner::ActiveRecord = double("ar module") unless defined? ::DatabaseCleaner::ActiveRecord - expect(subject.orm_module(:active_record)).to eq DatabaseCleaner::ActiveRecord + stub_const "DatabaseCleaner::ActiveRecord", mod + expect(subject.orm_module(:active_record)).to eq mod end it "should return DatabaseCleaner::DataMapper for :data_mapper" do - ::DatabaseCleaner::DataMapper = double("dm module") unless defined? ::DatabaseCleaner::DataMapper - expect(subject.orm_module(:data_mapper)).to eq DatabaseCleaner::DataMapper + stub_const "DatabaseCleaner::DataMapper", mod + expect(subject.orm_module(:data_mapper)).to eq mod end it "should return DatabaseCleaner::MongoMapper for :mongo_mapper" do - ::DatabaseCleaner::MongoMapper = double("mm module") unless defined? ::DatabaseCleaner::MongoMapper - expect(subject.orm_module(:mongo_mapper)).to eq DatabaseCleaner::MongoMapper + stub_const "DatabaseCleaner::MongoMapper", mod + expect(subject.orm_module(:mongo_mapper)).to eq mod end it "should return DatabaseCleaner::Mongoid for :mongoid" do - ::DatabaseCleaner::Mongoid = double("mongoid module") unless defined? ::DatabaseCleaner::Mongoid - expect(subject.orm_module(:mongoid)).to eq DatabaseCleaner::Mongoid + stub_const "DatabaseCleaner::Mongoid", mod + expect(subject.orm_module(:mongoid)).to eq mod end it "should return DatabaseCleaner::Mongo for :mongo" do - ::DatabaseCleaner::Mongo = double("mongo module") unless defined? ::DatabaseCleaner::Mongo - expect(subject.orm_module(:mongo)).to eq DatabaseCleaner::Mongo + stub_const "DatabaseCleaner::Mongo", mod + expect(subject.orm_module(:mongo)).to eq mod end it "should return DatabaseCleaner::CouchPotato for :couch_potato" do - ::DatabaseCleaner::CouchPotato = double("cp module") unless defined? ::DatabaseCleaner::CouchPotato - expect(subject.orm_module(:couch_potato)).to eq DatabaseCleaner::CouchPotato + stub_const "DatabaseCleaner::CouchPotato", mod + expect(subject.orm_module(:couch_potato)).to eq mod end it "should return DatabaseCleaner::Neo4j for :neo4j" do - ::DatabaseCleaner::Neo4j = double("nj module") unless defined? ::DatabaseCleaner::Neo4j - expect(subject.orm_module(:neo4j)).to eq DatabaseCleaner::Neo4j + stub_const "DatabaseCleaner::Neo4j", mod + expect(subject.orm_module(:neo4j)).to eq mod end - end end diff --git a/spec/database_cleaner/null_strategy_spec.rb b/spec/database_cleaner/null_strategy_spec.rb index 9ad974b..0b982f0 100644 --- a/spec/database_cleaner/null_strategy_spec.rb +++ b/spec/database_cleaner/null_strategy_spec.rb @@ -2,27 +2,27 @@ require "database_cleaner/null_strategy" module DatabaseCleaner describe NullStrategy do - it 'responds to .start' do - expect { NullStrategy.start }.not_to raise_error + it 'responds to .start' do + expect { NullStrategy.start }.not_to raise_error + end + + it 'responds to .clean' do + expect { NullStrategy.clean }.not_to raise_error + end + + describe '.cleaning' do + it 'fails without a block' do + expect { NullStrategy.cleaning }.to raise_error(LocalJumpError) end - it 'responds to .clean' do - expect { NullStrategy.clean }.not_to raise_error - end + it 'no-ops with a block' do + effect = double + expect(effect).to receive(:occur).once - describe '.cleaning' do - it 'fails without a block' do - expect { NullStrategy.cleaning }.to raise_error(LocalJumpError) - end - - it 'no-ops with a block' do - effect = double - expect(effect).to receive(:occur).once - - NullStrategy.cleaning do - effect.occur - end + NullStrategy.cleaning do + effect.occur end end + end end end From ec82904231a8194d010db8c56aa89dcd556daef8 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 11:45:17 -0700 Subject: [PATCH 08/14] refactor mongo specs. --- spec/database_cleaner/mongo/mongo_examples.rb | 12 +-- .../database_cleaner/mongo/truncation_spec.rb | 89 +++++++---------- .../mongo_mapper/mongo_examples.rb | 16 +-- .../mongo_mapper/truncation_spec.rb | 85 +++++++--------- spec/database_cleaner/moped/moped_examples.rb | 8 +- .../database_cleaner/moped/truncation_spec.rb | 99 ++++++++----------- 6 files changed, 133 insertions(+), 176 deletions(-) diff --git a/spec/database_cleaner/mongo/mongo_examples.rb b/spec/database_cleaner/mongo/mongo_examples.rb index d6fc4de..0360294 100644 --- a/spec/database_cleaner/mongo/mongo_examples.rb +++ b/spec/database_cleaner/mongo/mongo_examples.rb @@ -1,13 +1,13 @@ module MongoTest - class ThingBase + class Base def self.collection - @connection ||= ::Mongo::Connection.new('127.0.0.1') + @connection ||= Mongo::Connection.new('127.0.0.1') @db ||= @connection.db('database_cleaner_specs') - @mongo ||= @db.collection(name) || @db.create_collection(name) + @collection ||= @db.collection(name) || @db.create_collection(name) end def self.count - @mongo.count + @collection.count end def initialize(attrs={}) @@ -19,8 +19,8 @@ module MongoTest end end - class Widget < ThingBase + class Widget < Base end - class Gadget < ThingBase + class Gadget < Base end end diff --git a/spec/database_cleaner/mongo/truncation_spec.rb b/spec/database_cleaner/mongo/truncation_spec.rb index 1ac3ab5..922648d 100644 --- a/spec/database_cleaner/mongo/truncation_spec.rb +++ b/spec/database_cleaner/mongo/truncation_spec.rb @@ -2,62 +2,49 @@ require 'mongo' require 'database_cleaner/mongo/truncation' require File.dirname(__FILE__) + '/mongo_examples' -module DatabaseCleaner - module Mongo +describe DatabaseCleaner::Mongo::Truncation do + around do |example| + connection = Mongo::Connection.new('127.0.0.1') + db_name = 'database_cleaner_specs' + db = connection.db(db_name) + subject.db = db - describe Truncation do - let(:args) {{}} - let(:truncation) { described_class.new(args).tap { |t| t.db=@db } } - #doing this in the file root breaks autospec, doing it before(:all) just fails the specs - before(:all) do - @connection = ::Mongo::Connection.new('127.0.0.1') - @test_db = 'database_cleaner_specs' - @db = @connection.db(@test_db) - end + example.run - after(:each) do - @connection.drop_database(@test_db) - end + connection.drop_database(db_name) + end - def create_widget(attrs={}) - MongoTest::Widget.new({:name => 'some widget'}.merge(attrs)).save! - end - - def create_gadget(attrs={}) - MongoTest::Gadget.new({:name => 'some gadget'}.merge(attrs)).save! - end - - it "truncates all collections by default" do - create_widget - create_gadget - expect { truncation.clean }.to change { - [MongoTest::Widget.count, MongoTest::Gadget.count] - }.from([1,1]).to([0,0]) - end - - context "when collections are provided to the :only option" do - let(:args) {{:only => ['MongoTest::Widget']}} - it "only truncates the specified collections" do - create_widget - create_gadget - expect { truncation.clean }.to change { - [MongoTest::Widget.count, MongoTest::Gadget.count] - }.from([1,1]).to([0,1]) - end - end - - context "when collections are provided to the :except option" do - let(:args) {{:except => ['MongoTest::Widget']}} - it "truncates all but the specified collections" do - create_widget - create_gadget - expect { truncation.clean }.to change { - [MongoTest::Widget.count, MongoTest::Gadget.count] - }.from([1,1]).to([1,0]) - end - end + before do + MongoTest::Widget.new(name: 'some widget').save! + MongoTest::Gadget.new(name: 'some gadget').save! + end + context "by default" do + it "truncates all collections" do + expect { subject.clean }.to change { + [MongoTest::Widget.count, MongoTest::Gadget.count] + }.from([1,1]).to([0,0]) end + end + context "when collections are provided to the :only option" do + subject { described_class.new(only: ['MongoTest::Widget']) } + + it "only truncates the specified collections" do + expect { subject.clean }.to change { + [MongoTest::Widget.count, MongoTest::Gadget.count] + }.from([1,1]).to([0,1]) + end + end + + context "when collections are provided to the :except option" do + subject { described_class.new(except: ['MongoTest::Widget']) } + + it "truncates all but the specified collections" do + expect { subject.clean }.to change { + [MongoTest::Widget.count, MongoTest::Gadget.count] + }.from([1,1]).to([1,0]) + end end end + diff --git a/spec/database_cleaner/mongo_mapper/mongo_examples.rb b/spec/database_cleaner/mongo_mapper/mongo_examples.rb index 44637ea..f0b8a73 100644 --- a/spec/database_cleaner/mongo_mapper/mongo_examples.rb +++ b/spec/database_cleaner/mongo_mapper/mongo_examples.rb @@ -1,8 +1,10 @@ -class Widget - include ::MongoMapper::Document - key :name, String -end -class Gadget - include ::MongoMapper::Document - key :name, String +module MongoMapperTest + class Widget + include ::MongoMapper::Document + key :name, String + end + class Gadget + include ::MongoMapper::Document + key :name, String + end end diff --git a/spec/database_cleaner/mongo_mapper/truncation_spec.rb b/spec/database_cleaner/mongo_mapper/truncation_spec.rb index e65207b..211d3b6 100644 --- a/spec/database_cleaner/mongo_mapper/truncation_spec.rb +++ b/spec/database_cleaner/mongo_mapper/truncation_spec.rb @@ -2,59 +2,48 @@ require 'mongo_mapper' require 'database_cleaner/mongo_mapper/truncation' require File.dirname(__FILE__) + '/mongo_examples' -module DatabaseCleaner - module MongoMapper +describe DatabaseCleaner::MongoMapper::Truncation do + around do |example| + MongoMapper.connection = Mongo::Connection.new('127.0.0.1') + db_name = 'database_cleaner_specs' + MongoMapper.database = db_name - describe Truncation do + example.run - #doing this in the file root breaks autospec, doing it before(:all) just fails the specs - before(:all) do - ::MongoMapper.connection = ::Mongo::Connection.new('127.0.0.1') - @test_db = 'database_cleaner_specs' - ::MongoMapper.database = @test_db - end + MongoMapper.connection.drop_database(db_name) + end - before(:each) do - ::MongoMapper.connection.drop_database(@test_db) - end - - def create_widget(attrs={}) - Widget.new({:name => 'some widget'}.merge(attrs)).save! - end - - def create_gadget(attrs={}) - Gadget.new({:name => 'some gadget'}.merge(attrs)).save! - end - - it "truncates all collections by default" do - create_widget - create_gadget - expect { Truncation.new.clean }.to change { - [Widget.count, Gadget.count] - }.from([1,1]).to([0,0]) - end - - context "when collections are provided to the :only option" do - it "only truncates the specified collections" do - create_widget - create_gadget - expect { Truncation.new(only: ['widgets']).clean }.to change { - [Widget.count, Gadget.count] - }.from([1,1]).to([0,1]) - end - end - - context "when collections are provided to the :except option" do - it "truncates all but the specified collections" do - create_widget - create_gadget - expect { Truncation.new(except: ['widgets']).clean }.to change { - [Widget.count, Gadget.count] - }.from([1,1]).to([1,0]) - end - end + before do + MongoMapperTest::Widget.new(name: 'some widget').save! + MongoMapperTest::Gadget.new(name: 'some gadget').save! + end + context "by default" do + it "truncates all collections" do + expect { subject.clean }.to change { + [MongoMapperTest::Widget.count, MongoMapperTest::Gadget.count] + }.from([1,1]).to([0,0]) end + end + context "when collections are provided to the :only option" do + subject { described_class.new(only: ['mongo_mapper_test.widgets']) } + + it "only truncates the specified collections" do + expect { subject.clean }.to change { + [MongoMapperTest::Widget.count, MongoMapperTest::Gadget.count] + }.from([1,1]).to([0,1]) + end + end + + context "when collections are provided to the :except option" do + subject { described_class.new(except: ['mongo_mapper_test.widgets']) } + + it "truncates all but the specified collections" do + expect { subject.clean }.to change { + [MongoMapperTest::Widget.count, MongoMapperTest::Gadget.count] + }.from([1,1]).to([1,0]) + end end end + diff --git a/spec/database_cleaner/moped/moped_examples.rb b/spec/database_cleaner/moped/moped_examples.rb index fe8a96d..a144850 100644 --- a/spec/database_cleaner/moped/moped_examples.rb +++ b/spec/database_cleaner/moped/moped_examples.rb @@ -1,5 +1,5 @@ module MopedTest - class ThingBase + class Base def self.collection @db ||= 'database_cleaner_specs' @session ||= ::Moped::Session.new(['127.0.0.1:27017'], database: @db) @@ -19,11 +19,11 @@ module MopedTest end end - class Widget < ThingBase + class Widget < Base end - class Gadget < ThingBase + class Gadget < Base end - class System < ThingBase + class System < Base def self.collection super @collection = @session['system_logs'] diff --git a/spec/database_cleaner/moped/truncation_spec.rb b/spec/database_cleaner/moped/truncation_spec.rb index 956fc67..08b1c58 100644 --- a/spec/database_cleaner/moped/truncation_spec.rb +++ b/spec/database_cleaner/moped/truncation_spec.rb @@ -2,71 +2,50 @@ require 'moped' require 'database_cleaner/moped/truncation' require File.dirname(__FILE__) + '/moped_examples' -module DatabaseCleaner - module Moped +describe DatabaseCleaner::Moped::Truncation do + around do |example| + db_name = 'database_cleaner_specs' + session = ::Moped::Session.new(['127.0.0.1:27017'], database: db_name) + subject.db = db_name - describe Truncation do - let(:args) {{}} - let(:truncation) { described_class.new(args) } - #doing this in the file root breaks autospec, doing it before(:all) just fails the specs - before(:all) do - @test_db = 'database_cleaner_specs' - @session = ::Moped::Session.new(['127.0.0.1:27017'], database: @test_db) - end + example.run - before(:each) do - truncation.db = @test_db - end + session.drop + session.command(getlasterror: 1) + end - after(:each) do - @session.drop - @session.command(getlasterror: 1) - end - - def create_widget(attrs={}) - MopedTest::Widget.new({:name => 'some widget'}.merge(attrs)).save! - end - - def create_gadget(attrs={}) - MopedTest::Gadget.new({:name => 'some gadget'}.merge(attrs)).save! - end - - def create_system(attrs={}) - MopedTest::System.new({:name => 'some system'}.merge(attrs)).save! - end - - it "truncates all collections by default" do - create_widget - create_gadget - create_system - expect { truncation.clean }.to change { - [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] - }.from([1,1,1]).to([0,0,0]) - end - - context "when collections are provided to the :only option" do - let(:args) {{:only => ['MopedTest::Widget']}} - it "only truncates the specified collections" do - create_widget - create_gadget - expect { truncation.clean }.to change { - [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] - }.from([1,1,0]).to([0,1,0]) - end - end - - context "when collections are provided to the :except option" do - let(:args) {{:except => ['MopedTest::Widget']}} - it "truncates all but the specified collections" do - create_widget - create_gadget - expect { truncation.clean }.to change { - [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] - }.from([1,1,0]).to([1,0,0]) - end - end + before do + MopedTest::Widget.new(name: 'some widget').save! + MopedTest::Gadget.new(name: 'some gadget').save! + MopedTest::System.new(name: 'some system').save! + end + context "by default" do + it "truncates all collections" do + expect { subject.clean }.to change { + [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] + }.from([1,1,1]).to([0,0,0]) end + end + context "when collections are provided to the :only option" do + subject { described_class.new(only: ['MopedTest::Widget']) } + + it "only truncates the specified collections" do + expect { subject.clean }.to change { + [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] + }.from([1,1,1]).to([0,1,1]) + end + end + + context "when collections are provided to the :except option" do + subject { described_class.new(except: ['MopedTest::Widget']) } + + it "truncates all but the specified collections" do + expect { subject.clean }.to change { + [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] + }.from([1,1,1]).to([1,0,0]) + end end end + From 9615711c8146000546529d858ce4f4c3c7204be9 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 14:14:28 -0700 Subject: [PATCH 09/14] fix even more global state spec leakage. --- .../active_record/truncation/mysql2_spec.rb | 44 ++++--- .../active_record/truncation/mysql_spec.rb | 44 ++++--- .../truncation/postgresql_spec.rb | 116 +++++++++--------- .../active_record/truncation/sqlite3_spec.rb | 55 +++++---- .../data_mapper/truncation/sqlite3_spec.rb | 50 ++++---- .../neo4j/transaction_spec.rb | 1 + spec/database_cleaner/ohm/truncation_spec.rb | 99 +++++++-------- .../database_cleaner/redis/truncation_spec.rb | 75 +++++------ spec/database_cleaner/sequel/base_spec.rb | 4 - spec/database_cleaner/sequel/deletion_spec.rb | 13 +- .../sequel/truncation_spec.rb | 25 +++- spec/support/active_record/mysql2_setup.rb | 40 +++--- spec/support/active_record/mysql_setup.rb | 39 +++--- .../support/active_record/postgresql_setup.rb | 48 ++++---- spec/support/active_record/sqlite3_setup.rb | 42 +++---- spec/support/data_mapper/sqlite3_setup.rb | 41 +++---- spec/support/sequel/sequel_setup.rb | 38 ++++++ 17 files changed, 412 insertions(+), 362 deletions(-) create mode 100644 spec/support/sequel/sequel_setup.rb diff --git a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb index 306a8bb..d7c26a0 100644 --- a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb @@ -3,35 +3,41 @@ require 'support/active_record/mysql2_setup' require 'database_cleaner/active_record/truncation' require 'database_cleaner/active_record/truncation/shared_fast_truncation' -module ActiveRecord - module ConnectionAdapters - describe do - before(:all) { MySQL2Helper.active_record_mysql2_setup } +describe DatabaseCleaner::ActiveRecord::Truncation do + let(:helper) { MySQL2Helper.new } - let(:connection) { MySQL2Helper.active_record_mysql2_connection } + let(:connection) do + helper.active_record_mysql2_connection + end - describe "#truncate_table" do - it "should truncate the table" do - 2.times { User.create } + around do |example| + helper.active_record_mysql2_setup - connection.truncate_table('users') - expect(User.count).to eq 0 - end + example.run - it "should reset AUTO_INCREMENT index of table" do - 2.times { User.create } - User.delete_all + helper.active_record_mysql2_teardown + end - connection.truncate_table('users') + describe "AR connection adapter monkeypatches" do + describe "#truncate_table" do + it "should truncate the table" do + 2.times { User.create } - expect(User.create.id).to eq 1 - end + connection.truncate_table('users') + expect(User.count).to eq 0 end - it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { MySQL2Helper.active_record_mysql2_connection } + it "should reset AUTO_INCREMENT index of table" do + 2.times { User.create } + User.delete_all + + connection.truncate_table('users') + + expect(User.create.id).to eq 1 end end + + it_behaves_like "an adapter with pre-count truncation" end end diff --git a/spec/database_cleaner/active_record/truncation/mysql_spec.rb b/spec/database_cleaner/active_record/truncation/mysql_spec.rb index 20e946c..468aca8 100644 --- a/spec/database_cleaner/active_record/truncation/mysql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql_spec.rb @@ -3,35 +3,41 @@ require 'support/active_record/mysql_setup' require 'database_cleaner/active_record/truncation' require 'database_cleaner/active_record/truncation/shared_fast_truncation' -module ActiveRecord - module ConnectionAdapters - describe do - before(:all) { MySQLHelper.active_record_mysql_setup } +describe DatabaseCleaner::ActiveRecord::Truncation do + let(:helper) { MySQLHelper.new } - let(:connection) { MySQLHelper.active_record_mysql_connection } + let(:connection) do + helper.active_record_mysql_connection + end - describe "#truncate_table" do - it "should truncate the table" do - 2.times { User.create } + around do |example| + helper.active_record_mysql_setup - connection.truncate_table('users') - expect(User.count).to eq 0 - end + example.run - it "should reset AUTO_INCREMENT index of table" do - 2.times { User.create } - User.delete_all + helper.active_record_mysql_teardown + end - connection.truncate_table('users') + describe "AR connection adapter monkeypatches" do + describe "#truncate_table" do + it "should truncate the table" do + 2.times { User.create } - expect(User.create.id).to eq 1 - end + connection.truncate_table('users') + expect(User.count).to eq 0 end - it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { MySQLHelper.active_record_mysql_connection } + it "should reset AUTO_INCREMENT index of table" do + 2.times { User.create } + User.delete_all + + connection.truncate_table('users') + + expect(User.create.id).to eq 1 end end + + it_behaves_like "an adapter with pre-count truncation" end end diff --git a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb index 316bb98..8e7e085 100644 --- a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb @@ -3,72 +3,76 @@ require 'support/active_record/postgresql_setup' require 'database_cleaner/active_record/truncation' require 'database_cleaner/active_record/truncation/shared_fast_truncation' -module ActiveRecord - module ConnectionAdapters - describe "schema_migrations table" do - it "is not truncated" do - PostgreSQLHelper.active_record_pg_migrate - DatabaseCleaner::ActiveRecord::Truncation.new.clean - result = PostgreSQLHelper.active_record_pg_connection.execute("select count(*) from schema_migrations;") - expect(result.values.first).to eq ["2"] +describe DatabaseCleaner::ActiveRecord::Truncation do + let(:helper) { PostgreSQLHelper.new } + + let(:connection) do + helper.active_record_pg_connection + end + + around do |example| + helper.active_record_pg_setup + + example.run + + helper.active_record_pg_teardown + end + + describe "AR connection adapter monkeypatches" do + describe "#truncate_table" do + it "truncates the table" do + 2.times { User.create } + + connection.truncate_table('users') + expect(User.count).to eq 0 + end + + it "truncates the table without id sequence" do + 2.times { Agent.create } + + connection.truncate_table('agents') + expect(Agent.count).to eq 0 + end + + it "resets AUTO_INCREMENT index of table" do + 2.times { User.create } + User.delete_all + + connection.truncate_table('users') + + expect(User.create.id).to eq 1 end end + end - describe do - before(:all) { PostgreSQLHelper.active_record_pg_setup } + describe ":except option cleanup" do + it "should not truncate the tables specified in the :except option" do + 2.times { User.create } - let(:connection) do - PostgreSQLHelper.active_record_pg_connection - end + described_class.new(except: ['users']).clean - before(:each) do - connection.truncate_tables connection.tables - end + expect( User.count ).to eq 2 + end + end - describe "#truncate_table" do - it "truncates the table" do - 2.times { User.create } + describe '#database_cleaner_table_cache' do + it 'should default to the list of tables with their schema' do + expect(connection.database_cleaner_table_cache.first).to match(/^public\./) + end + end - connection.truncate_table('users') - expect(User.count).to eq 0 - end + it_behaves_like "an adapter with pre-count truncation" - it "truncates the table without id sequence" do - 2.times { Agent.create } + describe "schema_migrations table" do + it "is not truncated" do + helper.active_record_pg_teardown + helper.active_record_pg_migrate - connection.truncate_table('agents') - expect(Agent.count).to eq 0 - end - - it "resets AUTO_INCREMENT index of table" do - 2.times { User.create } - User.delete_all - - connection.truncate_table('users') - - expect(User.create.id).to eq 1 - end - end - - describe ":except option cleanup" do - it "should not truncate the tables specified in the :except option" do - 2.times { User.create } - - DatabaseCleaner::ActiveRecord::Truncation.new(:except => ['users']).clean - expect( User.count ).to eq 2 - end - end - - describe '#database_cleaner_table_cache' do - it 'should default to the list of tables with their schema' do - expect(connection.database_cleaner_table_cache.first).to match(/^public\./) - end - end - - it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { PostgreSQLHelper.active_record_pg_connection } - end + subject.clean + result = connection.execute("select count(*) from schema_migrations;") + expect(result.values.first).to eq ["2"] end end end + diff --git a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb index 0bf005c..f9f4f28 100644 --- a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb @@ -2,37 +2,38 @@ require 'active_record' require 'support/active_record/sqlite3_setup' require 'database_cleaner/active_record/truncation' -module ActiveRecord - module ConnectionAdapters - describe do - before(:all) { SQLite3Helper.active_record_sqlite3_setup } +describe DatabaseCleaner::ActiveRecord::Truncation do + let(:helper) { SQLite3Helper.new } - let(:connection) do - SQLite3Helper.active_record_sqlite3_connection + let(:connection) do + helper.active_record_sqlite3_connection + end + + around do |example| + helper.active_record_sqlite3_setup + + example.run + + helper.active_record_sqlite3_teardown + end + + describe "AR connection adapter monkeypatches" do + describe "#truncate_table" do + it "truncates the table" do + 2.times { User.create } + + connection.truncate_table('users') + expect(User.count).to eq 0 end - before(:each) do - connection.truncate_tables connection.tables + it "resets AUTO_INCREMENT index of table" do + 2.times { User.create } + User.delete_all + + connection.truncate_table('users') + + expect(User.create.id).to eq 1 end - - describe "#truncate_table" do - it "truncates the table" do - 2.times { User.create } - - connection.truncate_table('users') - expect(User.count).to eq 0 - end - - it "resets AUTO_INCREMENT index of table" do - 2.times { User.create } - User.delete_all - - connection.truncate_table('users') - - expect(User.create.id).to eq 1 - end - end - end end end diff --git a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb index 0dc8c98..a014569 100644 --- a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb @@ -3,37 +3,37 @@ require 'dm-sqlite-adapter' require 'support/data_mapper/sqlite3_setup' require 'database_cleaner/data_mapper/truncation' -module DataMapper - module ConnectionAdapters - describe do - before(:all) { DataMapperSQLite3Helper.data_mapper_sqlite3_setup } +describe DatabaseCleaner::DataMapper::Truncation do + let(:helper) { DataMapperSQLite3Helper.new } - let(:adapter) { DataMapperSQLite3Adapter } + let(:connection) do + helper.data_mapper_sqlite3_connection + end - let(:connection) do - DataMapperSQLite3Helper.data_mapper_sqlite3_connection + around do |example| + helper.data_mapper_sqlite3_setup + + example.run + + helper.data_mapper_sqlite3_teardown + end + + describe "DM connection adapter monkeypatches" do + describe "#truncate_table" do + it "truncates the table" do + 2.times { DmUser.create } + + connection.truncate_table(DmUser.storage_names[:default]) + expect(DmUser.count).to eq 0 end - before(:each) do - connection.truncate_tables(DataMapper::Model.descendants.map { |d| d.storage_names[:default] || d.name.underscore }) - end + it "resets AUTO_INCREMENT index of table" do + 2.times { DmUser.create } + DmUser.destroy - describe "#truncate_table" do - it "truncates the table" do - 2.times { DmUser.create } + connection.truncate_table(DmUser.storage_names[:default]) - connection.truncate_table(DmUser.storage_names[:default]) - expect(DmUser.count).to eq 0 - end - - it "resets AUTO_INCREMENT index of table" do - 2.times { DmUser.create } - DmUser.destroy - - connection.truncate_table(DmUser.storage_names[:default]) - - expect(DmUser.create.id).to eq 1 - end + expect(DmUser.create.id).to eq 1 end end end diff --git a/spec/database_cleaner/neo4j/transaction_spec.rb b/spec/database_cleaner/neo4j/transaction_spec.rb index e567f13..d0b213b 100644 --- a/spec/database_cleaner/neo4j/transaction_spec.rb +++ b/spec/database_cleaner/neo4j/transaction_spec.rb @@ -1,5 +1,6 @@ require 'neo4j-core' require 'database_cleaner/neo4j/transaction' +require 'database_cleaner/shared_strategy' module DatabaseCleaner module Neo4j diff --git a/spec/database_cleaner/ohm/truncation_spec.rb b/spec/database_cleaner/ohm/truncation_spec.rb index 7ec4ebd..9712ea0 100644 --- a/spec/database_cleaner/ohm/truncation_spec.rb +++ b/spec/database_cleaner/ohm/truncation_spec.rb @@ -1,69 +1,54 @@ require 'ohm' require 'database_cleaner/ohm/truncation' -module DatabaseCleaner - module Ohm +module OhmTests + class Widget < ::Ohm::Model + attribute :name + end - class Widget < ::Ohm::Model - attribute :name + class Gadget < ::Ohm::Model + attribute :name + end +end + +describe DatabaseCleaner::Ohm::Truncation do + around do |example| + config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml")) + Ohm.connect url: config['test']['url'] + @redis = Ohm.redis + + example.run + + @redis.flushdb + end + + before do + OhmTests::Widget.new(name: 'some widget').save + OhmTests::Gadget.new(name: 'some gadget').save + end + + context "by default" do + it "truncates all keys" do + expect { subject.clean }.to change { @redis.keys.size }.from(6).to(0) end + end - class Gadget < ::Ohm::Model - attribute :name + context "when keys are provided to the :only option" do + subject { described_class.new(only: ['*Widget*']) } + + it "only truncates the specified keys" do + expect { subject.clean }.to change { @redis.keys.size }.from(6).to(3) + expect(@redis.get('OhmTests::Gadget:id')).to eq '1' end + end - describe Truncation do - before(:all) do - config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml")) - ::Ohm.connect :url => config['test']['url'] - @redis = ::Ohm.redis - end + context "when keys are provided to the :except option" do + subject { described_class.new(except: ['*Widget*']) } - before(:each) do - @redis.flushdb - end - - it "should flush the database" do - Truncation.new.clean - end - - def create_widget(attrs={}) - Widget.new({:name => 'some widget'}.merge(attrs)).save - end - - def create_gadget(attrs={}) - Gadget.new({:name => 'some gadget'}.merge(attrs)).save - end - - it "truncates all keys by default" do - create_widget - create_gadget - expect(@redis.keys.size).to eq 6 - Truncation.new.clean - expect(@redis.keys.size).to eq 0 - end - - context "when keys are provided to the :only option" do - it "only truncates the specified keys" do - create_widget - create_gadget - expect(@redis.keys.size).to eq 6 - Truncation.new(:only => ['*Widget*']).clean - expect(@redis.keys.size).to eq 3 - expect(@redis.get('DatabaseCleaner::Ohm::Gadget:id')).to eq '1' - end - end - - context "when keys are provided to the :except option" do - it "truncates all but the specified keys" do - create_widget - create_gadget - expect(@redis.keys.size).to eq 6 - Truncation.new(:except => ['*Widget*']).clean - expect(@redis.keys.size).to eq 3 - expect(@redis.get('DatabaseCleaner::Ohm::Widget:id')).to eq '1' - end - end + it "truncates all but the specified keys" do + expect { subject.clean }.to change { @redis.keys.size }.from(6).to(3) + expect(@redis.get('OhmTests::Widget:id')).to eq '1' end end end + diff --git a/spec/database_cleaner/redis/truncation_spec.rb b/spec/database_cleaner/redis/truncation_spec.rb index 9899e9d..1b2bcfa 100644 --- a/spec/database_cleaner/redis/truncation_spec.rb +++ b/spec/database_cleaner/redis/truncation_spec.rb @@ -1,61 +1,42 @@ require 'redis' require 'database_cleaner/redis/truncation' +describe DatabaseCleaner::Redis::Truncation do + around do |example| + config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml")) + @redis = ::Redis.new :url => config['test']['url'] -module DatabaseCleaner - module Redis + example.run - describe Truncation do - before(:all) do - config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml")) - @redis = ::Redis.new :url => config['test']['url'] - end + @redis.flushdb + end - before(:each) do - @redis.flushdb - end + before do + @redis.set 'Widget', 1 + @redis.set 'Gadget', 1 + end - it "should flush the database" do - Truncation.new.clean - end + context "by default" do + it "truncates all keys" do + expect { subject.clean }.to change { @redis.keys.size }.from(2).to(0) + end + end - def create_widget(attrs={}) - @redis.set 'Widget', 1 - end + context "when keys are provided to the :only option" do + subject { described_class.new(only: ['Widge*']) } - def create_gadget(attrs={}) - @redis.set 'Gadget', 1 - end + it "only truncates the specified keys" do + expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1) + expect(@redis.get('Gadget')).to eq '1' + end + end - it "truncates all keys by default" do - create_widget - create_gadget - expect(@redis.keys.size).to eq 2 - Truncation.new.clean - expect(@redis.keys.size).to eq 0 - end + context "when keys are provided to the :except option" do + subject { described_class.new(except: ['Widg*']) } - context "when keys are provided to the :only option" do - it "only truncates the specified keys" do - create_widget - create_gadget - expect(@redis.keys.size).to eq 2 - Truncation.new(:only => ['Widge*']).clean - expect(@redis.keys.size).to eq 1 - expect(@redis.get('Gadget')).to eq '1' - end - end - - context "when keys are provided to the :except option" do - it "truncates all but the specified keys" do - create_widget - create_gadget - expect(@redis.keys.size).to eq 2 - Truncation.new(:except => ['Widg*']).clean - expect(@redis.keys.size).to eq 1 - expect(@redis.get('Widget')).to eq '1' - end - end + it "truncates all but the specified keys" do + expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1) + expect(@redis.get('Widget')).to eq '1' end end end diff --git a/spec/database_cleaner/sequel/base_spec.rb b/spec/database_cleaner/sequel/base_spec.rb index b2b3b73..16fa9e9 100644 --- a/spec/database_cleaner/sequel/base_spec.rb +++ b/spec/database_cleaner/sequel/base_spec.rb @@ -22,10 +22,6 @@ module DatabaseCleaner expect(subject.db).to eq :my_db end - it "should default to :default" do - expect(subject.db).to eq :default - end - pending "I figure out how to use Sequel and write some real tests for it..." end end diff --git a/spec/database_cleaner/sequel/deletion_spec.rb b/spec/database_cleaner/sequel/deletion_spec.rb index 08cfade..e5255e6 100644 --- a/spec/database_cleaner/sequel/deletion_spec.rb +++ b/spec/database_cleaner/sequel/deletion_spec.rb @@ -1,6 +1,8 @@ require 'database_cleaner/sequel/deletion' require 'database_cleaner/shared_strategy' require 'sequel' +require 'support/sequel/sequel_setup' +# XXX: use ActiveRecord's db_config (`db/config.yml`) for CI/dev convenience require 'support/active_record/database_setup' module DatabaseCleaner @@ -43,13 +45,22 @@ module DatabaseCleaner supported_configurations = [ { :url => 'mysql:///', :connection_options => db_config['mysql'] }, - { :url => 'postgres:///', :connection_options => db_config['postgres'] } + { :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]) } + around do |example| + helper = SequelHelper.new(config) + helper.setup + + example.run + + helper.teardown + end + it_behaves_like 'a Sequel deletion strategy' end end diff --git a/spec/database_cleaner/sequel/truncation_spec.rb b/spec/database_cleaner/sequel/truncation_spec.rb index 975a42e..4ebdb3f 100644 --- a/spec/database_cleaner/sequel/truncation_spec.rb +++ b/spec/database_cleaner/sequel/truncation_spec.rb @@ -1,7 +1,7 @@ require 'database_cleaner/sequel/truncation' require 'database_cleaner/shared_strategy' require 'sequel' - +require 'support/sequel/sequel_setup' # XXX: use ActiveRecord's db_config (`db/config.yml`) for CI/dev convenience require 'support/active_record/database_setup' @@ -22,7 +22,7 @@ module DatabaseCleaner end context 'when several tables have data' do - before(:each) do + before 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 } @@ -122,13 +122,24 @@ module DatabaseCleaner {url: 'mysql:///', connection_options: db_config['mysql']}, {url: 'mysql2:///', connection_options: db_config['mysql2']} ] + supported_configurations.each do |config| describe "Sequel truncation (using a #{config[:url]} connection)" do + around do |example| + helper = SequelHelper.new(config) + helper.setup + + example.run + + helper.teardown + end + 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' + describe '#pre_count?' do subject { Truncation.new.tap { |t| t.db = db } } @@ -170,8 +181,18 @@ module DatabaseCleaner end end end + half_supported_configurations.each do |config| describe "Sequel truncation (using a #{config[:url]} connection)" do + around do |example| + helper = SequelHelper.new(config) + helper.setup + + example.run + + helper.teardown + end + let(:db) { ::Sequel.connect(config[:url], config[:connection_options]) } it_behaves_like 'a Sequel truncation strategy' diff --git a/spec/support/active_record/mysql2_setup.rb b/spec/support/active_record/mysql2_setup.rb index 6e76ad6..a180f98 100644 --- a/spec/support/active_record/mysql2_setup.rb +++ b/spec/support/active_record/mysql2_setup.rb @@ -1,29 +1,12 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' -module MySQL2Helper - extend self - +class MySQL2Helper puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2" # require 'logger' # ActiveRecord::Base.logger = Logger.new(STDERR) - def default_config - db_config['mysql2'] - end - - def create_db - establish_connection(default_config.merge(:database => nil)) - - ActiveRecord::Base.connection.drop_database default_config['database'] rescue nil - ActiveRecord::Base.connection.create_database default_config['database'] - end - - def establish_connection(config = default_config) - ActiveRecord::Base.establish_connection config - end - def active_record_mysql2_setup patch_mysql2_adapter create_db @@ -35,6 +18,27 @@ module MySQL2Helper ActiveRecord::Base.connection end + def active_record_mysql2_teardown + ActiveRecord::Base.connection.drop_database default_config['database'] + end + + private + + def default_config + db_config['mysql2'] + end + + def create_db + establish_connection(default_config.merge("database" => nil)) + + ActiveRecord::Base.connection.drop_database default_config['database'] rescue nil + ActiveRecord::Base.connection.create_database default_config['database'] + end + + def establish_connection(config = default_config) + ActiveRecord::Base.establish_connection config + end + def patch_mysql2_adapter # remove DEFAULT NULL from column definition, which is an error on primary keys in MySQL 5.7.3+ ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY" diff --git a/spec/support/active_record/mysql_setup.rb b/spec/support/active_record/mysql_setup.rb index e0ade73..8b99bde 100644 --- a/spec/support/active_record/mysql_setup.rb +++ b/spec/support/active_record/mysql_setup.rb @@ -1,38 +1,37 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' -module MySQLHelper - extend self - +class MySQLHelper puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql" # require 'logger' # ActiveRecord::Base.logger = Logger.new(STDERR) + def active_record_mysql_setup + patch_mysql_adapter + create_db + active_record_load_schema + end + + def active_record_mysql_connection + ActiveRecord::Base.connection + end + + def active_record_mysql_teardown + ActiveRecord::Base.connection.drop_database default_config['database'] + end + + private + def default_config db_config['mysql'] end def create_db - establish_connection(default_config.merge(:database => nil)) - + ActiveRecord::Base.establish_connection default_config.merge("database" => nil) ActiveRecord::Base.connection.drop_database default_config['database'] rescue nil ActiveRecord::Base.connection.create_database default_config['database'] - end - - def establish_connection(config = default_config) - ActiveRecord::Base.establish_connection config - end - - def active_record_mysql_setup - patch_mysql_adapter - create_db - establish_connection - active_record_load_schema - end - - def active_record_mysql_connection - ActiveRecord::Base.connection + ActiveRecord::Base.establish_connection default_config end def patch_mysql_adapter diff --git a/spec/support/active_record/postgresql_setup.rb b/spec/support/active_record/postgresql_setup.rb index fb851ef..a3c4438 100644 --- a/spec/support/active_record/postgresql_setup.rb +++ b/spec/support/active_record/postgresql_setup.rb @@ -1,33 +1,11 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' -module PostgreSQLHelper - extend self - +class PostgreSQLHelper puts "Active Record #{ActiveRecord::VERSION::STRING}, pg" # ActiveRecord::Base.logger = Logger.new(STDERR) - def default_config - db_config['postgres'] - end - - def create_db - @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8' - begin - establish_connection(default_config.merge('database' => 'postgres', 'schema_search_path' => 'public')) - ActiveRecord::Base.connection.drop_database(default_config['database']) rescue nil - ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding)) - rescue Exception => e - $stderr.puts e, *(e.backtrace) - $stderr.puts "Couldn't create database for #{default_config.inspect}" - end - end - - def establish_connection(config = default_config) - ActiveRecord::Base.establish_connection(config) - end - def active_record_pg_setup create_db establish_connection @@ -35,13 +13,33 @@ module PostgreSQLHelper end def active_record_pg_migrate - create_db - establish_connection ActiveRecord::Migrator.migrate 'spec/support/active_record/migrations' end def active_record_pg_connection ActiveRecord::Base.connection end + + def active_record_pg_teardown + ActiveRecord::Base.connection.execute "DROP TABLE users, agents;" + rescue ActiveRecord::StatementInvalid + end + + private + + def default_config + db_config['postgres'] + end + + def create_db + @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8' + establish_connection(default_config.merge('database' => 'postgres', 'schema_search_path' => 'public')) + ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding)) + rescue ActiveRecord::StatementInvalid + end + + def establish_connection(config = default_config) + ActiveRecord::Base.establish_connection(config) + end end diff --git a/spec/support/active_record/sqlite3_setup.rb b/spec/support/active_record/sqlite3_setup.rb index 8047c83..325e1dd 100644 --- a/spec/support/active_record/sqlite3_setup.rb +++ b/spec/support/active_record/sqlite3_setup.rb @@ -1,31 +1,11 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' -module SQLite3Helper - extend self - +class SQLite3Helper puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3" # ActiveRecord::Base.logger = Logger.new(STDERR) - def default_config - db_config['sqlite3'] - end - - def create_db - @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8' - begin - establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public')) - rescue Exception => e - $stderr.puts e, *(e.backtrace) - $stderr.puts "Couldn't create database for #{default_config.inspect}" - end - end - - def establish_connection(config = default_config) - ActiveRecord::Base.establish_connection(config) - end - def active_record_sqlite3_setup create_db establish_connection @@ -35,5 +15,25 @@ module SQLite3Helper def active_record_sqlite3_connection ActiveRecord::Base.connection end + + def active_record_sqlite3_teardown + ActiveRecord::Base.connection.truncate_table('users') + ActiveRecord::Base.connection.truncate_table('agents') + end + + private + + def default_config + db_config['sqlite3'] + end + + def create_db + @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8' + establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public')) + end + + def establish_connection(config = default_config) + ActiveRecord::Base.establish_connection(config) + end end diff --git a/spec/support/data_mapper/sqlite3_setup.rb b/spec/support/data_mapper/sqlite3_setup.rb index 98b29be..32ef78a 100644 --- a/spec/support/data_mapper/sqlite3_setup.rb +++ b/spec/support/data_mapper/sqlite3_setup.rb @@ -1,29 +1,9 @@ require 'support/active_record/database_setup' require 'support/data_mapper/schema_setup' -module DataMapperSQLite3Helper - extend self - +class DataMapperSQLite3Helper puts "DataMapper #{DataMapper::VERSION}, sqlite3" - def default_config - db_config['sqlite3'] - end - - def create_db - @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8' - begin - establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public')) - rescue Exception => e - $stderr.puts e, *(e.backtrace) - $stderr.puts "Couldn't create database for #{default_config.inspect}" - end - end - - def establish_connection(config = default_config) - DataMapper.setup(:default, config) - end - def data_mapper_sqlite3_setup create_db establish_connection @@ -33,5 +13,24 @@ module DataMapperSQLite3Helper def data_mapper_sqlite3_connection DataMapper.repository.adapter end + + def data_mapper_sqlite3_teardown + DataMapper.repository.adapter.truncate_tables(DataMapper::Model.descendants.map { |d| d.storage_names[:default] || d.name.underscore }) + end + + private + + def default_config + db_config['sqlite3'] + end + + def create_db + @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8' + establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public')) + end + + def establish_connection(config = default_config) + DataMapper.setup(:default, config) + end end diff --git a/spec/support/sequel/sequel_setup.rb b/spec/support/sequel/sequel_setup.rb new file mode 100644 index 0000000..3694252 --- /dev/null +++ b/spec/support/sequel/sequel_setup.rb @@ -0,0 +1,38 @@ +class SequelHelper < Struct.new(:config) + def setup + if config[:url] == "postgres:///" || config[:url] == "sqlite:///" + ::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 teardown + if config[:url] == "postgres:///" || config[:url] == "sqlite:///" + ::Sequel.connect(config[:url], config[:connection_options]) do |db| + db.execute "DROP TABLE IF EXISTS precious_stones" + db.execute "DROP TABLE IF EXISTS replaceable_trifles" + db.execute "DROP TABLE IF EXISTS worthless_junk" + end + else + ::Sequel.connect(config[:url], config[:connection_options].merge('database' => nil)) do |db| + db.execute "DROP DATABASE IF EXISTS #{database}" + end + end + rescue SQLite3::BusyException + end + + private + + def database + config[:connection_options]['database'] + end +end From 37eee17e8d5e2cb13a83e2d63b82b3c7ba7ad985 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 14:38:37 -0700 Subject: [PATCH 10/14] enable random spec ordering to ensure specs remain isolated. --- .rspec | 1 + 1 file changed, 1 insertion(+) diff --git a/.rspec b/.rspec index b83d9b7..53b85b9 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,4 @@ --color --format documentation --require spec_helper +--order rand From 72d3d33ca77402d6357c2c30a1159cd04bed74c9 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Wed, 25 Apr 2018 10:22:24 -0700 Subject: [PATCH 11/14] fix pending configuration specs. --- spec/database_cleaner/configuration_spec.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/database_cleaner/configuration_spec.rb b/spec/database_cleaner/configuration_spec.rb index 072b135..035e8d7 100644 --- a/spec/database_cleaner/configuration_spec.rb +++ b/spec/database_cleaner/configuration_spec.rb @@ -121,9 +121,9 @@ describe DatabaseCleaner do context "connection/db retrieval" do it "should retrieve a db rather than create a new one" do - pending - connection = DatabaseCleaner[:active_record].strategy = :truncation - expect(DatabaseCleaner[:active_record]).to eq connection + connection = ::DatabaseCleaner[:active_record] + ::DatabaseCleaner[:active_record].strategy = :truncation + expect(DatabaseCleaner[:active_record]).to equal connection end end @@ -243,7 +243,7 @@ describe DatabaseCleaner do # plausably want to force orm/strategy change on two sets of orm that differ only on db context "multiple orm proxy methods" do - pending "should proxy orm to all connections and remove duplicate connections" do + it "should proxy orm to all connections and remove duplicate connections" do active_record_1 = double("active_mock_on_db_one").as_null_object active_record_2 = double("active_mock_on_db_two").as_null_object data_mapper_1 = double("data_mock_on_db_one").as_null_object @@ -254,6 +254,8 @@ describe DatabaseCleaner do expect(active_record_2).to receive(:orm=).with(:data_mapper) expect(data_mapper_1).to receive(:orm=).with(:data_mapper) + expect(active_record_1).to receive(:==).with(active_record_2).and_return(false) + expect(active_record_2).to receive(:==).with(data_mapper_1).and_return(false) expect(active_record_1).to receive(:==).with(data_mapper_1).and_return(true) expect(DatabaseCleaner.connections.size).to eq 3 From ace44975d11e06e6a11165d5e1a971f559990646 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 20:36:02 -0700 Subject: [PATCH 12/14] silence AR migration noise during specs. --- spec/support/active_record/schema_setup.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/spec/support/active_record/schema_setup.rb b/spec/support/active_record/schema_setup.rb index 59d62ac..d54bea1 100644 --- a/spec/support/active_record/schema_setup.rb +++ b/spec/support/active_record/schema_setup.rb @@ -1,11 +1,13 @@ def active_record_load_schema - ActiveRecord::Schema.define do - create_table :users, :force => true do |t| - t.integer :name - end + ActiveRecord::Migration.suppress_messages do + ActiveRecord::Schema.define do + create_table :users, :force => true do |t| + t.integer :name + end - create_table :agents, :id => false, :force => true do |t| - t.integer :name + create_table :agents, :id => false, :force => true do |t| + t.integer :name + end end end end From 79f8b8e6e9d27782d485287226fa0de6b3b8b474 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Mon, 30 Apr 2018 19:31:44 -0700 Subject: [PATCH 13/14] disable RSpec monkeypatching. --- spec/database_cleaner/active_record/base_spec.rb | 4 ++-- .../active_record/transaction_spec.rb | 2 +- .../active_record/truncation/mysql2_spec.rb | 2 +- .../active_record/truncation/mysql_spec.rb | 2 +- .../active_record/truncation/postgresql_spec.rb | 2 +- .../truncation/shared_fast_truncation.rb | 2 +- .../active_record/truncation/sqlite3_spec.rb | 2 +- .../active_record/truncation_spec.rb | 4 ++-- spec/database_cleaner/base_spec.rb | 2 +- spec/database_cleaner/configuration_spec.rb | 2 +- .../database_cleaner/couch_potato/truncation_spec.rb | 2 +- spec/database_cleaner/data_mapper/base_spec.rb | 4 ++-- .../database_cleaner/data_mapper/transaction_spec.rb | 2 +- .../data_mapper/truncation/sqlite3_spec.rb | 2 +- spec/database_cleaner/data_mapper/truncation_spec.rb | 2 +- spec/database_cleaner/generic/base_spec.rb | 2 +- spec/database_cleaner/generic/truncation_spec.rb | 2 +- spec/database_cleaner/mongo/truncation_spec.rb | 2 +- spec/database_cleaner/mongo_mapper/base_spec.rb | 4 ++-- .../database_cleaner/mongo_mapper/truncation_spec.rb | 2 +- spec/database_cleaner/moped/truncation_spec.rb | 2 +- spec/database_cleaner/neo4j/base_spec.rb | 4 ++-- spec/database_cleaner/neo4j/transaction_spec.rb | 2 +- spec/database_cleaner/null_strategy_spec.rb | 2 +- spec/database_cleaner/ohm/truncation_spec.rb | 2 +- spec/database_cleaner/redis/base_spec.rb | 4 ++-- spec/database_cleaner/redis/truncation_spec.rb | 2 +- spec/database_cleaner/safeguard_spec.rb | 2 +- spec/database_cleaner/sequel/base_spec.rb | 4 ++-- spec/database_cleaner/sequel/deletion_spec.rb | 6 +++--- spec/database_cleaner/sequel/transaction_spec.rb | 2 +- spec/database_cleaner/sequel/truncation_spec.rb | 12 ++++++------ spec/database_cleaner/shared_strategy.rb | 6 +++--- spec/spec_helper.rb | 2 ++ 34 files changed, 51 insertions(+), 49 deletions(-) diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index ab27d03..fc37927 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -9,7 +9,7 @@ class FakeModel end end -describe DatabaseCleaner::ActiveRecord do +RSpec.describe DatabaseCleaner::ActiveRecord do it { is_expected.to respond_to(:available_strategies) } describe "config_file_location" do @@ -32,7 +32,7 @@ module DatabaseCleaner include DatabaseCleaner::ActiveRecord::Base end - describe ExampleStrategy do + RSpec.describe ExampleStrategy do let(:config_location) { '/path/to/config/database.yml' } before do diff --git a/spec/database_cleaner/active_record/transaction_spec.rb b/spec/database_cleaner/active_record/transaction_spec.rb index db7524d..c91c319 100644 --- a/spec/database_cleaner/active_record/transaction_spec.rb +++ b/spec/database_cleaner/active_record/transaction_spec.rb @@ -4,7 +4,7 @@ require 'active_record' module DatabaseCleaner module ActiveRecord - describe Transaction do + RSpec.describe Transaction do let (:connection) { double("connection") } let (:connection_2) { double("connection") } let (:connection_pool) { double("connection_pool")} diff --git a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb index d7c26a0..43c539e 100644 --- a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb @@ -3,7 +3,7 @@ require 'support/active_record/mysql2_setup' require 'database_cleaner/active_record/truncation' require 'database_cleaner/active_record/truncation/shared_fast_truncation' -describe DatabaseCleaner::ActiveRecord::Truncation do +RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do let(:helper) { MySQL2Helper.new } let(:connection) do diff --git a/spec/database_cleaner/active_record/truncation/mysql_spec.rb b/spec/database_cleaner/active_record/truncation/mysql_spec.rb index 468aca8..d3373b1 100644 --- a/spec/database_cleaner/active_record/truncation/mysql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql_spec.rb @@ -3,7 +3,7 @@ require 'support/active_record/mysql_setup' require 'database_cleaner/active_record/truncation' require 'database_cleaner/active_record/truncation/shared_fast_truncation' -describe DatabaseCleaner::ActiveRecord::Truncation do +RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do let(:helper) { MySQLHelper.new } let(:connection) do diff --git a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb index 8e7e085..3c3faca 100644 --- a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb @@ -3,7 +3,7 @@ require 'support/active_record/postgresql_setup' require 'database_cleaner/active_record/truncation' require 'database_cleaner/active_record/truncation/shared_fast_truncation' -describe DatabaseCleaner::ActiveRecord::Truncation do +RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do let(:helper) { PostgreSQLHelper.new } let(:connection) do diff --git a/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb b/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb index dda580c..10d7571 100644 --- a/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb +++ b/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb @@ -1,4 +1,4 @@ -shared_examples_for "an adapter with pre-count truncation" do +RSpec.shared_examples_for "an adapter with pre-count truncation" do describe "#pre_count_truncate_tables" do context "with :reset_ids set true" do diff --git a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb index f9f4f28..572d428 100644 --- a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb @@ -2,7 +2,7 @@ require 'active_record' require 'support/active_record/sqlite3_setup' require 'database_cleaner/active_record/truncation' -describe DatabaseCleaner::ActiveRecord::Truncation do +RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do let(:helper) { SQLite3Helper.new } let(:connection) do diff --git a/spec/database_cleaner/active_record/truncation_spec.rb b/spec/database_cleaner/active_record/truncation_spec.rb index 65d83e0..15cfc29 100644 --- a/spec/database_cleaner/active_record/truncation_spec.rb +++ b/spec/database_cleaner/active_record/truncation_spec.rb @@ -10,7 +10,7 @@ module ActiveRecord module ConnectionAdapters #JdbcAdapter IBM_DBAdapter [ MysqlAdapter, Mysql2Adapter, SQLite3Adapter, PostgreSQLAdapter ].each do |adapter| - describe adapter, "#truncate_table" do + RSpec.describe adapter, "#truncate_table" do it "responds" do expect(adapter.instance_methods).to include(:truncate_table) end @@ -22,7 +22,7 @@ end module DatabaseCleaner module ActiveRecord - describe Truncation do + RSpec.describe Truncation do let(:connection) { double('connection') } before(:each) do diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index ba3f9c6..bbe1c76 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -6,7 +6,7 @@ require 'database_cleaner/couch_potato/truncation' require 'database_cleaner/neo4j/transaction' module DatabaseCleaner - describe Base do + RSpec.describe Base do let(:mock_strategy) { double("strategy").tap{|strategy| diff --git a/spec/database_cleaner/configuration_spec.rb b/spec/database_cleaner/configuration_spec.rb index 035e8d7..8f30f86 100644 --- a/spec/database_cleaner/configuration_spec.rb +++ b/spec/database_cleaner/configuration_spec.rb @@ -20,7 +20,7 @@ module DatabaseCleaner end end -describe DatabaseCleaner do +RSpec.describe DatabaseCleaner do before(:each) { DatabaseCleaner.reset } context "orm specification" do diff --git a/spec/database_cleaner/couch_potato/truncation_spec.rb b/spec/database_cleaner/couch_potato/truncation_spec.rb index c69be10..8f4360a 100644 --- a/spec/database_cleaner/couch_potato/truncation_spec.rb +++ b/spec/database_cleaner/couch_potato/truncation_spec.rb @@ -4,7 +4,7 @@ require 'couch_potato' module DatabaseCleaner module CouchPotato - describe Truncation do + RSpec.describe Truncation do let(:database) { double('database') } before(:each) do diff --git a/spec/database_cleaner/data_mapper/base_spec.rb b/spec/database_cleaner/data_mapper/base_spec.rb index 61b819d..ff5f822 100644 --- a/spec/database_cleaner/data_mapper/base_spec.rb +++ b/spec/database_cleaner/data_mapper/base_spec.rb @@ -2,7 +2,7 @@ require 'database_cleaner/data_mapper/base' require 'database_cleaner/shared_strategy' module DatabaseCleaner - describe DataMapper do + RSpec.describe DataMapper do it { is_expected.to respond_to(:available_strategies) } end @@ -11,7 +11,7 @@ module DatabaseCleaner include ::DatabaseCleaner::DataMapper::Base end - describe ExampleStrategy do + RSpec.describe ExampleStrategy do it_should_behave_like "a generic strategy" it { is_expected.to respond_to(:db) } it { is_expected.to respond_to(:db=) } diff --git a/spec/database_cleaner/data_mapper/transaction_spec.rb b/spec/database_cleaner/data_mapper/transaction_spec.rb index a7350e2..91f827c 100644 --- a/spec/database_cleaner/data_mapper/transaction_spec.rb +++ b/spec/database_cleaner/data_mapper/transaction_spec.rb @@ -5,7 +5,7 @@ require 'database_cleaner/shared_strategy' module DatabaseCleaner module DataMapper - describe Transaction do + RSpec.describe Transaction do it_should_behave_like "a generic strategy" it_should_behave_like "a generic transaction strategy" diff --git a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb index a014569..f7f82cd 100644 --- a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb @@ -3,7 +3,7 @@ require 'dm-sqlite-adapter' require 'support/data_mapper/sqlite3_setup' require 'database_cleaner/data_mapper/truncation' -describe DatabaseCleaner::DataMapper::Truncation do +RSpec.describe DatabaseCleaner::DataMapper::Truncation do let(:helper) { DataMapperSQLite3Helper.new } let(:connection) do diff --git a/spec/database_cleaner/data_mapper/truncation_spec.rb b/spec/database_cleaner/data_mapper/truncation_spec.rb index 7e3c11d..9fd3916 100644 --- a/spec/database_cleaner/data_mapper/truncation_spec.rb +++ b/spec/database_cleaner/data_mapper/truncation_spec.rb @@ -3,7 +3,7 @@ require 'database_cleaner/shared_strategy' module DatabaseCleaner module DataMapper - describe Truncation do + RSpec.describe Truncation do it_should_behave_like "a generic strategy" it_should_behave_like "a generic truncation strategy" end diff --git a/spec/database_cleaner/generic/base_spec.rb b/spec/database_cleaner/generic/base_spec.rb index 61aa98b..8eef96a 100644 --- a/spec/database_cleaner/generic/base_spec.rb +++ b/spec/database_cleaner/generic/base_spec.rb @@ -10,7 +10,7 @@ module ::DatabaseCleaner def start; end end - describe ExampleStrategy do + RSpec.describe ExampleStrategy do context "class methods" do subject { ExampleStrategy } diff --git a/spec/database_cleaner/generic/truncation_spec.rb b/spec/database_cleaner/generic/truncation_spec.rb index d155a9d..39cf267 100644 --- a/spec/database_cleaner/generic/truncation_spec.rb +++ b/spec/database_cleaner/generic/truncation_spec.rb @@ -28,7 +28,7 @@ module ::DatabaseCleaner end end - describe TruncationExample do + RSpec.describe TruncationExample do subject(:truncation_example) { TruncationExample.new } it "will start" do diff --git a/spec/database_cleaner/mongo/truncation_spec.rb b/spec/database_cleaner/mongo/truncation_spec.rb index 922648d..9cf1dff 100644 --- a/spec/database_cleaner/mongo/truncation_spec.rb +++ b/spec/database_cleaner/mongo/truncation_spec.rb @@ -2,7 +2,7 @@ require 'mongo' require 'database_cleaner/mongo/truncation' require File.dirname(__FILE__) + '/mongo_examples' -describe DatabaseCleaner::Mongo::Truncation do +RSpec.describe DatabaseCleaner::Mongo::Truncation do around do |example| connection = Mongo::Connection.new('127.0.0.1') db_name = 'database_cleaner_specs' diff --git a/spec/database_cleaner/mongo_mapper/base_spec.rb b/spec/database_cleaner/mongo_mapper/base_spec.rb index 03a93ad..9b1d95d 100644 --- a/spec/database_cleaner/mongo_mapper/base_spec.rb +++ b/spec/database_cleaner/mongo_mapper/base_spec.rb @@ -2,7 +2,7 @@ require 'database_cleaner/mongo_mapper/base' require 'database_cleaner/shared_strategy' module DatabaseCleaner - describe MongoMapper do + RSpec.describe MongoMapper do it { is_expected.to respond_to(:available_strategies) } end @@ -11,7 +11,7 @@ module DatabaseCleaner include ::DatabaseCleaner::MongoMapper::Base end - describe ExampleStrategy do + RSpec.describe ExampleStrategy do it_should_behave_like "a generic strategy" diff --git a/spec/database_cleaner/mongo_mapper/truncation_spec.rb b/spec/database_cleaner/mongo_mapper/truncation_spec.rb index 211d3b6..ce0a3f6 100644 --- a/spec/database_cleaner/mongo_mapper/truncation_spec.rb +++ b/spec/database_cleaner/mongo_mapper/truncation_spec.rb @@ -2,7 +2,7 @@ require 'mongo_mapper' require 'database_cleaner/mongo_mapper/truncation' require File.dirname(__FILE__) + '/mongo_examples' -describe DatabaseCleaner::MongoMapper::Truncation do +RSpec.describe DatabaseCleaner::MongoMapper::Truncation do around do |example| MongoMapper.connection = Mongo::Connection.new('127.0.0.1') db_name = 'database_cleaner_specs' diff --git a/spec/database_cleaner/moped/truncation_spec.rb b/spec/database_cleaner/moped/truncation_spec.rb index 08b1c58..758dcff 100644 --- a/spec/database_cleaner/moped/truncation_spec.rb +++ b/spec/database_cleaner/moped/truncation_spec.rb @@ -2,7 +2,7 @@ require 'moped' require 'database_cleaner/moped/truncation' require File.dirname(__FILE__) + '/moped_examples' -describe DatabaseCleaner::Moped::Truncation do +RSpec.describe DatabaseCleaner::Moped::Truncation do around do |example| db_name = 'database_cleaner_specs' session = ::Moped::Session.new(['127.0.0.1:27017'], database: db_name) diff --git a/spec/database_cleaner/neo4j/base_spec.rb b/spec/database_cleaner/neo4j/base_spec.rb index 4d0c97c..503e0fe 100644 --- a/spec/database_cleaner/neo4j/base_spec.rb +++ b/spec/database_cleaner/neo4j/base_spec.rb @@ -2,7 +2,7 @@ require 'database_cleaner/neo4j/base' require 'database_cleaner/shared_strategy' module DatabaseCleaner - describe Neo4j do + RSpec.describe Neo4j do it { is_expected.to respond_to(:available_strategies) } end @@ -11,7 +11,7 @@ module DatabaseCleaner include ::DatabaseCleaner::Neo4j::Base end - describe ExampleStrategy do + RSpec.describe ExampleStrategy do it_should_behave_like "a generic strategy" it { is_expected.to respond_to(:db) } diff --git a/spec/database_cleaner/neo4j/transaction_spec.rb b/spec/database_cleaner/neo4j/transaction_spec.rb index d0b213b..7c4be0a 100644 --- a/spec/database_cleaner/neo4j/transaction_spec.rb +++ b/spec/database_cleaner/neo4j/transaction_spec.rb @@ -5,7 +5,7 @@ require 'database_cleaner/shared_strategy' module DatabaseCleaner module Neo4j - describe Transaction do + RSpec.describe Transaction do before(:all) do DatabaseCleaner[:neo4j, :connection => {:type => :server_db, :path => 'http://localhost:7474'}] end diff --git a/spec/database_cleaner/null_strategy_spec.rb b/spec/database_cleaner/null_strategy_spec.rb index 0b982f0..0345ffd 100644 --- a/spec/database_cleaner/null_strategy_spec.rb +++ b/spec/database_cleaner/null_strategy_spec.rb @@ -1,7 +1,7 @@ require "database_cleaner/null_strategy" module DatabaseCleaner - describe NullStrategy do + RSpec.describe NullStrategy do it 'responds to .start' do expect { NullStrategy.start }.not_to raise_error end diff --git a/spec/database_cleaner/ohm/truncation_spec.rb b/spec/database_cleaner/ohm/truncation_spec.rb index 9712ea0..bee581e 100644 --- a/spec/database_cleaner/ohm/truncation_spec.rb +++ b/spec/database_cleaner/ohm/truncation_spec.rb @@ -11,7 +11,7 @@ module OhmTests end end -describe DatabaseCleaner::Ohm::Truncation do +RSpec.describe DatabaseCleaner::Ohm::Truncation do around do |example| config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml")) Ohm.connect url: config['test']['url'] diff --git a/spec/database_cleaner/redis/base_spec.rb b/spec/database_cleaner/redis/base_spec.rb index 81f90c2..e30e576 100644 --- a/spec/database_cleaner/redis/base_spec.rb +++ b/spec/database_cleaner/redis/base_spec.rb @@ -3,7 +3,7 @@ require 'database_cleaner/redis/base' require 'database_cleaner/shared_strategy' module DatabaseCleaner - describe Redis do + RSpec.describe Redis do it { is_expected.to respond_to(:available_strategies) } end @@ -12,7 +12,7 @@ module DatabaseCleaner include ::DatabaseCleaner::Redis::Base end - describe ExampleStrategy do + RSpec.describe ExampleStrategy do it_should_behave_like "a generic strategy" it { is_expected.to respond_to(:db) } diff --git a/spec/database_cleaner/redis/truncation_spec.rb b/spec/database_cleaner/redis/truncation_spec.rb index 1b2bcfa..739648d 100644 --- a/spec/database_cleaner/redis/truncation_spec.rb +++ b/spec/database_cleaner/redis/truncation_spec.rb @@ -1,7 +1,7 @@ require 'redis' require 'database_cleaner/redis/truncation' -describe DatabaseCleaner::Redis::Truncation do +RSpec.describe DatabaseCleaner::Redis::Truncation do around do |example| config = YAML::load(File.open("#{File.dirname(__FILE__)}/../../../examples/config/redis.yml")) @redis = ::Redis.new :url => config['test']['url'] diff --git a/spec/database_cleaner/safeguard_spec.rb b/spec/database_cleaner/safeguard_spec.rb index 911b377..689bc59 100644 --- a/spec/database_cleaner/safeguard_spec.rb +++ b/spec/database_cleaner/safeguard_spec.rb @@ -2,7 +2,7 @@ require 'active_record' require 'database_cleaner/active_record/transaction' module DatabaseCleaner - describe Safeguard do + RSpec.describe Safeguard do let(:strategy) { DatabaseCleaner::ActiveRecord::Transaction } let(:cleaner) { Base.new(:autodetect) } diff --git a/spec/database_cleaner/sequel/base_spec.rb b/spec/database_cleaner/sequel/base_spec.rb index 16fa9e9..a2e9244 100644 --- a/spec/database_cleaner/sequel/base_spec.rb +++ b/spec/database_cleaner/sequel/base_spec.rb @@ -3,7 +3,7 @@ require 'database_cleaner/shared_strategy' require 'sequel' module DatabaseCleaner - describe Sequel do + RSpec.describe Sequel do it { is_expected.to respond_to(:available_strategies) } end @@ -12,7 +12,7 @@ module DatabaseCleaner include ::DatabaseCleaner::Sequel::Base end - describe ExampleStrategy do + RSpec.describe ExampleStrategy do it_should_behave_like "a generic strategy" it { is_expected.to respond_to(:db) } it { is_expected.to respond_to(:db=) } diff --git a/spec/database_cleaner/sequel/deletion_spec.rb b/spec/database_cleaner/sequel/deletion_spec.rb index e5255e6..bbca31e 100644 --- a/spec/database_cleaner/sequel/deletion_spec.rb +++ b/spec/database_cleaner/sequel/deletion_spec.rb @@ -7,11 +7,11 @@ require 'support/active_record/database_setup' module DatabaseCleaner module Sequel - describe Deletion do + RSpec.describe Deletion do it_should_behave_like "a generic strategy" end - shared_examples 'a Sequel deletion strategy' do + RSpec.shared_examples 'a Sequel deletion strategy' do let(:deletion) do d = Deletion.new d.db = db @@ -49,7 +49,7 @@ module DatabaseCleaner ] supported_configurations.each do |config| - describe "Sequel deletion (using a #{config[:url]} connection)" do + RSpec.describe "Sequel deletion (using a #{config[:url]} connection)" do let(:db) { ::Sequel.connect(config[:url], config[:connection_options]) } around do |example| diff --git a/spec/database_cleaner/sequel/transaction_spec.rb b/spec/database_cleaner/sequel/transaction_spec.rb index 85636ac..f24856c 100644 --- a/spec/database_cleaner/sequel/transaction_spec.rb +++ b/spec/database_cleaner/sequel/transaction_spec.rb @@ -4,7 +4,7 @@ require 'sequel' module DatabaseCleaner module Sequel - describe Transaction do + RSpec.describe Transaction do it_should_behave_like "a generic strategy" it_should_behave_like "a generic transaction strategy" diff --git a/spec/database_cleaner/sequel/truncation_spec.rb b/spec/database_cleaner/sequel/truncation_spec.rb index 4ebdb3f..e5f8a54 100644 --- a/spec/database_cleaner/sequel/truncation_spec.rb +++ b/spec/database_cleaner/sequel/truncation_spec.rb @@ -7,12 +7,12 @@ require 'support/active_record/database_setup' module DatabaseCleaner module Sequel - describe Truncation do + RSpec.describe Truncation do it_should_behave_like "a generic strategy" it_should_behave_like "a generic truncation strategy" end - shared_examples 'a Sequel truncation strategy' do + RSpec.shared_examples 'a Sequel truncation strategy' do # XXX: it'd be really nice if Truncation accepted db: constructor parameter let(:truncation) do @@ -67,7 +67,7 @@ module DatabaseCleaner end end - shared_examples_for 'a truncation strategy without autoincrement resets' do + RSpec.shared_examples_for 'a truncation strategy without autoincrement resets' do it "leaves AUTO_INCREMENT index alone by default (BUG: it should be reset instead)" do pending # Jordan Hollinger made everything reset auto increment IDs @@ -94,7 +94,7 @@ module DatabaseCleaner end end - shared_examples_for 'a truncation strategy that resets autoincrement keys by default' do + RSpec.shared_examples_for 'a truncation strategy that resets autoincrement keys by default' do it "resets AUTO_INCREMENT primary keys" do db.create_table!(:replaceable_trifles) { primary_key :id } table = db[:replaceable_trifles] @@ -124,7 +124,7 @@ module DatabaseCleaner ] supported_configurations.each do |config| - describe "Sequel truncation (using a #{config[:url]} connection)" do + RSpec.describe "Sequel truncation (using a #{config[:url]} connection)" do around do |example| helper = SequelHelper.new(config) helper.setup @@ -183,7 +183,7 @@ module DatabaseCleaner end half_supported_configurations.each do |config| - describe "Sequel truncation (using a #{config[:url]} connection)" do + RSpec.describe "Sequel truncation (using a #{config[:url]} connection)" do around do |example| helper = SequelHelper.new(config) helper.setup diff --git a/spec/database_cleaner/shared_strategy.rb b/spec/database_cleaner/shared_strategy.rb index 1c8eb7c..a8a4ed3 100644 --- a/spec/database_cleaner/shared_strategy.rb +++ b/spec/database_cleaner/shared_strategy.rb @@ -1,14 +1,14 @@ -shared_examples_for "a generic strategy" do +RSpec.shared_examples_for "a generic strategy" do it { is_expected.to respond_to(:db) } end -shared_examples_for "a generic truncation strategy" do +RSpec.shared_examples_for "a generic truncation strategy" do it { is_expected.to respond_to(:start) } it { is_expected.to respond_to(:clean) } it { is_expected.to respond_to(:cleaning) } end -shared_examples_for "a generic transaction strategy" do +RSpec.shared_examples_for "a generic transaction strategy" do it { is_expected.to respond_to(:start) } it { is_expected.to respond_to(:clean) } it { is_expected.to respond_to(:cleaning) } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0725357..b58e50e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,5 +8,7 @@ RSpec.configure do |config| # get run. config.filter_run :focus config.run_all_when_everything_filtered = true + + config.disable_monkey_patching! end From b9c7927f42522e73c8a19b173e5c3c280edbd2a4 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Mon, 30 Apr 2018 19:19:37 -0700 Subject: [PATCH 14/14] get guard working again. --- Gemfile.lock | 43 ++++++++++++++++++++++++++++++++++++++++ Guardfile | 6 ++++++ database_cleaner.gemspec | 2 ++ 3 files changed, 51 insertions(+) create mode 100644 Guardfile diff --git a/Gemfile.lock b/Gemfile.lock index 97b30f7..9890726 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -32,6 +32,7 @@ GEM bson_ext (1.12.5) bson (~> 1.12.5) builder (2.1.2) + coderay (1.1.2) couch_potato (1.3.0) activemodel couchrest (~> 1.2.0) @@ -105,10 +106,27 @@ GEM faraday_middleware (0.9.1) faraday (>= 0.7.4, < 0.10) fastercsv (1.5.5) + ffi (1.9.23) + ffi (1.9.23-java) + formatador (0.2.5) gherkin (2.11.6) json (>= 1.7.6) gherkin (2.11.6-java) json (>= 1.7.6) + guard (2.14.2) + formatador (>= 0.2.4) + listen (>= 2.7, < 4.0) + lumberjack (>= 1.0.12, < 2.0) + nenv (~> 0.1) + notiffany (~> 0.0) + pry (>= 0.9.12) + shellany (~> 0.0) + thor (>= 0.18.1) + guard-compat (1.2.1) + guard-rspec (4.7.3) + guard (~> 2.1) + guard-compat (~> 1.1) + rspec (>= 2.99.0, < 4.0) httparty (0.13.3) json (~> 1.8) multi_xml (>= 0.5.2) @@ -118,6 +136,11 @@ GEM json (1.8.6) json (1.8.6-java) json_pure (1.8.1) + listen (3.0.8) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + lumberjack (1.0.13) + method_source (0.9.0) mime-types (1.25.1) mongo (1.12.5) bson (= 1.12.5) @@ -138,6 +161,7 @@ GEM multipart-post (2.0.0) mysql (2.9.1) mysql2 (0.3.18) + nenv (0.3.0) neo4j-community (2.1.5) neo4j-core (3.0.8) activesupport @@ -153,13 +177,26 @@ GEM nest (1.1.2) redis net-http-persistent (2.9.4) + notiffany (0.1.1) + nenv (~> 0.1) + shellany (~> 0.0) ohm (0.1.5) nest (~> 1.0) os (0.9.6) pg (0.18.2) plucky (0.5.2) mongo (~> 1.5) + pry (0.11.3) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry (0.11.3-java) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + spoon (~> 0.0) rake (11.1.2) + rb-fsevent (0.10.3) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) rdoc (4.1.2) json (~> 1.4) redis (3.1.0) @@ -180,8 +217,12 @@ GEM rspec-support (~> 3.7.0) rspec-support (3.7.1) sequel (3.21.0) + shellany (0.0.1) + spoon (0.0.6) + ffi sqlite3 (1.3.10) stringex (1.5.1) + thor (0.20.0) tzinfo (0.3.48) uuidtools (2.1.5) zip (2.0.2) @@ -201,7 +242,9 @@ DEPENDENCIES datamapper dm-migrations dm-sqlite-adapter + guard-rspec json_pure + listen (~> 3.0.0) mongo (~> 1.12.0) mongo_ext mongo_mapper diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..7b6ade9 --- /dev/null +++ b/Guardfile @@ -0,0 +1,6 @@ +guard :rspec, cmd: "bundle exec rspec" do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch("spec/spec_helper.rb") { "spec" } +end + diff --git a/database_cleaner.gemspec b/database_cleaner.gemspec index 90cd42b..d42a97e 100644 --- a/database_cleaner.gemspec +++ b/database_cleaner.gemspec @@ -51,6 +51,8 @@ Gem::Specification.new do |s| s.add_development_dependency "couch_potato" s.add_development_dependency "sequel", "~> 3.21.0" s.add_development_dependency 'ohm', '~> 0.1.3' + s.add_development_dependency 'guard-rspec' + s.add_development_dependency "listen", "~> 3.0.0" # 3.1 requires Ruby >= 2.2 s.add_development_dependency "rspec" s.add_development_dependency "cucumber"