Switched over to DB reset strategy in tests and removed DatabaseCleaner because it can't guarantee a clean DB state for all test scenarios.

This commit is contained in:
Thorsten Eckel 2018-10-18 14:20:51 +02:00
parent 7d9c087a84
commit b538b05209
5 changed files with 24 additions and 14 deletions

View file

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

View file

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

View file

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

View file

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

View file

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