|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../db/migrate/20210922193500_move_translations_custom_fields_to_table" |
| 4 | + |
| 5 | +RSpec.describe "MoveTranslationsCustomFieldsToTable" do |
| 6 | + fab!(:topic) { Fabricate(:topic) } |
| 7 | + fab!(:post) { Fabricate(:post) } |
| 8 | + |
| 9 | + before do |
| 10 | + TopicCustomField.create!(topic_id: topic.id, name: "post_detected_lang", value: "en") |
| 11 | + |
| 12 | + PostCustomField.create!(post_id: post.id, name: "post_detected_lang", value: "es") |
| 13 | + |
| 14 | + TopicCustomField.create!( |
| 15 | + topic_id: topic.id, |
| 16 | + name: "translated_text", |
| 17 | + value: { fr: "Bonjour", es: "Hola" }.to_json, |
| 18 | + ) |
| 19 | + |
| 20 | + PostCustomField.create!( |
| 21 | + post_id: post.id, |
| 22 | + name: "translated_text", |
| 23 | + value: { en: "Hello", fr: "Salut" }.to_json, |
| 24 | + ) |
| 25 | + end |
| 26 | + |
| 27 | + it "correctly migrates custom fields to new tables" do |
| 28 | + MoveTranslationsCustomFieldsToTable.new.up |
| 29 | + |
| 30 | + # Check languages |
| 31 | + topic_lang = DiscourseTranslatorTopicLanguage.find_by(topic_id: topic.id) |
| 32 | + expect(topic_lang.detected_locale).to eq("en") |
| 33 | + |
| 34 | + post_lang = DiscourseTranslatorPostLanguage.find_by(post_id: post.id) |
| 35 | + expect(post_lang.detected_locale).to eq("es") |
| 36 | + |
| 37 | + # Check translations |
| 38 | + topic_translations = DiscourseTranslatorTopicTranslation.where(topic_id: topic.id) |
| 39 | + expect(topic_translations.count).to eq(2) |
| 40 | + expect(topic_translations.pluck(:locale)).to contain_exactly("fr", "es") |
| 41 | + expect(topic_translations.find_by(locale: "fr").translation).to eq("Bonjour") |
| 42 | + |
| 43 | + post_translations = DiscourseTranslatorPostTranslation.where(post_id: post.id) |
| 44 | + expect(post_translations.count).to eq(2) |
| 45 | + expect(post_translations.pluck(:locale)).to contain_exactly("en", "fr") |
| 46 | + expect(post_translations.find_by(locale: "en").translation).to eq("Hello") |
| 47 | + end |
| 48 | + |
| 49 | + it "properly cleans up data on rollback" do |
| 50 | + MoveTranslationsCustomFieldsToTable.new.up |
| 51 | + MoveTranslationsCustomFieldsToTable.new.down |
| 52 | + |
| 53 | + expect(DiscourseTranslatorTopicLanguage.count).to eq(0) |
| 54 | + expect(DiscourseTranslatorTopicTranslation.count).to eq(0) |
| 55 | + expect(DiscourseTranslatorPostLanguage.count).to eq(0) |
| 56 | + expect(DiscourseTranslatorPostTranslation.count).to eq(0) |
| 57 | + end |
| 58 | +end |
0 commit comments