Просмотр исходного кода

Added db_strategy (:truncaction) meta helper to switch to DB cleanup via truncation (from transaction) for single examples. We choose to stick to Rails/RSpec transactional cleanup over DatabaseCleaner transaction strategy because of performance reasons. Rails default performs between 30 and 50% better.

Thorsten Eckel 6 лет назад
Родитель
Сommit
02ba0e455e

+ 13 - 0
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

+ 3 - 17
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

+ 8 - 0
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

+ 4 - 0
spec/support/rake.rb

@@ -0,0 +1,4 @@
+require 'rake'
+
+Rake::Task.clear
+Zammad::Application.load_tasks

+ 1 - 4
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

+ 0 - 4
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