From b538b05209b68192cf5c38ae7173a03a4bf46898 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 18 Oct 2018 14:20:51 +0200 Subject: [PATCH] Switched over to DB reset strategy in tests and removed DatabaseCleaner because it can't guarantee a clean DB state for all test scenarios. --- Gemfile | 4 ---- Gemfile.lock | 2 -- lib/tasks/zammad/db/reset.rake | 21 ++++++++++++++++--- ...7_remove_invalid_user_foreign_keys_spec.rb | 6 +++--- spec/support/db_strategies.rb | 5 +++-- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index 8ef71c43f..9e097c1ed 100644 --- a/Gemfile +++ b/Gemfile @@ -176,10 +176,6 @@ group :development, :test do # record and replay TCP/HTTP transactions gem 'tcr' gem 'vcr' - - # database cleanup when transactions are not possible - # and DB initialization before running RSpec suite - gem 'database_cleaner' end # Want to extend Zammad with additional gems? diff --git a/Gemfile.lock b/Gemfile.lock index a7b24e420..361041523 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -113,7 +113,6 @@ GEM crass (1.0.4) daemons (1.2.5) dalli (2.7.6) - database_cleaner (1.7.0) debug_inspector (0.0.3) delayed_job (4.1.3) activesupport (>= 3.0, < 5.2) @@ -518,7 +517,6 @@ DEPENDENCIES coveralls daemons dalli - database_cleaner delayed_job_active_record diffy doorkeeper diff --git a/lib/tasks/zammad/db/reset.rake b/lib/tasks/zammad/db/reset.rake index 7545bf106..865177c96 100644 --- a/lib/tasks/zammad/db/reset.rake +++ b/lib/tasks/zammad/db/reset.rake @@ -2,10 +2,25 @@ namespace :zammad do namespace :db do - desc 'Truncates and reseeds the database, clears the Cache and reloads the Settings' + desc 'Drops, recreates and seeds the database, clears the Cache and reloads the Settings' task reset: :environment do - DatabaseCleaner.clean_with(:truncation) - Rails.application.load_seed + + # we loop over each dependent task to be able to + # execute them and their prerequisites multiple times (in tests) + # there is no way in rake to achive that + %w[db:drop:_unsafe db:create db:schema:load db:seed].each do |task| + + begin + $stdout = StringIO.new if task == 'db:schema:load'.freeze + + Rake::Task[task].reenable + Rake::Task[task].invoke + ensure + $stdout = STDOUT + end + end + + ActiveRecord::Base.connection.reconnect! Cache.clear Setting.reload end diff --git a/spec/db/migrate/issue_1977_remove_invalid_user_foreign_keys_spec.rb b/spec/db/migrate/issue_1977_remove_invalid_user_foreign_keys_spec.rb index 389ff0313..ea99d6701 100644 --- a/spec/db/migrate/issue_1977_remove_invalid_user_foreign_keys_spec.rb +++ b/spec/db/migrate/issue_1977_remove_invalid_user_foreign_keys_spec.rb @@ -8,7 +8,7 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do context 'invalid User foreign key columns' do - it 'cleans up OnlineNotification#user_id', db_strategy: :truncation do + it 'cleans up OnlineNotification#user_id', db_strategy: :reset do witout_foreign_key(:online_notifications, column: :user_id) create(:online_notification, user_id: 1337) @@ -21,7 +21,7 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do }.by(-1) end - it 'cleans up RecentView#created_by_id', db_strategy: :truncation do + it 'cleans up RecentView#created_by_id', db_strategy: :reset do witout_foreign_key(:online_notifications, column: :user_id) witout_foreign_key(:recent_views, column: :created_by_id) @@ -35,7 +35,7 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do }.by(-1) end - it 'cleans up Avatar#o_id', db_strategy: :truncation do + it 'cleans up Avatar#o_id', db_strategy: :reset do witout_foreign_key(:online_notifications, column: :user_id) create(:avatar, object_lookup_id: ObjectLookup.by_name('User'), o_id: 1337) diff --git a/spec/support/db_strategies.rb b/spec/support/db_strategies.rb index fcb14a652..a4a8c831f 100644 --- a/spec/support/db_strategies.rb +++ b/spec/support/db_strategies.rb @@ -1,8 +1,9 @@ RSpec.configure do |config| - config.around(:each, db_strategy: :truncation) do |example| + config.around(:each, db_strategy: :reset) do |example| self.use_transactional_tests = false example.run - Rake::Task['zammad:db:reset'].execute + Rake::Task['zammad:db:reset'].reenable + Rake::Task['zammad:db:reset'].invoke end end