Skip to content

Commit

Permalink
Switched over to DB reset strategy in tests and removed DatabaseClean…
Browse files Browse the repository at this point in the history
…er because it can't guarantee a clean DB state for all test scenarios.
  • Loading branch information
thorsteneckel committed Oct 19, 2018
1 parent 7d9c087 commit b538b05
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -518,7 +517,6 @@ DEPENDENCIES
coveralls
daemons
dalli
database_cleaner
delayed_job_active_record
diffy
doorkeeper
Expand Down
21 changes: 18 additions & 3 deletions lib/tasks/zammad/db/reset.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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)
Expand All @@ -21,7 +21,7 @@
}.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)

Expand All @@ -35,7 +35,7 @@
}.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)
Expand Down
5 changes: 3 additions & 2 deletions spec/support/db_strategies.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b538b05

Please sign in to comment.