diff --git a/.gitignore b/.gitignore index 2f6900c..e9f555f 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ tmp/ .ruby-* *.iml coverage/ +.env diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2466790..38d6cba 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,6 +11,7 @@ require 'active_record' require 'active_support/core_ext/array' +puts "Using ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}" # Start Simplecov if RUBY_ENGINE == 'ruby' @@ -21,7 +22,7 @@ end database_file = SecureRandom.hex -ActiveRecord::Base.configurations = debug = { +ActiveRecord::Base.configurations = { default_env: { url: ENV['DATABASE_URL'].presence || "sqlite3://#{Dir.tmpdir}/#{database_file}.sqlite3", properties: { allowPublicKeyRetrieval: true } # for JRuby madness @@ -32,8 +33,6 @@ } } -puts "Testing with #{debug}" - # Configure ActiveRecord ActiveRecord::Migration.verbose = false ActiveRecord::Base.table_name_prefix = ENV['DB_PREFIX'].to_s @@ -76,15 +75,14 @@ def sqlite? # see: https://relishapp.com/rspec/rspec-core/v/3-8/docs/configuration/zero-monkey-patching-mode config.disable_monkey_patching! - if sqlite? + config.before(:suite) do - ENV['FLOCK_DIR'] = Dir.mktmpdir + ENV['FLOCK_DIR'] = Dir.mktmpdir if sqlite? end config.after(:suite) do - FileUtils.remove_entry_secure ENV['FLOCK_DIR'] + FileUtils.remove_entry_secure(ENV['FLOCK_DIR']) if sqlite? end - end end # Configure parallel specs @@ -94,14 +92,16 @@ def sqlite? # See: https://github.com/ClosureTree/with_advisory_lock ENV['WITH_ADVISORY_LOCK_PREFIX'] ||= SecureRandom.hex -ActiveRecord::Base.connection.recreate_database("closure_tree_test") unless sqlite? -puts "Testing with #{env_db} database, ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}" # Require our gem require 'closure_tree' +ActiveRecord::Tasks::DatabaseTasks.create_current('default_env', 'test') +ActiveRecord::Tasks::DatabaseTasks.create_current('secondary_env', 'test') + # Load test helpers require_relative 'support/schema' require_relative 'support/models' require_relative 'support/helpers' require_relative 'support/exceed_query_limit' require_relative 'support/query_counter' +puts "Testing with #{env_db} database" diff --git a/spec/support/models.rb b/spec/support/models.rb index 077a7b7..eee6841 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -10,7 +10,7 @@ def to_s def add_destroyed_tag # Proof for the tests that the destroy rather than the delete method was called: - DestroyedTag.create(name:) + DestroyedTag.create(name: to_s) end end @@ -30,7 +30,7 @@ def to_s def add_destroyed_tag # Proof for the tests that the destroy rather than the delete method was called: - DestroyedTag.create(name:) + DestroyedTag.create(name: to_s) end end diff --git a/test/test_helper.rb b/test/test_helper.rb index cdb6c6b..9c69323 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -7,8 +7,10 @@ require 'support/query_counter' require 'parallel' +puts "Using ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}" + database_file = SecureRandom.hex -ActiveRecord::Base.configurations = debug = { +ActiveRecord::Base.configurations = { default_env: { url: ENV['DATABASE_URL'].presence || "sqlite3://#{Dir.tmpdir}/#{database_file}.sqlite3", properties: { allowPublicKeyRetrieval: true } # for JRuby madness @@ -19,11 +21,8 @@ } } -puts "Testing with #{debug}" - ENV['WITH_ADVISORY_LOCK_PREFIX'] ||= SecureRandom.hex - def env_db @env_db ||= ActiveRecord::Base.connection_db_config.adapter.to_sym end @@ -37,9 +36,9 @@ def sqlite? env_db == :sqlite3 end -puts "Testing with #{env_db} database, ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}" DatabaseCleaner.strategy = :truncation +DatabaseCleaner.allow_remote_database_url = true module Minitest class Spec @@ -61,6 +60,10 @@ class Spec Thread.abort_on_exception = true require 'closure_tree' +ActiveRecord::Tasks::DatabaseTasks.create_current('default_env', 'test') +ActiveRecord::Tasks::DatabaseTasks.create_current('secondary_env', 'test') + require_relative '../spec/support/schema' require_relative '../spec/support/models' -ActiveRecord::Base.connection.recreate_database('closure_tree_test') unless sqlite? + +puts "Testing with #{env_db} database"