diff --git a/Gemfile.lock b/Gemfile.lock index 17694b4..7519c50 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,109 +6,15 @@ PATH GEM remote: https://rubygems.org/ specs: - activemodel (3.0.0) - activesupport (= 3.0.0) - builder (~> 2.1.2) - i18n (~> 0.4.1) - activerecord (3.0.0) - activemodel (= 3.0.0) - activesupport (= 3.0.0) - arel (~> 1.0.0) - tzinfo (~> 0.3.23) - activerecord-mysql2-adapter (0.0.3) - mysql2 - activesupport (3.0.0) - addressable (2.3.6) - arel (1.0.1) - activesupport (~> 3.0.0) - bcrypt (3.1.7) - bcrypt (3.1.7-java) - bcrypt-ruby (3.1.5) - bcrypt (>= 3.1.3) - bcrypt-ruby (3.1.5-java) - bcrypt (>= 3.1.3) - bson (1.12.5) - bson (1.12.5-java) - bson_ext (1.12.5) - bson (~> 1.12.5) builder (2.1.2) byebug (10.0.2) coderay (1.1.2) - couch_potato (1.3.0) - activemodel - couchrest (~> 1.2.0) - json (~> 1.6) - couchrest (1.2.1) - mime-types (>= 1.15) - multi_json (~> 1.7, ~> 1.0) - rest-client (~> 1.8.0) cucumber (1.2.1) builder (>= 2.1.2) diff-lcs (>= 1.1.3) gherkin (~> 2.11.0) json (>= 1.4.6) - data_objects (0.10.14) - addressable (~> 2.1) - datamapper (1.2.0) - dm-aggregates (~> 1.2.0) - dm-constraints (~> 1.2.0) - dm-core (~> 1.2.0) - dm-migrations (~> 1.2.0) - dm-serializer (~> 1.2.0) - dm-timestamps (~> 1.2.0) - dm-transactions (~> 1.2.0) - dm-types (~> 1.2.0) - dm-validations (~> 1.2.0) diff-lcs (1.3) - dm-aggregates (1.2.0) - dm-core (~> 1.2.0) - dm-constraints (1.2.0) - dm-core (~> 1.2.0) - dm-core (1.2.1) - addressable (~> 2.3) - dm-do-adapter (1.2.0) - data_objects (~> 0.10.6) - dm-core (~> 1.2.0) - dm-migrations (1.2.0) - dm-core (~> 1.2.0) - dm-serializer (1.2.2) - dm-core (~> 1.2.0) - fastercsv (~> 1.5) - json (~> 1.6) - json_pure (~> 1.6) - multi_json (~> 1.0) - dm-sqlite-adapter (1.2.0) - dm-do-adapter (~> 1.2.0) - do_sqlite3 (~> 0.10.6) - dm-timestamps (1.2.0) - dm-core (~> 1.2.0) - dm-transactions (1.2.0) - dm-core (~> 1.2.0) - dm-types (1.2.2) - bcrypt-ruby (~> 3.0) - dm-core (~> 1.2.0) - fastercsv (~> 1.5) - json (~> 1.6) - multi_json (~> 1.0) - stringex (~> 1.4) - uuidtools (~> 2.1) - dm-validations (1.2.0) - dm-core (~> 1.2.0) - do_jdbc (0.10.14-java) - data_objects (= 0.10.14) - do_sqlite3 (0.10.14) - data_objects (= 0.10.14) - do_sqlite3 (0.10.14-java) - data_objects (= 0.10.14) - do_jdbc (= 0.10.14) - jdbc-sqlite3 (>= 3.5.8) - domain_name (0.5.20180417) - unf (>= 0.0.5, < 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.25) ffi (1.9.25-java) formatador (0.2.5) @@ -130,68 +36,17 @@ GEM guard (~> 2.1) guard-compat (~> 1.1) rspec (>= 2.99.0, < 4.0) - http-cookie (1.0.3) - domain_name (~> 0.5) - httparty (0.13.3) - json (~> 1.8) - multi_xml (>= 0.5.2) - httpclient (2.5.3.3) - i18n (0.4.2) - jdbc-sqlite3 (3.8.10.1) json (1.8.6) json (1.8.6-java) - json_pure (1.8.1) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) lumberjack (1.0.13) method_source (0.9.0) - mime-types (2.99.3) - mongo (1.12.5) - bson (= 1.12.5) - mongo_ext (0.19.3) - mongo_mapper (0.12.0) - activemodel (~> 3.0) - activesupport (~> 3.0) - plucky (~> 0.5.2) - mongoid (2.2.6) - activemodel (~> 3.0.0) - mongo (~> 1.3) - tzinfo (~> 0.3.22) - mongoid-tree (0.7.0) - mongoid (~> 2.0) - moped (1.5.2) - multi_json (1.13.1) - multi_xml (0.5.5) - 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 - faraday (~> 0.9.0) - faraday_middleware (~> 0.9.1) - httparty - httpclient - json - neo4j-community (~> 2.1.1) - net-http-persistent - os - zip - nest (1.1.2) - redis - net-http-persistent (2.9.4) - netrc (0.11.0) notiffany (0.1.1) nenv (~> 0.1) shellany (~> 0.0) - ohm (0.1.5) - nest (~> 1.0) - os (0.9.6) - pg (0.18.2) - plucky (0.5.2) - mongo (~> 1.5) pry (0.11.3) coderay (~> 1.1.0) method_source (~> 0.9.0) @@ -203,11 +58,6 @@ GEM rb-fsevent (0.10.3) rb-inotify (0.9.10) ffi (>= 0.5.0, < 2) - redis (3.1.0) - rest-client (1.8.0) - http-cookie (>= 1.0.2, < 2.0) - mime-types (>= 1.16, < 3.0) - netrc (~> 0.7) rspec (3.7.0) rspec-core (~> 3.7.0) rspec-expectations (~> 3.7.0) @@ -221,56 +71,24 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.7.0) rspec-support (3.7.1) - sequel (3.21.0) shellany (0.0.1) spoon (0.0.6) ffi - sqlite3 (1.3.10) - stringex (1.5.1) thor (0.20.0) - tzinfo (0.3.48) - unf (0.1.4) - unf_ext - unf (0.1.4-java) - unf_ext (0.0.7.5) - uuidtools (2.1.5) - zip (2.0.2) PLATFORMS java ruby DEPENDENCIES - activerecord - activerecord-mysql2-adapter - bson_ext bundler byebug - couch_potato cucumber database_cleaner! - datamapper - dm-migrations - dm-sqlite-adapter guard-rspec - json_pure - listen (~> 3.0.0) - mongo (~> 1.12.0) - mongo_ext - mongo_mapper - mongoid - mongoid-tree - moped - mysql (~> 2.9.1) - mysql2 - neo4j-core - ohm (~> 0.1.3) - pg + listen rake rspec - sequel (~> 3.21.0) - sqlite3 - tzinfo BUNDLED WITH 1.17.3 diff --git a/README.markdown b/README.markdown index 568392f..cbed4af 100644 --- a/README.markdown +++ b/README.markdown @@ -1,9 +1,8 @@ - +(1.8.1).](https://github.com/DatabaseCleaner/database_cleaner/blob/v1.8.1/README.markdown)** + # Database Cleaner [![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner) @@ -58,7 +57,6 @@ MongoDB Redis * [database_cleaner-redis](adapters/database_cleaner-redis) - * [database_cleaner-ohm](adapters/database_cleaner-ohm) Neo4j * [database_cleaner-neo4j](adapters/database_cleaner-neo4j) @@ -320,40 +318,6 @@ One of my motivations for writing this library was to have an easy way to turn o After copying and pasting code to do this several times I decided to package it up as a gem and save everyone a bit of time. -## Common Errors - -#### DatabaseCleaner is trying to use the wrong ORM - -DatabaseCleaner has a deprecated autodetect mechanism where if you do not explicitly define your ORM it will use the first ORM it can detect that is loaded. - -Since ActiveRecord is the most common ORM used that is the first one checked for. - -Sometimes other libraries (e.g. ActiveAdmin) will load other ORMs (e.g. ActiveRecord) even though you are using a different ORM. This will result in DatabaseCleaner trying to use the wrong ORM (e.g. ActiveRecord) unless you explicitly require the correct adapter gem: - -```ruby -# Gemfile -gem "database_cleaner-mongoid" -``` - -### STDERR is being flooded when using Postgres - -If you are using Postgres and have foreign key constraints, the truncation strategy will cause a lot of extra noise to appear on STDERR (in the form of "NOTICE truncate cascades" messages). - -To silence these warnings set the following log level in your `postgresql.conf` file: - -```ruby -client_min_messages = warning -``` - -For ActiveRecord, you add the following parameter in your database.yml file: - -
-test:
-  adapter: postgresql
-  # ...
-  min_messages: WARNING
-
- ## Safeguards DatabaseCleaner comes with safeguards against: diff --git a/database_cleaner.gemspec b/database_cleaner.gemspec index ff26053..85a3306 100644 --- a/database_cleaner.gemspec +++ b/database_cleaner.gemspec @@ -37,36 +37,8 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake" spec.add_development_dependency "bundler" - spec.add_development_dependency "json_pure" - spec.add_development_dependency "activerecord-mysql2-adapter" unless RUBY_PLATFORM =~ /java/ - spec.add_development_dependency "activerecord" - spec.add_development_dependency "datamapper" - spec.add_development_dependency "dm-migrations" - spec.add_development_dependency "dm-sqlite-adapter" - spec.add_development_dependency "mongoid" - spec.add_development_dependency "tzinfo" - spec.add_development_dependency "mongoid-tree" - spec.add_development_dependency "mongo_mapper" - spec.add_development_dependency "mongo", "~> 1.12.0" - spec.add_development_dependency "moped" - spec.add_development_dependency "neo4j-core" - spec.add_development_dependency "couch_potato" - spec.add_development_dependency "sequel", "~> 3.21.0" - spec.add_development_dependency 'ohm', '~> 0.1.3' spec.add_development_dependency 'guard-rspec' - spec.add_development_dependency "listen", "~> 3.0.0" # 3.1 requires Ruby >= 2.2 + spec.add_development_dependency "listen" spec.add_development_dependency "rspec" spec.add_development_dependency "cucumber" - - unless RUBY_PLATFORM =~ /java/ - spec.add_development_dependency "mongo_ext" - spec.add_development_dependency "bson_ext" - spec.add_development_dependency 'mysql', '~> 2.9.1' - spec.add_development_dependency 'mysql2' - spec.add_development_dependency 'pg' - spec.add_development_dependency "sqlite3-ruby" if RUBY_VERSION < "1.9" - spec.add_development_dependency "sqlite3" if RUBY_VERSION >= "1.9" - else - spec.add_development_dependency "activerecord-jdbc-adapter" - end end diff --git a/lib/database_cleaner.rb b/lib/database_cleaner.rb index 90bec72..6210a7a 100644 --- a/lib/database_cleaner.rb +++ b/lib/database_cleaner.rb @@ -21,23 +21,10 @@ module DatabaseCleaner :clean, :clean_with, :cleaning, - - # TODO remove in 2.0 - :clean!, - :clean_with!, - :init_cleaners, - :add_cleaner, - :connections, - :remove_duplicates, ] => :configuration attr_accessor :allow_remote_database_url, :allow_production, :url_whitelist - def can_detect_orm? - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.can_detect_orm?` is deprecated, and will be removed in database_cleaner 2.0 with no replacement." - DatabaseCleaner::Base.autodetect_orm - end - private def configuration diff --git a/lib/database_cleaner/base.rb b/lib/database_cleaner/base.rb index d34e9bc..f2aba04 100644 --- a/lib/database_cleaner/base.rb +++ b/lib/database_cleaner/base.rb @@ -1,7 +1,6 @@ require 'database_cleaner/deprecation' require 'database_cleaner/null_strategy' require 'database_cleaner/safeguard' -require 'database_cleaner/orm_autodetector' require 'forwardable' module DatabaseCleaner @@ -13,10 +12,8 @@ module DatabaseCleaner end def initialize(desired_orm = nil, opts = {}) - @orm_autodetector = ORMAutodetector.new self.orm = desired_orm self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model) - self.strategy = orm_module && orm_module.default_strategy Safeguard.new.run end @@ -48,8 +45,7 @@ module DatabaseCleaner attr_reader :orm def orm=(desired_orm) - @orm = (desired_orm || :autodetect).to_sym - @orm = @orm_autodetector.orm if @orm == :autodetect + @orm = desired_orm && desired_orm.to_sym end extend Forwardable @@ -62,41 +58,13 @@ module DatabaseCleaner strategy end - # TODO remove the following methods in 2.0 - - def auto_detected? - DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].auto_detected?` is deprecated, and will be removed in database_cleaner 2.0 with no replacement." - @orm_autodetector.autodetected? - end - - def autodetect_orm - DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].autodetect_orm` is deprecated, and will be removed in database_cleaner 2.0 with no replacement." - @orm_autodetector.orm - end - - def clean! - DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].clean!` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].clean instead." - clean - end - - def clean_with! - DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].clean_with!` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].clean_with instead." - clean_with - end - - # TODO privatize the following methods in 2.0 + private def strategy_db=(desired_db) - if called_externally?(caller) - DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].strategy_db=` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].db=` instead." - end set_strategy_db(strategy, desired_db) end def set_strategy_db(strategy, desired_db) - if called_externally?(caller) - DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].set_strategy_db=` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].db=` instead." - end if strategy.respond_to? :db= strategy.db = desired_db elsif desired_db != :default @@ -105,45 +73,28 @@ module DatabaseCleaner end def create_strategy(*args) - if called_externally?(caller) - DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].create_strategy` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].strategy=` instead." - end strategy, *strategy_args = args orm_strategy(strategy).new(*strategy_args) end - private - - def orm_module - return unless [:active_record, :data_mapper, :mongo, :mongoid, :mongo_mapper, :moped, :couch_potato, :sequel, :ohm, :redis, :neo4j].include?(orm) - $LOAD_PATH.unshift File.expand_path("#{File.dirname(__FILE__)}/../../adapters/database_cleaner-#{orm}/lib") - require "database_cleaner/#{orm}" - orm_module_name = ORMAutodetector::ORMS[orm] - DatabaseCleaner.const_get(orm_module_name) - end - def orm_strategy(strategy) strategy_module_name = strategy.to_s.capitalize orm_module.const_get(strategy_module_name) rescue NameError - if orm != :active_record - DatabaseCleaner.deprecate <<-TEXT - The #{orm_module} adapter has been extracted to its own gem: database_cleaner-#{orm}, and will be removed from database_cleaner in 2.0. To silence this message, please replace `gem "database_cleaner"` with `gem "database_cleaner-#{orm}"` in your Gemfile. - TEXT - end - require_orm_strategy(orm, strategy) - retry - end - - def require_orm_strategy(orm, strategy) - $LOAD_PATH.unshift File.expand_path("#{File.dirname(__FILE__)}/../../adapters/database_cleaner-#{orm}/lib") - require "database_cleaner/#{orm}/#{strategy}" - rescue LoadError raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}" end - def called_externally?(caller) - __FILE__ != caller.first.split(":").first + def orm_module + orm_module_name = camelize(orm) + DatabaseCleaner.const_get(orm_module_name) + end + + def camelize(term) + string = term.to_s + string = string.sub(/^[a-z\d]*/) { |match| match.capitalize } + string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" } + string.gsub!("/", "::") + string end end end diff --git a/lib/database_cleaner/configuration.rb b/lib/database_cleaner/configuration.rb index 6e35983..bb32294 100644 --- a/lib/database_cleaner/configuration.rb +++ b/lib/database_cleaner/configuration.rb @@ -26,7 +26,7 @@ module DatabaseCleaner remove_duplicates end - # TODO privatize the following methods in 2.0 + private def add_cleaner(orm, opts = {}) self[[orm, opts]] = ::DatabaseCleaner::Base.new(orm, opts) @@ -54,26 +54,6 @@ module DatabaseCleaner attr_accessor :cleaners - def app_root - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.app_root` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner::ActiveRecord.config_file_location`, instead." - @app_root ||= Dir.pwd - end - - def app_root= value - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.app_root=` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner::ActiveRecord.config_file_location=`, instead." - @app_root = value - end - - def logger - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.logger` is deprecated, and will be removed in database_cleaner 2.0 with no replacement." - @logger ||= Logger.new(STDOUT).tap { |l| l.level = Logger::ERROR } - end - - def logger= value - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.logger=` is deprecated, and will be removed in database_cleaner 2.0 with no replacement." - @logger = value - end - def start connections.each { |connection| connection.start } end @@ -92,50 +72,18 @@ module DatabaseCleaner connections.each { |connection| connection.clean_with(*args) } end - # TODO remove the following methods in 2.0 - - def clean! - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.clean!` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner.clean`, instead." - clean - end - - def clean_with!(*args) - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.clean_with!` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner.clean_with`, instead." - clean_with(*args) - end - - def init_cleaners - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.init_cleaners` is deprecated, and will be removed in database_cleaner 2.0 with no replacement." - end + private def connections - if called_externally?(caller) - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.connections` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner.cleaners`, instead." - end - add_cleaner(:autodetect) if @cleaners.none? @cleaners.values end - # TODO privatize the following methods in 2.0 - def add_cleaner(orm, opts = {}) - if called_externally?(caller) - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.add_cleaner` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner.[]`, instead." - end @cleaners.add_cleaner(orm, opts = {}) end def remove_duplicates - if called_externally?(caller) - DatabaseCleaner.deprecate "Calling `DatabaseCleaner.remove_duplicates` is deprecated, and will be removed in database_cleaner 2.0 with no replacement." - end @cleaners.remove_duplicates end - - private - - def called_externally?(caller) - __FILE__ != caller.first.split(":").first - end end end diff --git a/lib/database_cleaner/orm_autodetector.rb b/lib/database_cleaner/orm_autodetector.rb deleted file mode 100644 index 04f432f..0000000 --- a/lib/database_cleaner/orm_autodetector.rb +++ /dev/null @@ -1,40 +0,0 @@ -module DatabaseCleaner - class ORMAutodetector - ORMS = { - active_record: "ActiveRecord", - data_mapper: "DataMapper", - mongo_mapper: "MongoMapper", - mongoid: "Mongoid", - couch_potato: "CouchPotato", - sequel: "Sequel", - moped: "Moped", - ohm: "Ohm", - redis: "Redis", - neo4j: "Neo4j", - } - - def orm - @autodetected = true - autodetected_orm or raise no_orm_detected_error - ORMS.key(autodetected_orm.to_s) - end - - def autodetected? - !!@autodetected - end - - private - - def autodetected_orm - ORMS.values.find do |orm| - Kernel.const_get(orm) rescue next - end - end - - def no_orm_detected_error - orm_list = ORMS.values.join(", ").sub(ORMS.values.last, "or #{ORMS.values.last}") - NoORMDetected.new("No known ORM was detected! Is #{orm_list} loaded?") - end - end - private_constant :ORMAutodetector -end diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index b7b2783..05b8560 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -1,131 +1,5 @@ -require 'active_record' -require 'data_mapper' -require 'mongo_mapper' -require 'mongoid' -require 'couch_potato' -require 'couch_potato' -require 'sequel' -require 'moped' -require 'ohm' -require 'redis' -require 'neo4j-core' - module DatabaseCleaner RSpec.describe Base do - describe "autodetect" do - it "should raise an error when no ORM is detected" 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" - - expect { subject }.to raise_error(DatabaseCleaner::NoORMDetected, <<-ERROR.chomp) -No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, CouchPotato, Sequel, Moped, Ohm, Redis, or Neo4j loaded? - ERROR - end - - it "should detect ActiveRecord first" do - expect(subject.orm).to eq :active_record - expect(subject).to be_auto_detected - end - - it "should detect DataMapper second" do - hide_const "ActiveRecord" - expect(subject.orm).to eq :data_mapper - expect(subject).to be_auto_detected - end - - it "should detect MongoMapper third" do - hide_const "ActiveRecord" - hide_const "DataMapper" - expect(subject.orm).to eq :mongo_mapper - expect(subject).to be_auto_detected - end - - it "should detect Mongoid fourth" do - hide_const "ActiveRecord" - hide_const "DataMapper" - hide_const "MongoMapper" - expect(subject.orm).to eq :mongoid - expect(subject).to be_auto_detected - end - - it "should detect CouchPotato fifth" do - hide_const "ActiveRecord" - hide_const "DataMapper" - hide_const "MongoMapper" - hide_const "Mongoid" - expect(subject.orm).to eq :couch_potato - expect(subject).to be_auto_detected - end - - it "should detect Sequel sixth" do - hide_const "ActiveRecord" - hide_const "DataMapper" - hide_const "MongoMapper" - hide_const "Mongoid" - hide_const "CouchPotato" - expect(subject.orm).to eq :sequel - expect(subject).to be_auto_detected - end - - it 'detects Moped seventh' do - hide_const "ActiveRecord" - hide_const "DataMapper" - hide_const "MongoMapper" - hide_const "Mongoid" - hide_const "CouchPotato" - hide_const "Sequel" - expect(subject.orm).to eq :moped - expect(subject).to be_auto_detected - end - - it 'detects Ohm eighth' do - hide_const "ActiveRecord" - hide_const "DataMapper" - hide_const "MongoMapper" - hide_const "Mongoid" - hide_const "CouchPotato" - hide_const "Sequel" - hide_const "Moped" - expect(subject.orm).to eq :ohm - expect(subject).to be_auto_detected - end - - it 'detects Redis ninth' do - hide_const "ActiveRecord" - hide_const "DataMapper" - hide_const "MongoMapper" - hide_const "Mongoid" - hide_const "CouchPotato" - hide_const "Sequel" - hide_const "Moped" - hide_const "Ohm" - expect(subject.orm).to eq :redis - expect(subject).to be_auto_detected - end - - it 'detects Neo4j tenth' do - hide_const "ActiveRecord" - hide_const "DataMapper" - hide_const "MongoMapper" - hide_const "Mongoid" - hide_const "CouchPotato" - hide_const "Sequel" - hide_const "Moped" - hide_const "Ohm" - hide_const "Redis" - expect(subject.orm).to eq :neo4j - expect(subject).to be_auto_detected - end - end - describe "comparison" do it "should be equal if orm and connection are the same" do one = DatabaseCleaner::Base.new(:active_record, :connection => :default) @@ -172,18 +46,8 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C expect(cleaner.orm).to eq :mongoid end - it "is autodetected if orm is not provided" do - cleaner = ::DatabaseCleaner::Base.new - expect(cleaner).to be_auto_detected - end - - it "is autodetected if you specify :autodetect" do - cleaner = ::DatabaseCleaner::Base.new "autodetect" - expect(cleaner).to be_auto_detected - end - - it "should default to autodetect upon initalisation" do - expect(subject).to be_auto_detected + it "should default to nil" do + expect(subject.orm).to be_nil end end end @@ -231,23 +95,29 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C end describe "clean_with" do - # FIXME hacky null strategy - # because you can't pass a NullStrategy to #clean_with + subject { described_class.new(:active_record) } - let(:strategy) { double(clean: true) } - - let(:strategy_class) do - require "database_cleaner/active_record/truncation" - DatabaseCleaner::ActiveRecord::Truncation - end + let(:strategy_class) { Class.new } + + before do + orm_module = Module.new do + def self.available_strategies + %i[truncation transaction deletion] + end + end + stub_const "DatabaseCleaner::ActiveRecord", orm_module + stub_const "DatabaseCleaner::ActiveRecord::Truncation", strategy_class + end + + let(:strategy) { double } before do - allow(::ActiveRecord::Base).to receive(:connection).and_return(double.as_null_object) allow(strategy_class).to receive(:new).and_return(strategy) end it "should pass all arguments to strategy initializer" do expect(strategy_class).to receive(:new).with(:dollar, :amet, ipsum: "random").and_return(strategy) + expect(strategy).to receive(:clean) subject.clean_with :truncation, :dollar, :amet, ipsum: "random" end @@ -257,14 +127,24 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C end it "should return the created strategy" do + expect(strategy).to receive(:clean) expect(subject.clean_with(:truncation)).to eq strategy end end describe "strategy=" do - let(:strategy_class) do - require "database_cleaner/active_record/truncation" - DatabaseCleaner::ActiveRecord::Truncation + subject { described_class.new(:active_record) } + + let(:strategy_class) { Class.new } + + before do + orm_module = Module.new do + def self.available_strategies + %i[truncation transaction deletion] + end + end + stub_const "DatabaseCleaner::ActiveRecord", orm_module + stub_const "DatabaseCleaner::ActiveRecord::Truncation", strategy_class end it "should look up and create a the named strategy for the current ORM" do @@ -299,13 +179,6 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C expect { subject.strategy = :horrible_plan }.to \ raise_error(UnknownStrategySpecified, "The 'horrible_plan' strategy does not exist for the active_record ORM! Available strategies: truncation, transaction, deletion") end - - it "loads and instantiates the described strategy" do - stub_const "DatabaseCleaner::ActiveRecord::Cunningplan", strategy_class - - subject.strategy = :cunningplan - expect(subject.strategy).to be_a strategy_class - end end describe "strategy" do @@ -323,18 +196,6 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C subject.orm = :desired_orm expect(subject.orm).to eq :desired_orm end - - context "orm isn't set" do - subject { described_class.new } - - it "should run autodetect if orm isn't set" do - expect(subject).to be_auto_detected - end - - it "should return the result of autodetect if orm isn't set" do - expect(subject.orm).to eq :active_record - end - end end describe "proxy methods" do @@ -365,68 +226,5 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C end end end - - describe "autodetected?" do - it "is true if auto detection was used" do - expect(subject).to be_auto_detected - end - - it "is false if orm was specified" do - subject = described_class.new(:a_orm) - expect(subject).to_not be_auto_detected - end - end - - describe 'set_default_orm_strategy' do - it 'sets strategy to :transaction for ActiveRecord' do - cleaner = DatabaseCleaner::Base.new(:active_record) - 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) - 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) - expect(cleaner.strategy).to be_instance_of DatabaseCleaner::MongoMapper::Truncation - end - - it 'sets strategy to :truncation for Mongoid' do - cleaner = DatabaseCleaner::Base.new(:mongoid) - 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) - expect(cleaner.strategy).to be_instance_of DatabaseCleaner::CouchPotato::Truncation - end - - it 'sets strategy to :transaction for Sequel' do - cleaner = DatabaseCleaner::Base.new(:sequel) - expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Sequel::Transaction - end - - it 'sets strategy to :truncation for Moped' do - cleaner = DatabaseCleaner::Base.new(:moped) - expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Moped::Truncation - end - - it 'sets strategy to :truncation for Ohm' do - cleaner = DatabaseCleaner::Base.new(:ohm) - expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Ohm::Truncation - end - - it 'sets strategy to :truncation for Redis' do - cleaner = DatabaseCleaner::Base.new(:redis) - expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Redis::Truncation - end - - it 'sets strategy to :transaction for Neo4j' do - cleaner = DatabaseCleaner::Base.new(:neo4j) - expect(cleaner.strategy).to be_instance_of DatabaseCleaner::Neo4j::Transaction - end - end end end diff --git a/spec/database_cleaner/configuration_spec.rb b/spec/database_cleaner/configuration_spec.rb index c7c84d1..f30bf7b 100644 --- a/spec/database_cleaner/configuration_spec.rb +++ b/spec/database_cleaner/configuration_spec.rb @@ -14,13 +14,6 @@ RSpec.describe DatabaseCleaner::Configuration do expect { subject[nil] }.to raise_error(DatabaseCleaner::NoORMDetected) end - it "should default to autodetection" do - require "active_record" - subject.strategy = :truncation - cleaner = subject.cleaners.values.first - expect(cleaner.orm).to eq :active_record - end - it "should accept :active_record" do cleaner = subject[:active_record] expect(cleaner).to be_a(DatabaseCleaner::Base) @@ -93,6 +86,9 @@ RSpec.describe DatabaseCleaner::Configuration do end it "should retrieve a db rather than create a new one" do + stub_const "DatabaseCleaner::ActiveRecord", Module.new + stub_const "DatabaseCleaner::ActiveRecord::Truncation", Class.new + connection = subject[:active_record] subject[:active_record].strategy = :truncation expect(subject[:active_record]).to equal connection @@ -245,15 +241,4 @@ RSpec.describe DatabaseCleaner::Configuration do end end end - - describe "app_root" do - it "should default to Dir.pwd" do - expect(subject.app_root).to eq Dir.pwd - end - - it "should store specific paths" do - subject.app_root = '/path/to' - expect(subject.app_root).to eq '/path/to' - end - end end