Skip to content

Commit 069d043

Browse files
committed
Update tests using custom fields
1 parent 6c7e155 commit 069d043

14 files changed

+49
-61
lines changed

db/migrate/20250205082401_move_translations_custom_fields_to_table.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def migrate_custom_fields(model)
3535
SELECT #{model}_id, value
3636
FROM #{model}_custom_fields
3737
WHERE name = 'post_detected_lang'
38+
AND length(value) <= 20
3839
AND id >= :start_id
3940
AND id < :end_id
4041
ORDER BY id

spec/fabricators/post_locale.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
Fabricator(:post_locale, from: DiscourseTranslator::PostLocale) do
3+
post
4+
detected_locale { %w[en de es en-GB ja pt pt-BR].sample }
5+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
Fabricator(:post_translation, from: DiscourseTranslator::PostTranslation) do
3+
post
4+
locale { %w[en de es en-GB ja pt pt-BR].sample }
5+
translation do |attrs|
6+
{
7+
"en" => "Hello",
8+
"de" => "Hallo",
9+
"es" => "Hola",
10+
"en-GB" => "Hello",
11+
"ja" => "こんにちは",
12+
"pt" => "Olá",
13+
"pt-BR" => "Olá",
14+
}[
15+
attrs[:locale]
16+
]
17+
end
18+
end

spec/jobs/detect_posts_language_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
posts.each do |post|
2525
post.reload
26-
expect(post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]).not_to be_nil
26+
expect(post.detected_locale).not_to be_nil
2727
end
2828

2929
expect(Discourse.redis.smembers(redis_key)).to be_empty

spec/lib/guardian_extension_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,13 @@
156156
end
157157

158158
it "cannot translate when post has DETECTED_LANG_CUSTOM_FIELD matches locale" do
159-
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "pt"
160-
post.save
159+
post.set_detected_locale("pt")
161160

162161
expect(guardian.can_translate?(post)).to eq(false)
163162
end
164163

165164
it "can translate when post has DETECTED_LANG_CUSTOM_FIELD does not match locale" do
166-
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "jp"
167-
post.save
165+
post.set_detected_locale("jp")
168166

169167
expect(guardian.can_translate?(post)).to eq(true)
170168
end

spec/models/post_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
it "should reset translation data when post title has been updated" do
1212
Fabricate(:post_translation, post:)
1313
Fabricate(:post_locale, post:)
14-
post.update!(title: "this is an updated title")
14+
post.update!(raw: "this is an updated title")
1515

1616
expect(DiscourseTranslator::PostLocale.where(post:)).to be_empty
1717
expect(DiscourseTranslator::PostLocale.find_by(post:)).to be_nil

spec/models/translation_tables_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ def create_translation_custom_fields(count)
9090
post.save_custom_fields(true)
9191

9292
migration = described_class.new
93-
migration.up
93+
expect { migration.up }.not_to raise_error
94+
9495
expect(PostLocale.count).to eq(0)
9596
expect(PostTranslation.count).to eq(0)
9697
end

spec/serializers/post_serializer_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,13 @@
6363
end
6464

6565
it "cannot translate when post has DETECTED_LANG_CUSTOM_FIELD matches locale" do
66-
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "pt"
67-
post.save
66+
post.set_detected_locale("pt")
6867

6968
expect(serializer.can_translate).to eq(false)
7069
end
7170

7271
it "can translate when post has DETECTED_LANG_CUSTOM_FIELD does not match locale" do
73-
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "jp"
74-
post.save
72+
post.set_detected_locale("jp")
7573

7674
expect(serializer.can_translate).to eq(true)
7775
end

spec/services/amazon_spec.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@
3737
it "should store the detected language in a custom field" do
3838
expect(described_class.detect(post)).to eq(detected_lang)
3939

40-
2.times do
41-
expect(post.custom_fields[::DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]).to eq(
42-
detected_lang,
43-
)
44-
end
40+
expect(post.detected_locale).to eq(detected_lang)
4541
end
4642

4743
it "should fail graciously when the cooked translated text is blank" do
@@ -65,8 +61,7 @@
6561
},
6662
)
6763
described_class.stubs(:client).returns(client)
68-
post.custom_fields[::DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "en"
69-
post.save_custom_fields
64+
post.set_detected_locale("en")
7065
I18n.stubs(:locale).returns(:es)
7166
end
7267

spec/services/base_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ class EmptyTranslator < DiscourseTranslator::Base
116116
end
117117

118118
it "performs detection if no cached result" do
119-
TestTranslator.save_detected_locale(post) { nil }
120119
TestTranslator.expects(:detect!).with(post).returns("es")
121120

122121
expect(TestTranslator.detect(post)).to eq("es")
@@ -147,15 +146,13 @@ class EmptyTranslator < DiscourseTranslator::Base
147146

148147
it "raises error when translation not supported" do
149148
TestTranslator.save_detected_locale(post) { "xx" }
150-
TestTranslator.save_translation(post) { nil }
151149
TestTranslator.expects(:translate_supported?).with("xx", :en).returns(false)
152150

153151
expect { TestTranslator.translate(post) }.to raise_error(DiscourseTranslator::TranslatorError)
154152
end
155153

156154
it "performs translation when needed" do
157155
TestTranslator.save_detected_locale(post) { "es" }
158-
TestTranslator.save_translation(post) { nil }
159156
TestTranslator.expects(:translate!).returns("hello")
160157

161158
expect(TestTranslator.translate(post)).to eq(%w[es hello])

0 commit comments

Comments
 (0)