diff --git a/lib/tasks/zammad/db/reset.rake b/lib/tasks/zammad/db/reset.rake new file mode 100644 index 000000000..7545bf106 --- /dev/null +++ b/lib/tasks/zammad/db/reset.rake @@ -0,0 +1,13 @@ +namespace :zammad do + + namespace :db do + + desc 'Truncates and reseeds the database, clears the Cache and reloads the Settings' + task reset: :environment do + DatabaseCleaner.clean_with(:truncation) + Rails.application.load_seed + Cache.clear + Setting.reload + end + end +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 8ab5722dd..389ff0313 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 @@ -3,13 +3,12 @@ require 'rails_helper' RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do context 'no online_notifications foreign key' do - self.use_transactional_tests = false let(:existing_user_id) { User.first.id } context 'invalid User foreign key columns' do - it 'cleans up OnlineNotification#user_id' do + it 'cleans up OnlineNotification#user_id', db_strategy: :truncation do witout_foreign_key(:online_notifications, column: :user_id) create(:online_notification, user_id: 1337) @@ -20,13 +19,9 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do end.to change { OnlineNotification.count }.by(-1) - - # cleanup since we disabled - # transactions for this tests - valid.destroy end - it 'cleans up RecentView#created_by_id' do + it 'cleans up RecentView#created_by_id', db_strategy: :truncation do witout_foreign_key(:online_notifications, column: :user_id) witout_foreign_key(:recent_views, column: :created_by_id) @@ -38,13 +33,9 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do end.to change { RecentView.count }.by(-1) - - # cleanup since we disabled - # transactions for this tests - valid.destroy end - it 'cleans up Avatar#o_id' do + it 'cleans up Avatar#o_id', db_strategy: :truncation do witout_foreign_key(:online_notifications, column: :user_id) create(:avatar, object_lookup_id: ObjectLookup.by_name('User'), o_id: 1337) @@ -56,11 +47,6 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do end.to change { Avatar.count }.by(-1) - - # cleanup since we disabled - # transactions for this tests - valid_ticket.destroy - valid_user.destroy end end diff --git a/spec/support/db_strategies.rb b/spec/support/db_strategies.rb new file mode 100644 index 000000000..fcb14a652 --- /dev/null +++ b/spec/support/db_strategies.rb @@ -0,0 +1,8 @@ +RSpec.configure do |config| + + config.around(:each, db_strategy: :truncation) do |example| + self.use_transactional_tests = false + example.run + Rake::Task['zammad:db:reset'].execute + end +end diff --git a/spec/support/rake.rb b/spec/support/rake.rb new file mode 100644 index 000000000..27673128a --- /dev/null +++ b/spec/support/rake.rb @@ -0,0 +1,4 @@ +require 'rake' + +Rake::Task.clear +Zammad::Application.load_tasks diff --git a/spec/support/reset_system_before_suite.rb b/spec/support/reset_system_before_suite.rb index ea15a8664..7aed3210e 100644 --- a/spec/support/reset_system_before_suite.rb +++ b/spec/support/reset_system_before_suite.rb @@ -1,8 +1,5 @@ RSpec.configure do |config| config.before(:suite) do - DatabaseCleaner.clean_with(:truncation) - Rails.application.load_seed - Cache.clear - Setting.reload + Rake::Task['zammad:db:reset'].invoke end end diff --git a/spec/support/searchindex_backend.rb b/spec/support/searchindex_backend.rb index 263dbf1c9..10dfbefd0 100644 --- a/spec/support/searchindex_backend.rb +++ b/spec/support/searchindex_backend.rb @@ -1,5 +1,3 @@ -require 'rake' - module SearchindexBackendHelper def configure_elasticsearch(required: false) @@ -32,8 +30,6 @@ module SearchindexBackendHelper end def rebuild_searchindex - Rake::Task.clear - Zammad::Application.load_tasks Rake::Task['searchindex:rebuild'].execute end