|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../db/migrate/20250205082401_move_translations_custom_fields_to_table" |
| 4 | + |
| 5 | +module DiscourseTranslator |
| 6 | + describe MoveTranslationsCustomFieldsToTable do |
| 7 | + let!(:batch_size) { 5 } |
| 8 | + |
| 9 | + before { described_class.const_set(:BATCH_SIZE, batch_size) } |
| 10 | + |
| 11 | + def create_custom_fields(count) |
| 12 | + count.times do |
| 13 | + post = Fabricate(:post) |
| 14 | + topic = Fabricate(:topic) |
| 15 | + |
| 16 | + post.custom_fields[DETECTED_LANG_CUSTOM_FIELD] = "pt" |
| 17 | + post.save_custom_fields |
| 18 | + |
| 19 | + topic.custom_fields[DETECTED_LANG_CUSTOM_FIELD] = "es" |
| 20 | + topic.save_custom_fields |
| 21 | + |
| 22 | + post.custom_fields[TRANSLATED_CUSTOM_FIELD] = { |
| 23 | + en_GB: "The Romance of the Three Kingdoms", |
| 24 | + de: "Die Romanze der Drei Königreiche", |
| 25 | + } |
| 26 | + post.save_custom_fields |
| 27 | + |
| 28 | + topic.custom_fields[TRANSLATED_CUSTOM_FIELD] = { |
| 29 | + en_GB: "The Romance of the Three Kingdoms", |
| 30 | + de: "Die Romanze der Drei Königreiche", |
| 31 | + } |
| 32 | + topic.save_custom_fields |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + it "correctly migrates custom fields in batches" do |
| 37 | + # batch size is 5 |
| 38 | + create_custom_fields(12) |
| 39 | + |
| 40 | + migration = described_class.new |
| 41 | + migration.up |
| 42 | + |
| 43 | + # 12 posts * 2 translations each |
| 44 | + expect(PostLocale.count).to eq(12) |
| 45 | + expect(PostTranslation.count).to eq(24) |
| 46 | + |
| 47 | + # 12 topics * 2 translations each |
| 48 | + expect(TopicLocale.count).to eq(12) |
| 49 | + expect(TopicTranslation.count).to eq(24) |
| 50 | + |
| 51 | + expect(PostLocale.first.detected_locale).to eq("pt") |
| 52 | + |
| 53 | + expect(PostTranslation.where(post_id: Post.first.id).pluck(:locale, :translation)).to include( |
| 54 | + ["en_GB", "The Romance of the Three Kingdoms"], |
| 55 | + ["de", "Die Romanze der Drei Königreiche"], |
| 56 | + ) |
| 57 | + |
| 58 | + migration.down |
| 59 | + expect(PostLocale.count).to eq(0) |
| 60 | + expect(PostTranslation.count).to eq(0) |
| 61 | + expect(TopicLocale.count).to eq(0) |
| 62 | + expect(TopicTranslation.count).to eq(0) |
| 63 | + end |
| 64 | + |
| 65 | + it "ignores invalid JSON in translated_text" do |
| 66 | + post = Fabricate(:post) |
| 67 | + post.custom_fields[TRANSLATED_CUSTOM_FIELD] = "invalid json" |
| 68 | + post.save_custom_fields(true) |
| 69 | + |
| 70 | + migration = described_class.new |
| 71 | + expect { migration.up }.not_to raise_error |
| 72 | + expect(PostTranslation.count).to eq(0) |
| 73 | + end |
| 74 | + |
| 75 | + it "ignores translations with locale longer than 20 chars" do |
| 76 | + post = Fabricate(:post) |
| 77 | + post.custom_fields[TRANSLATED_CUSTOM_FIELD] = { very_very_long_locale_name: "test" } |
| 78 | + post.custom_fields[DETECTED_LANG_CUSTOM_FIELD] = "very_very_long_locale_name" |
| 79 | + post.save_custom_fields(true) |
| 80 | + |
| 81 | + migration = described_class.new |
| 82 | + migration.up |
| 83 | + expect(PostLocale.count).to eq(0) |
| 84 | + expect(PostTranslation.count).to eq(0) |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments