remove_duplicate_translations_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe RemoveDuplicateTranslations, type: :db_migration do
  4. def create_translation(attributes)
  5. Translation.new(attributes.merge({ locale: 'de-de', created_by_id: 1, updated_by_id: 1 })).tap(&:save!)
  6. end
  7. context 'when having unsynchronized entries with duplicates' do
  8. let!(:unrelated_entry_one) { create_translation({ source: 'unknown entry', target: 'unknown translation', is_synchronized_from_codebase: false }) }
  9. let!(:unrelated_entry_two) { create_translation({ source: 'unknown entry', target: 'unknown translation', is_synchronized_from_codebase: false }) }
  10. let!(:unrelated_entry_three) { create_translation({ source: 'unknown entry', target: 'unknown translation', is_synchronized_from_codebase: false }) }
  11. before do
  12. migrate
  13. end
  14. it 'does not delete the first' do
  15. expect(unrelated_entry_one.reload).to be_present
  16. end
  17. it 'deletes the second' do
  18. expect { unrelated_entry_two.reload }.to raise_error(ActiveRecord::RecordNotFound)
  19. end
  20. it 'deletes the third' do
  21. expect { unrelated_entry_three.reload }.to raise_error(ActiveRecord::RecordNotFound)
  22. end
  23. end
  24. context 'when having multiple duplicate records of existing synchronized records' do
  25. # 'yes': 'ja' already exists as synchronized entry in the translations for 'de-de'.
  26. let!(:duplicate_one) { create_translation({ source: 'yes', target: 'ja', is_synchronized_from_codebase: false }) }
  27. let!(:duplicate_two) { create_translation({ source: 'yes', target: 'ja', is_synchronized_from_codebase: false }) }
  28. before do
  29. migrate
  30. end
  31. it 'deletes the first' do
  32. expect { duplicate_one.reload }.to raise_error(ActiveRecord::RecordNotFound)
  33. end
  34. it 'deletes the second' do
  35. expect { duplicate_two.reload }.to raise_error(ActiveRecord::RecordNotFound)
  36. end
  37. it 'keeps the original' do
  38. expect(Translation.where(locale: 'de-de', source: 'yes').count { |e| e.source == 'yes' }).to eq(1)
  39. end
  40. end
  41. context 'when no duplicates are present' do
  42. it 'does nothing' do
  43. expect { migrate }.not_to change(Translation, :count)
  44. end
  45. end
  46. end