diff --git a/.rspec b/.rspec index 660ad19..53b85b9 100644 --- a/.rspec +++ b/.rspec @@ -1,4 +1,4 @@ --color --format documentation -mtime ---backtrace +--require spec_helper +--order rand diff --git a/Gemfile.lock b/Gemfile.lock index f57bdb6..9890726 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,7 @@ GEM bson_ext (1.12.5) bson (~> 1.12.5) builder (2.1.2) - coderay (1.1.0) + coderay (1.1.2) couch_potato (1.3.0) activemodel couchrest (~> 1.2.0) @@ -69,7 +58,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 +101,32 @@ 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) + 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 (1.8.3) + guard (2.14.2) 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) + 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) @@ -144,12 +136,11 @@ 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) + 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) @@ -170,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 @@ -185,66 +177,52 @@ 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.10.1) + pry (0.11.3) coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - pry (0.10.1-java) + method_source (~> 0.9.0) + pry (0.11.3-java) coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) + method_source (~> 0.9.0) 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) + 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) 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) + shellany (0.0.1) + spoon (0.0.6) ffi sqlite3 (1.3.10) stringex (1.5.1) - thor (0.14.6) + thor (0.20.0) tzinfo (0.3.48) uuidtools (2.1.5) zip (2.0.2) @@ -266,6 +244,7 @@ DEPENDENCIES dm-sqlite-adapter guard-rspec json_pure + listen (~> 3.0.0) mongo (~> 1.12.0) mongo_ext mongo_mapper @@ -278,7 +257,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 index 767287d..7b6ade9 100644 --- a/Guardfile +++ b/Guardfile @@ -1,9 +1,6 @@ -# A sample Guardfile -# More info at https://github.com/guard/guard#readme - -guard 'rspec', :version => 2 do +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" } + watch("spec/spec_helper.rb") { "spec" } end diff --git a/database_cleaner.gemspec b/database_cleaner.gemspec index 831af16..d42a97e 100644 --- a/database_cleaner.gemspec +++ b/database_cleaner.gemspec @@ -52,7 +52,8 @@ Gem::Specification.new do |s| 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 "listen", "~> 3.0.0" # 3.1 requires Ruby >= 2.2 + s.add_development_dependency "rspec" s.add_development_dependency "cucumber" unless RUBY_PLATFORM =~ /java/ 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 e6c1588..fc37927 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -1,7 +1,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 @@ -9,51 +9,53 @@ class FakeModel end end -module DatabaseCleaner - describe ActiveRecord do - it { should respond_to(:available_strategies) } +RSpec.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 - DatabaseCleaner.should_receive(:app_root).and_return("/path/to") - subject.should 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 + RSpec.describe ExampleStrategy do + let(:config_location) { '/path/to/config/database.yml' } - before { ::DatabaseCleaner::ActiveRecord.stub(: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" 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 @@ -66,12 +68,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 @@ -80,62 +82,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 @@ -143,14 +145,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 @@ -159,25 +161,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) { double("hash") } - before { ::ActiveRecord::Base.stub(:respond_to?).and_return(false) } - before { subject.stub(:connection_hash).and_return(hash) } + let(: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 8e3515d..c91c319 100644 --- a/spec/database_cleaner/active_record/transaction_spec.rb +++ b/spec/database_cleaner/active_record/transaction_spec.rb @@ -1,46 +1,41 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'database_cleaner/active_record/transaction' 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")} 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) - connection.stub(:respond_to?).with(:begin_transaction).and_return(:begin_transaction == 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.stub(:respond_to?).with(:increment_open_transactions).and_return(true) - 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 - connection.stub(:respond_to?).with(:increment_open_transactions).and_return(false) - ::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(:respond_to?).with(:increment_open_transactions).and_return(true) - 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 @@ -50,74 +45,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(: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) + 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(: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) + 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(: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) + 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(: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) + 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(: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) + 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 @@ -126,35 +106,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(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(true) - 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 @@ -163,11 +141,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 + 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_false + 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 0a36ab8..43c539e 100644 --- a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb @@ -1,38 +1,43 @@ -require 'spec_helper' require 'active_record' 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) { active_record_mysql2_setup } +RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do + let(:helper) { MySQL2Helper.new } - let(:connection) { 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') - User.count.should 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 } - User.create.id.should 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) { 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 a6eb8e9..d3373b1 100644 --- a/spec/database_cleaner/active_record/truncation/mysql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql_spec.rb @@ -1,38 +1,43 @@ -require 'spec_helper' require 'active_record' 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) { active_record_mysql_setup } +RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do + let(:helper) { MySQLHelper.new } - let(:connection) { 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') - User.count.should 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 } - User.create.id.should 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) { 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 15baf49..3c3faca 100644 --- a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb @@ -1,75 +1,78 @@ -require 'spec_helper' require 'active_record' 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 - active_record_pg_migrate - DatabaseCleaner::ActiveRecord::Truncation.new.clean - result = active_record_pg_connection.execute("select count(*) from schema_migrations;") - result.values.first.should eq ["2"] +RSpec.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) { 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 - 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') - User.count.should 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') - Agent.count.should eq 0 - end - - it "resets AUTO_INCREMENT index of table" do - 2.times { User.create } - User.delete_all - - connection.truncate_table('users') - - User.create.id.should 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 - connection.database_cleaner_table_cache.first.should match(/^public\./) - end - end - - it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { 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/shared_fast_truncation.rb b/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb index 2f2e9b3..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 @@ -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 75c212b..572d428 100644 --- a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb @@ -1,39 +1,39 @@ -require 'spec_helper' require 'active_record' require 'support/active_record/sqlite3_setup' require 'database_cleaner/active_record/truncation' -module ActiveRecord - module ConnectionAdapters - describe do - before(:all) { active_record_sqlite3_setup } +RSpec.describe DatabaseCleaner::ActiveRecord::Truncation do + let(:helper) { SQLite3Helper.new } - let(:connection) do - 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') - User.count.should eq 0 - end - - it "resets AUTO_INCREMENT index of table" do - 2.times { User.create } - User.delete_all - - connection.truncate_table('users') - - User.create.id.should eq 1 - end - end - end end end diff --git a/spec/database_cleaner/active_record/truncation_spec.rb b/spec/database_cleaner/active_record/truncation_spec.rb index 2ff665f..15cfc29 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' @@ -11,9 +10,9 @@ 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 - adapter.instance_methods.should include(:truncate_table) + expect(adapter.instance_methods).to include(:truncate_table) end end end @@ -23,64 +22,64 @@ end module DatabaseCleaner module ActiveRecord - describe Truncation do + RSpec.describe Truncation do 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 { 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 { 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 +88,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 +114,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,43 +135,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 } - its(:pre_count?) { should eq false } + + it 'should return false initially' do + 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 } - 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 + 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 d422b42..bbe1c76 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' @@ -7,187 +6,144 @@ 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| - strategy.stub(:to_ary => [strategy]) + allow(strategy).to receive_messages(:to_ary => [strategy]) } } 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 } it "should raise an error when no ORM is detected" do - running { cleaner }.should raise_error(DatabaseCleaner::NoORMDetected) + expect { cleaner }.to raise_error(DatabaseCleaner::NoORMDetected) 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') - 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 - 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') - 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 - 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') - 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 - 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') - 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 - 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') - 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 - 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') - 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') + stub_const('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 - 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') - 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') + stub_const('Redis', 'Redis mock') + stub_const('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') + stub_const('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 +153,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 +169,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 +180,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 +192,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 +202,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,26 +258,21 @@ 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=) + allow(strategy).to receive(: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) + expect(strategy).to receive(:db=).with(:a_db) subject.strategy_db = :a_db end 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) + expect(db).to receive(:==).with(:default).and_return(true) subject.strategy_db = db end @@ -336,40 +287,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 @@ -377,31 +328,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 @@ -414,14 +365,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 @@ -429,20 +380,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 @@ -451,20 +402,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 @@ -473,33 +424,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 @@ -508,12 +459,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 + 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_false + expect(subject.auto_detected?).to be_falsey end end @@ -521,29 +472,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.stub(:respond_to?).with(:available_strategies).and_return(true) - 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 @@ -552,11 +502,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 @@ -564,52 +514,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..8f30f86 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)] @@ -11,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) @@ -20,192 +20,192 @@ module DatabaseCleaner end end -describe ::DatabaseCleaner do - before(:each) { ::DatabaseCleaner.reset } +RSpec.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] - cleaner.should be_a(::DatabaseCleaner::Base) - cleaner.orm.should eq :active_record - ::DatabaseCleaner.connections.size.should eq 1 + 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 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 + 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 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 + 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 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 + 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 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 + cleaner = DatabaseCleaner[:moped] + 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 + cleaner = DatabaseCleaner[:ohm] + 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 + 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}] - cleaner.should be_a(::DatabaseCleaner::Base) - cleaner.orm.should eq :active_record - cleaner.db.should eq :first_connection + 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}] - ::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 + 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}] - ::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 context "connection/db retrieval" 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 + connection = ::DatabaseCleaner[:active_record] + ::DatabaseCleaner[:active_record].strategy = :truncation + expect(DatabaseCleaner[:active_record]).to equal 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 - ::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 context "single orm single connection" do - let(:connection) { ::DatabaseCleaner.connections.first } + let(:connection) { DatabaseCleaner.connections.first } it "should proxy strategy=" do - stratagum = double("stratagum") - connection.should_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") - connection.should_receive(:orm=).with(orm) - ::DatabaseCleaner.orm = orm + expect(connection).to receive(:orm=).with(orm) + DatabaseCleaner.orm = orm end it "should proxy start" do - connection.should_receive(:start) - ::DatabaseCleaner.start + expect(connection).to receive(:start) + DatabaseCleaner.start end it "should proxy clean" do - connection.should_receive(:clean) - ::DatabaseCleaner.clean + expect(connection).to receive(:clean) + DatabaseCleaner.clean end it 'should proxy cleaning' do - connection.should_receive(:cleaning) - ::DatabaseCleaner.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, {}) - ::DatabaseCleaner.clean_with stratagem, {} + expect(connection).to receive(:clean_with).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 - ::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") + 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 + 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 + DatabaseCleaner.clean end it "should initiate cleaning on each connection, yield, and finish cleaning each connection" do @@ -219,23 +219,23 @@ describe ::DatabaseCleaner do end end - ::DatabaseCleaner.cleaning do - active_record.started.should == true - data_mapper.started.should == true - active_record.cleaned.should == nil - data_mapper.cleaned.should == nil + DatabaseCleaner.cleaning do + 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 + DatabaseCleaner.clean_with stratagem end end @@ -243,22 +243,24 @@ 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 - ::DatabaseCleaner.connections_stub [active_record_1,active_record_2,data_mapper_1] + 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(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) - ::DatabaseCleaner.connections.size.should eq 3 - ::DatabaseCleaner.orm = :data_mapper - ::DatabaseCleaner.connections.size.should 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 @@ -266,16 +268,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] - 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 - ::DatabaseCleaner.strategy = strategy - ::DatabaseCleaner.connections.size.should eq 1 + expect(DatabaseCleaner.connections.size).to eq 2 + DatabaseCleaner.strategy = strategy + expect(DatabaseCleaner.connections.size).to eq 1 end end end @@ -285,61 +287,62 @@ 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 - ::DatabaseCleaner.connections.size.should eq 1 + DatabaseCleaner.remove_duplicates + 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 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 - subject.orm_module(:active_record).should 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 - subject.orm_module(:data_mapper).should 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 - subject.orm_module(:mongo_mapper).should 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 - subject.orm_module(:mongoid).should 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 - subject.orm_module(:mongo).should 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 - subject.orm_module(:couch_potato).should 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 - subject.orm_module(:neo4j).should 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/couch_potato/truncation_spec.rb b/spec/database_cleaner/couch_potato/truncation_spec.rb index 883adc0..8f4360a 100644 --- a/spec/database_cleaner/couch_potato/truncation_spec.rb +++ b/spec/database_cleaner/couch_potato/truncation_spec.rb @@ -1,39 +1,38 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'database_cleaner/couch_potato/truncation' require 'couch_potato' module DatabaseCleaner module CouchPotato - describe Truncation do + RSpec.describe Truncation do 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 { 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 { 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 { 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..ff5f822 100644 --- a/spec/database_cleaner/data_mapper/base_spec.rb +++ b/spec/database_cleaner/data_mapper/base_spec.rb @@ -1,10 +1,9 @@ -require 'spec_helper' require 'database_cleaner/data_mapper/base' require 'database_cleaner/shared_strategy' module DatabaseCleaner - describe DataMapper do - it { should respond_to(:available_strategies) } + RSpec.describe DataMapper do + it { is_expected.to respond_to(:available_strategies) } end module DataMapper @@ -12,18 +11,18 @@ module DatabaseCleaner include ::DatabaseCleaner::DataMapper::Base end - describe ExampleStrategy do + RSpec.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/transaction_spec.rb b/spec/database_cleaner/data_mapper/transaction_spec.rb index e4c1a97..91f827c 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' @@ -6,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 e294169..f7f82cd 100644 --- a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb @@ -1,40 +1,39 @@ -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 } +RSpec.describe DatabaseCleaner::DataMapper::Truncation do + let(:helper) { DataMapperSQLite3Helper.new } - let(:adapter) { DataMapperSQLite3Adapter } + let(:connection) do + helper.data_mapper_sqlite3_connection + end - let(:connection) do - 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]) - DmUser.count.should eq 0 - end - - it "resets AUTO_INCREMENT index of table" do - 2.times { DmUser.create } - DmUser.destroy - - connection.truncate_table(DmUser.storage_names[:default]) - - DmUser.create.id.should eq 1 - end + expect(DmUser.create.id).to eq 1 end end end 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 6c223d1..8eef96a 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' @@ -11,7 +10,7 @@ module ::DatabaseCleaner def start; end end - describe ExampleStrategy do + RSpec.describe ExampleStrategy do context "class methods" do subject { ExampleStrategy } @@ -35,14 +34,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 +50,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 ad9ad40..39cf267 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 @@ -29,7 +28,7 @@ module ::DatabaseCleaner end end - describe TruncationExample do + RSpec.describe TruncationExample do subject(:truncation_example) { TruncationExample.new } it "will start" do @@ -41,14 +40,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) } - its(:migration_storage_names) { should be_empty } + it { is_expected.not_to respond_to(:migration_storage_names) } end describe "initialize" do @@ -67,40 +65,40 @@ module ::DatabaseCleaner context "" do subject { TruncationExample.new( { :only => ["something"] } ) } - its(:only) { should eq ["something"] } - its(:except) { should eq [] } + it { expect(subject.only).to eq ["something"] } + it { expect(subject.except).to eq [] } end context "" do subject { TruncationExample.new( { :except => ["something"] } ) } - its(:only) { should eq nil } - its(: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"] } ) } - its(:reset_ids?) { should eq true } + it { expect(subject.reset_ids?).to eq true } end context "" do subject { TruncationExample.new( { :reset_ids => nil } ) } - its(:reset_ids?) { should eq false } + it { expect(subject.reset_ids?).to eq false } end context "" do subject { TruncationExample.new( { :pre_count => ["something"] } ) } - its(:pre_count?) { should eq true } + it { expect(subject.pre_count?).to eq true } end context "" do subject { TruncationExample.new( { :pre_count => nil } ) } - its(:pre_count?) { should eq false } + it { expect(subject.pre_count?).to eq false } end context "" do subject { MigrationExample.new } - its(:only) { should eq nil } - its(: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 @@ -108,8 +106,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/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 6736eeb..9cf1dff 100644 --- a/spec/database_cleaner/mongo/truncation_spec.rb +++ b/spec/database_cleaner/mongo/truncation_spec.rb @@ -1,73 +1,50 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'mongo' require 'database_cleaner/mongo/truncation' require File.dirname(__FILE__) + '/mongo_examples' -module DatabaseCleaner - module Mongo +RSpec.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 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 - actual_count.should 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 - - 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 - ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1) - truncation.clean - ensure_counts(MongoTest::Widget => 0, MongoTest::Gadget => 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 - ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1) - truncation.clean - ensure_counts(MongoTest::Widget => 0, MongoTest::Gadget => 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 - ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1) - truncation.clean - ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 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/base_spec.rb b/spec/database_cleaner/mongo_mapper/base_spec.rb index 9db2a8b..9b1d95d 100644 --- a/spec/database_cleaner/mongo_mapper/base_spec.rb +++ b/spec/database_cleaner/mongo_mapper/base_spec.rb @@ -1,10 +1,9 @@ -require 'spec_helper' require 'database_cleaner/mongo_mapper/base' require 'database_cleaner/shared_strategy' module DatabaseCleaner - describe MongoMapper do - it { should respond_to(:available_strategies) } + RSpec.describe MongoMapper do + it { is_expected.to respond_to(:available_strategies) } end module MongoMapper @@ -12,20 +11,20 @@ module DatabaseCleaner include ::DatabaseCleaner::MongoMapper::Base end - describe ExampleStrategy do + RSpec.describe ExampleStrategy do 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/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 fc26734..ce0a3f6 100644 --- a/spec/database_cleaner/mongo_mapper/truncation_spec.rb +++ b/spec/database_cleaner/mongo_mapper/truncation_spec.rb @@ -1,75 +1,49 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'mongo_mapper' require 'database_cleaner/mongo_mapper/truncation' require File.dirname(__FILE__) + '/mongo_examples' -module DatabaseCleaner - module MongoMapper +RSpec.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 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 - actual_count.should 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 - - def create_gadget(attrs={}) - Gadget.new({:name => 'some gadget'}.merge(attrs)).save! - end - - 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) - 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) - end - end - - context "when collections are provided to the :except option" do - 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) - 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 e7220ba..758dcff 100644 --- a/spec/database_cleaner/moped/truncation_spec.rb +++ b/spec/database_cleaner/moped/truncation_spec.rb @@ -1,82 +1,51 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'moped' require 'database_cleaner/moped/truncation' require File.dirname(__FILE__) + '/moped_examples' -module DatabaseCleaner - module Moped +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) + 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 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 - actual_count.should 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 - - 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 - ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1, MopedTest::System => 1) - truncation.clean - ensure_counts(MopedTest::Widget => 0, MopedTest::Gadget => 0, MopedTest::System => 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 - ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1) - truncation.clean - ensure_counts(MopedTest::Widget => 0, MopedTest::Gadget => 1) - 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 - ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1) - truncation.clean - ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 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 + diff --git a/spec/database_cleaner/neo4j/base_spec.rb b/spec/database_cleaner/neo4j/base_spec.rb index 50c7d18..503e0fe 100644 --- a/spec/database_cleaner/neo4j/base_spec.rb +++ b/spec/database_cleaner/neo4j/base_spec.rb @@ -1,10 +1,9 @@ -require 'spec_helper' require 'database_cleaner/neo4j/base' require 'database_cleaner/shared_strategy' module DatabaseCleaner - describe Neo4j do - it { should respond_to(:available_strategies) } + RSpec.describe Neo4j do + it { is_expected.to respond_to(:available_strategies) } end module Neo4j @@ -12,31 +11,31 @@ module DatabaseCleaner include ::DatabaseCleaner::Neo4j::Base end - describe ExampleStrategy do + RSpec.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/neo4j/transaction_spec.rb b/spec/database_cleaner/neo4j/transaction_spec.rb index 95eb806..7c4be0a 100644 --- a/spec/database_cleaner/neo4j/transaction_spec.rb +++ b/spec/database_cleaner/neo4j/transaction_spec.rb @@ -1,11 +1,11 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'neo4j-core' require 'database_cleaner/neo4j/transaction' +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 3fb1d14..0345ffd 100644 --- a/spec/database_cleaner/null_strategy_spec.rb +++ b/spec/database_cleaner/null_strategy_spec.rb @@ -1,28 +1,28 @@ -require 'spec_helper' +require "database_cleaner/null_strategy" module DatabaseCleaner - describe NullStrategy do - it 'responds to .start' do - expect { NullStrategy.start }.not_to raise_error(NoMethodError) + RSpec.describe NullStrategy do + 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(NoMethodError) - 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 diff --git a/spec/database_cleaner/ohm/truncation_spec.rb b/spec/database_cleaner/ohm/truncation_spec.rb index 0b21773..bee581e 100644 --- a/spec/database_cleaner/ohm/truncation_spec.rb +++ b/spec/database_cleaner/ohm/truncation_spec.rb @@ -1,70 +1,54 @@ -require File.dirname(__FILE__) + '/../../spec_helper' 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 + +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'] + @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 - @redis.keys.size.should eq 6 - Truncation.new.clean - @redis.keys.size.should 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 - Truncation.new(:only => ['*Widget*']).clean - @redis.keys.size.should eq 3 - @redis.get('DatabaseCleaner::Ohm::Gadget:id').should 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 - @redis.keys.size.should eq 6 - Truncation.new(:except => ['*Widget*']).clean - @redis.keys.size.should eq 3 - @redis.get('DatabaseCleaner::Ohm::Widget:id').should 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/base_spec.rb b/spec/database_cleaner/redis/base_spec.rb index d1aec4a..e30e576 100644 --- a/spec/database_cleaner/redis/base_spec.rb +++ b/spec/database_cleaner/redis/base_spec.rb @@ -1,11 +1,10 @@ -require 'spec_helper' require 'redis' require 'database_cleaner/redis/base' require 'database_cleaner/shared_strategy' module DatabaseCleaner - describe Redis do - it { should respond_to(:available_strategies) } + RSpec.describe Redis do + it { is_expected.to respond_to(:available_strategies) } end module Redis @@ -13,17 +12,17 @@ module DatabaseCleaner include ::DatabaseCleaner::Redis::Base end - describe ExampleStrategy do + RSpec.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 +30,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..739648d 100644 --- a/spec/database_cleaner/redis/truncation_spec.rb +++ b/spec/database_cleaner/redis/truncation_spec.rb @@ -1,62 +1,42 @@ -require File.dirname(__FILE__) + '/../../spec_helper' require 'redis' require 'database_cleaner/redis/truncation' +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'] -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 - @redis.keys.size.should eq 2 - Truncation.new.clean - @redis.keys.size.should 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 - @redis.keys.size.should eq 2 - Truncation.new(:only => ['Widge*']).clean - @redis.keys.size.should eq 1 - @redis.get('Gadget').should 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 - @redis.keys.size.should eq 2 - Truncation.new(:except => ['Widg*']).clean - @redis.keys.size.should eq 1 - @redis.get('Widget').should 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/safeguard_spec.rb b/spec/database_cleaner/safeguard_spec.rb index c2de148..1a1d4e3 100644 --- a/spec/database_cleaner/safeguard_spec.rb +++ b/spec/database_cleaner/safeguard_spec.rb @@ -1,9 +1,8 @@ -require 'spec_helper' 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 956afa8..a2e9244 100644 --- a/spec/database_cleaner/sequel/base_spec.rb +++ b/spec/database_cleaner/sequel/base_spec.rb @@ -1,11 +1,10 @@ -require 'spec_helper' require 'database_cleaner/sequel/base' require 'database_cleaner/shared_strategy' require 'sequel' module DatabaseCleaner - describe Sequel do - it { should respond_to(:available_strategies) } + RSpec.describe Sequel do + it { is_expected.to respond_to(:available_strategies) } end module Sequel @@ -13,20 +12,17 @@ module DatabaseCleaner include ::DatabaseCleaner::Sequel::Base end - describe ExampleStrategy do + RSpec.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 - 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..bbca31e 100644 --- a/spec/database_cleaner/sequel/deletion_spec.rb +++ b/spec/database_cleaner/sequel/deletion_spec.rb @@ -1,16 +1,17 @@ -require 'spec_helper' 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 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 @@ -34,9 +35,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 @@ -44,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 + RSpec.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/transaction_spec.rb b/spec/database_cleaner/sequel/transaction_spec.rb index 794481e..f24856c 100644 --- a/spec/database_cleaner/sequel/transaction_spec.rb +++ b/spec/database_cleaner/sequel/transaction_spec.rb @@ -1,11 +1,10 @@ -require 'spec_helper' require 'database_cleaner/sequel/transaction' require 'database_cleaner/shared_strategy' 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 003201f..e5f8a54 100644 --- a/spec/database_cleaner/sequel/truncation_spec.rb +++ b/spec/database_cleaner/sequel/truncation_spec.rb @@ -1,19 +1,18 @@ -require 'spec_helper' 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' 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 @@ -23,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 } @@ -38,9 +37,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 +48,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,14 +61,15 @@ 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 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 # in commit 6a0104382647e5c06578aeac586c0333c8944492 so I'm pretty sure # everything is meant to reset by default. @@ -84,9 +84,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 @@ -96,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,26 +122,39 @@ 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 + RSpec.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 } } - its(:pre_count?) { should eq false } + it 'should return false initially' do + 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 @@ -153,8 +164,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 @@ -162,16 +173,26 @@ 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 end end 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 + + 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/database_cleaner/shared_strategy.rb b/spec/database_cleaner/shared_strategy.rb index 62680d1..a8a4ed3 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) } +RSpec.shared_examples_for "a generic strategy" do + 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) } +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 - it { should respond_to(:start) } - it { should respond_to(:clean) } - it { should respond_to(:cleaning) } +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) } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 80d0a99..b58e50e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,21 +1,14 @@ -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| + # 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.disable_monkey_patching! end -alias running lambda diff --git a/spec/support/active_record/mysql2_setup.rb b/spec/support/active_record/mysql2_setup.rb index d0caf24..a180f98 100644 --- a/spec/support/active_record/mysql2_setup.rb +++ b/spec/support/active_record/mysql2_setup.rb @@ -1,27 +1,12 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' -module MySQL2Helper +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 @@ -33,12 +18,30 @@ 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" 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..8b99bde 100644 --- a/spec/support/active_record/mysql_setup.rb +++ b/spec/support/active_record/mysql_setup.rb @@ -1,36 +1,37 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' -module MySQLHelper +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 @@ -39,6 +40,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..a3c4438 100644 --- a/spec/support/active_record/postgresql_setup.rb +++ b/spec/support/active_record/postgresql_setup.rb @@ -1,31 +1,11 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' -module PostgreSQLHelper +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 @@ -33,16 +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 -RSpec.configure do |c| - c.include PostgreSQLHelper -end 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 diff --git a/spec/support/active_record/sqlite3_setup.rb b/spec/support/active_record/sqlite3_setup.rb index 0ba264a..325e1dd 100644 --- a/spec/support/active_record/sqlite3_setup.rb +++ b/spec/support/active_record/sqlite3_setup.rb @@ -1,29 +1,11 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' -module SQLite3Helper +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 @@ -33,8 +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 -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..32ef78a 100644 --- a/spec/support/data_mapper/sqlite3_setup.rb +++ b/spec/support/data_mapper/sqlite3_setup.rb @@ -1,28 +1,9 @@ require 'support/active_record/database_setup' require 'support/data_mapper/schema_setup' -module DataMapperSQLite3Helper - +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 @@ -32,8 +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 -RSpec.configure do |c| - c.include(DataMapperSQLite3Helper) -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