cleanup_obsolete_translations_spec.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe CleanupObsoleteTranslations, type: :db_migration do
  4. def create_parallel_records(locales:, customized: false, is_synchronized_from_codebase: false)
  5. source = Faker::Name.unique.name
  6. target_initial = Faker::Name.unique.name
  7. target = customized ? "#{target_initial}_changed" : target_initial
  8. Locale.first(locales).each do |locale|
  9. create(:translation, locale: locale.name, source:, target:, target_initial:, is_synchronized_from_codebase:)
  10. end
  11. end
  12. context 'when purging obsolete records' do
  13. it 'cleans them up' do
  14. 5.times { create_parallel_records(locales: 20) }
  15. expect { migrate }.to change(Translation, :count).by(-100)
  16. end
  17. it 'keeps codebase strings' do
  18. create_parallel_records(locales: 20, is_synchronized_from_codebase: true)
  19. expect { migrate }.not_to change(Translation, :count)
  20. end
  21. it 'keeps strings below the locale count threshold' do
  22. create_parallel_records(locales: 5)
  23. expect { migrate }.not_to change(Translation, :count)
  24. end
  25. it 'keeps customized strings' do
  26. create_parallel_records(locales: 20)
  27. create_parallel_records(locales: 20, customized: true)
  28. Translation.last.tap { |t| t.target = "#{t.target}_changed" }.save!
  29. expect { migrate }.to change(Translation, :count).by(-20)
  30. end
  31. end
  32. end