remove all deprecated code and get the specs passing again.

This commit is contained in:
Micah Geisel 2020-02-01 14:07:00 -08:00 committed by Micah Geisel
parent a750956708
commit 9c580ace0c
9 changed files with 52 additions and 669 deletions

View file

@ -6,109 +6,15 @@ PATH
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: 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) builder (2.1.2)
byebug (10.0.2) byebug (10.0.2)
coderay (1.1.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) cucumber (1.2.1)
builder (>= 2.1.2) builder (>= 2.1.2)
diff-lcs (>= 1.1.3) diff-lcs (>= 1.1.3)
gherkin (~> 2.11.0) gherkin (~> 2.11.0)
json (>= 1.4.6) 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) 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)
ffi (1.9.25-java) ffi (1.9.25-java)
formatador (0.2.5) formatador (0.2.5)
@ -130,68 +36,17 @@ GEM
guard (~> 2.1) guard (~> 2.1)
guard-compat (~> 1.1) guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0) 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)
json (1.8.6-java) json (1.8.6-java)
json_pure (1.8.1)
listen (3.0.8) listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4) rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7) rb-inotify (~> 0.9, >= 0.9.7)
lumberjack (1.0.13) lumberjack (1.0.13)
method_source (0.9.0) 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) 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) notiffany (0.1.1)
nenv (~> 0.1) nenv (~> 0.1)
shellany (~> 0.0) 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) pry (0.11.3)
coderay (~> 1.1.0) coderay (~> 1.1.0)
method_source (~> 0.9.0) method_source (~> 0.9.0)
@ -203,11 +58,6 @@ GEM
rb-fsevent (0.10.3) rb-fsevent (0.10.3)
rb-inotify (0.9.10) rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2) 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 (3.7.0)
rspec-core (~> 3.7.0) rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0) rspec-expectations (~> 3.7.0)
@ -221,56 +71,24 @@ GEM
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0) rspec-support (~> 3.7.0)
rspec-support (3.7.1) rspec-support (3.7.1)
sequel (3.21.0)
shellany (0.0.1) shellany (0.0.1)
spoon (0.0.6) spoon (0.0.6)
ffi ffi
sqlite3 (1.3.10)
stringex (1.5.1)
thor (0.20.0) 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 PLATFORMS
java java
ruby ruby
DEPENDENCIES DEPENDENCIES
activerecord
activerecord-mysql2-adapter
bson_ext
bundler bundler
byebug byebug
couch_potato
cucumber cucumber
database_cleaner! database_cleaner!
datamapper
dm-migrations
dm-sqlite-adapter
guard-rspec guard-rspec
json_pure listen
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
rake rake
rspec rspec
sequel (~> 3.21.0)
sqlite3
tzinfo
BUNDLED WITH BUNDLED WITH
1.17.3 1.17.3

View file

@ -1,9 +1,8 @@
<!--
**If you're viewing this at https://github.com/DatabaseCleaner/database_cleaner, **If you're viewing this at https://github.com/DatabaseCleaner/database_cleaner,
you're reading the documentation for the `master` branch. you're reading the documentation for the `master` branch.
[View documentation for the latest release [View documentation for the latest release
(1.7.0).](https://github.com/DatabaseCleaner/database_cleaner/blob/v1.7.0/README.markdown)** (1.8.1).](https://github.com/DatabaseCleaner/database_cleaner/blob/v1.8.1/README.markdown)**
-->
# Database Cleaner # Database Cleaner
[![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/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 Redis
* [database_cleaner-redis](adapters/database_cleaner-redis) * [database_cleaner-redis](adapters/database_cleaner-redis)
* [database_cleaner-ohm](adapters/database_cleaner-ohm)
Neo4j Neo4j
* [database_cleaner-neo4j](adapters/database_cleaner-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. 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:
<pre>
test:
adapter: postgresql
# ...
min_messages: WARNING
</pre>
## Safeguards ## Safeguards
DatabaseCleaner comes with safeguards against: DatabaseCleaner comes with safeguards against:

View file

@ -37,36 +37,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake" spec.add_development_dependency "rake"
spec.add_development_dependency "bundler" 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 '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 "rspec"
spec.add_development_dependency "cucumber" 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 end

View file

@ -21,23 +21,10 @@ module DatabaseCleaner
:clean, :clean,
:clean_with, :clean_with,
:cleaning, :cleaning,
# TODO remove in 2.0
:clean!,
:clean_with!,
:init_cleaners,
:add_cleaner,
:connections,
:remove_duplicates,
] => :configuration ] => :configuration
attr_accessor :allow_remote_database_url, :allow_production, :url_whitelist 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 private
def configuration def configuration

View file

@ -1,7 +1,6 @@
require 'database_cleaner/deprecation' require 'database_cleaner/deprecation'
require 'database_cleaner/null_strategy' require 'database_cleaner/null_strategy'
require 'database_cleaner/safeguard' require 'database_cleaner/safeguard'
require 'database_cleaner/orm_autodetector'
require 'forwardable' require 'forwardable'
module DatabaseCleaner module DatabaseCleaner
@ -13,10 +12,8 @@ module DatabaseCleaner
end end
def initialize(desired_orm = nil, opts = {}) def initialize(desired_orm = nil, opts = {})
@orm_autodetector = ORMAutodetector.new
self.orm = desired_orm self.orm = desired_orm
self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model) 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 Safeguard.new.run
end end
@ -48,8 +45,7 @@ module DatabaseCleaner
attr_reader :orm attr_reader :orm
def orm=(desired_orm) def orm=(desired_orm)
@orm = (desired_orm || :autodetect).to_sym @orm = desired_orm && desired_orm.to_sym
@orm = @orm_autodetector.orm if @orm == :autodetect
end end
extend Forwardable extend Forwardable
@ -62,41 +58,13 @@ module DatabaseCleaner
strategy strategy
end end
# TODO remove the following methods in 2.0 private
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
def strategy_db=(desired_db) 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) set_strategy_db(strategy, desired_db)
end end
def set_strategy_db(strategy, desired_db) 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= if strategy.respond_to? :db=
strategy.db = desired_db strategy.db = desired_db
elsif desired_db != :default elsif desired_db != :default
@ -105,45 +73,28 @@ module DatabaseCleaner
end end
def create_strategy(*args) 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 strategy, *strategy_args = args
orm_strategy(strategy).new(*strategy_args) orm_strategy(strategy).new(*strategy_args)
end 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) def orm_strategy(strategy)
strategy_module_name = strategy.to_s.capitalize strategy_module_name = strategy.to_s.capitalize
orm_module.const_get(strategy_module_name) orm_module.const_get(strategy_module_name)
rescue NameError 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(', ')}" raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
end end
def called_externally?(caller) def orm_module
__FILE__ != caller.first.split(":").first 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 end
end end

View file

@ -26,7 +26,7 @@ module DatabaseCleaner
remove_duplicates remove_duplicates
end end
# TODO privatize the following methods in 2.0 private
def add_cleaner(orm, opts = {}) def add_cleaner(orm, opts = {})
self[[orm, opts]] = ::DatabaseCleaner::Base.new(orm, opts) self[[orm, opts]] = ::DatabaseCleaner::Base.new(orm, opts)
@ -54,26 +54,6 @@ module DatabaseCleaner
attr_accessor :cleaners 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 def start
connections.each { |connection| connection.start } connections.each { |connection| connection.start }
end end
@ -92,50 +72,18 @@ module DatabaseCleaner
connections.each { |connection| connection.clean_with(*args) } connections.each { |connection| connection.clean_with(*args) }
end end
# TODO remove the following methods in 2.0 private
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
def connections 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 @cleaners.values
end end
# TODO privatize the following methods in 2.0
def add_cleaner(orm, opts = {}) 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 = {}) @cleaners.add_cleaner(orm, opts = {})
end end
def remove_duplicates 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 @cleaners.remove_duplicates
end end
private
def called_externally?(caller)
__FILE__ != caller.first.split(":").first
end
end end
end end

View file

@ -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

View file

@ -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 module DatabaseCleaner
RSpec.describe Base do 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 describe "comparison" do
it "should be equal if orm and connection are the same" do it "should be equal if orm and connection are the same" do
one = DatabaseCleaner::Base.new(:active_record, :connection => :default) 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 expect(cleaner.orm).to eq :mongoid
end end
it "is autodetected if orm is not provided" do it "should default to nil" do
cleaner = ::DatabaseCleaner::Base.new expect(subject.orm).to be_nil
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
end end
end end
end end
@ -231,23 +95,29 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C
end end
describe "clean_with" do describe "clean_with" do
# FIXME hacky null strategy subject { described_class.new(:active_record) }
# because you can't pass a NullStrategy to #clean_with
let(:strategy) { double(clean: true) } let(:strategy_class) { Class.new }
let(:strategy_class) do before do
require "database_cleaner/active_record/truncation" orm_module = Module.new do
DatabaseCleaner::ActiveRecord::Truncation def self.available_strategies
end %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 before do
allow(::ActiveRecord::Base).to receive(:connection).and_return(double.as_null_object)
allow(strategy_class).to receive(:new).and_return(strategy) allow(strategy_class).to receive(:new).and_return(strategy)
end end
it "should pass all arguments to strategy initializer" do 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_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" subject.clean_with :truncation, :dollar, :amet, ipsum: "random"
end end
@ -257,14 +127,24 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C
end end
it "should return the created strategy" do it "should return the created strategy" do
expect(strategy).to receive(:clean)
expect(subject.clean_with(:truncation)).to eq strategy expect(subject.clean_with(:truncation)).to eq strategy
end end
end end
describe "strategy=" do describe "strategy=" do
let(:strategy_class) do subject { described_class.new(:active_record) }
require "database_cleaner/active_record/truncation"
DatabaseCleaner::ActiveRecord::Truncation 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 end
it "should look up and create a the named strategy for the current ORM" do 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 \ 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") raise_error(UnknownStrategySpecified, "The 'horrible_plan' strategy does not exist for the active_record ORM! Available strategies: truncation, transaction, deletion")
end 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 end
describe "strategy" do describe "strategy" do
@ -323,18 +196,6 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C
subject.orm = :desired_orm subject.orm = :desired_orm
expect(subject.orm).to eq :desired_orm expect(subject.orm).to eq :desired_orm
end 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 end
describe "proxy methods" do describe "proxy methods" do
@ -365,68 +226,5 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C
end end
end 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
end end

View file

@ -14,13 +14,6 @@ RSpec.describe DatabaseCleaner::Configuration do
expect { subject[nil] }.to raise_error(DatabaseCleaner::NoORMDetected) expect { subject[nil] }.to raise_error(DatabaseCleaner::NoORMDetected)
end 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 it "should accept :active_record" do
cleaner = subject[:active_record] cleaner = subject[:active_record]
expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner).to be_a(DatabaseCleaner::Base)
@ -93,6 +86,9 @@ RSpec.describe DatabaseCleaner::Configuration do
end end
it "should retrieve a db rather than create a new one" do 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] connection = subject[:active_record]
subject[:active_record].strategy = :truncation subject[:active_record].strategy = :truncation
expect(subject[:active_record]).to equal connection expect(subject[:active_record]).to equal connection
@ -245,15 +241,4 @@ RSpec.describe DatabaseCleaner::Configuration do
end end
end 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 end