|
45 | 45 | expect(topic_view.posts.first.association(:content_locale)).to be_loaded |
46 | 46 | end |
47 | 47 |
|
48 | | - it "preloads translations when experimental_inline_translation is enabled" do |
49 | | - SiteSetting.experimental_inline_translation = true |
50 | | - |
51 | | - en_post.set_translation("es", "Hola") |
52 | | - es_post.set_translation("en", "Hello") |
53 | | - |
54 | | - topic_view = TopicView.new(topic) |
55 | | - serializer = TopicViewSerializer.new(topic_view, scope: Guardian.new(user), root: false) |
56 | | - |
57 | | - topic_view.posts.reload |
58 | | - |
59 | | - queries = |
60 | | - track_sql_queries do |
61 | | - json = serializer.as_json |
62 | | - json[:post_stream][:posts].each { |p| p[:translations] } |
63 | | - end |
64 | | - |
65 | | - translation_queries = |
66 | | - queries.count { |q| q.include?("discourse_translator_post_translations") } |
67 | | - expect(translation_queries).to eq(1) |
68 | | - expect(topic_view.posts.first.association(:translations)).to be_loaded |
| 48 | + describe "preloading translations with SiteSetting.experimental_inline_translations" do |
| 49 | + before do |
| 50 | + SiteSetting.experimental_inline_translation = true |
| 51 | + |
| 52 | + en_post.set_translation("es", "Hola") |
| 53 | + es_post.set_translation("en", "Hello") |
| 54 | + end |
| 55 | + |
| 56 | + it "does not preload translations when user locale matches site default locale" do |
| 57 | + SiteSetting.default_locale = "en" |
| 58 | + I18n.locale = "en" |
| 59 | + |
| 60 | + topic_view = TopicView.new(topic) |
| 61 | + serializer = TopicViewSerializer.new(topic_view, scope: Guardian.new(user), root: false) |
| 62 | + |
| 63 | + topic_view.posts.reload |
| 64 | + |
| 65 | + queries = |
| 66 | + track_sql_queries do |
| 67 | + json = serializer.as_json |
| 68 | + json[:post_stream][:posts].each { |p| p[:translations] } |
| 69 | + end |
| 70 | + |
| 71 | + expect(queries.count { |q| q.include?("discourse_translator_post_translations") }).to eq(3) |
| 72 | + end |
| 73 | + |
| 74 | + it "does not preload translations when locales are different and not in automatic_translation_target_languages" do |
| 75 | + SiteSetting.default_locale = "en" |
| 76 | + I18n.locale = "ja" |
| 77 | + SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 1 |
| 78 | + SiteSetting.automatic_translation_target_languages = "es" |
| 79 | + |
| 80 | + topic_view = TopicView.new(topic) |
| 81 | + serializer = TopicViewSerializer.new(topic_view, scope: Guardian.new(user), root: false) |
| 82 | + |
| 83 | + topic_view.posts.reload |
| 84 | + |
| 85 | + queries = |
| 86 | + track_sql_queries do |
| 87 | + json = serializer.as_json |
| 88 | + json[:post_stream][:posts].each { |p| p[:translations] } |
| 89 | + end |
| 90 | + |
| 91 | + translation_queries = |
| 92 | + queries.count { |q| q.include?("discourse_translator_post_translations") } |
| 93 | + expect(translation_queries).to eq(3) |
| 94 | + expect(topic_view.posts.first.association(:translations)).to be_loaded |
| 95 | + end |
| 96 | + |
| 97 | + it "preloads translations when locales are different and in automatic_translation_target_languages" do |
| 98 | + SiteSetting.default_locale = "en" |
| 99 | + I18n.locale = "es" |
| 100 | + SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 1 |
| 101 | + SiteSetting.automatic_translation_target_languages = "es" |
| 102 | + |
| 103 | + topic_view = TopicView.new(topic) |
| 104 | + serializer = TopicViewSerializer.new(topic_view, scope: Guardian.new(user), root: false) |
| 105 | + |
| 106 | + topic_view.posts.reload |
| 107 | + |
| 108 | + queries = |
| 109 | + track_sql_queries do |
| 110 | + json = serializer.as_json |
| 111 | + json[:post_stream][:posts].each { |p| p[:translations] } |
| 112 | + end |
| 113 | + |
| 114 | + translation_queries = |
| 115 | + queries.count { |q| q.include?("discourse_translator_post_translations") } |
| 116 | + expect(translation_queries).to eq(1) |
| 117 | + expect(topic_view.posts.first.association(:translations)).to be_loaded |
| 118 | + end |
69 | 119 | end |
70 | 120 | end |
71 | 121 |
|
|
0 commit comments