|
4 | 4 | fab!(:post)
|
5 | 5 | subject(:job) { described_class.new }
|
6 | 6 |
|
7 |
| - let(:locales) { %w[en ja] } |
| 7 | + let(:locales) { %w[en ja de] } |
8 | 8 |
|
9 | 9 | before do
|
10 | 10 | SiteSetting.translator_enabled = true
|
|
41 | 41 | job.execute({})
|
42 | 42 | end
|
43 | 43 |
|
44 |
| - it "translates posts to the configured locales" do |
45 |
| - DiscourseTranslator::PostTranslator.expects(:translate).with(post, "en").at_least_once |
46 |
| - DiscourseTranslator::PostTranslator.expects(:translate).with(post, "ja").at_least_once |
47 |
| - |
48 |
| - job.execute({}) |
49 |
| - end |
50 |
| - |
51 | 44 | it "skips posts that already have localizations" do
|
52 | 45 | Post.all.each do |post|
|
53 | 46 | Fabricate(:post_localization, post:, locale: "en")
|
|
67 | 60 | end
|
68 | 61 |
|
69 | 62 | it "handles translation errors gracefully" do
|
| 63 | + post.update(locale: "es") |
70 | 64 | DiscourseTranslator::PostTranslator
|
71 | 65 | .expects(:translate)
|
72 | 66 | .with(post, "en")
|
73 | 67 | .raises(StandardError.new("API error"))
|
74 | 68 | DiscourseTranslator::PostTranslator.expects(:translate).with(post, "ja").once
|
| 69 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "de").once |
75 | 70 |
|
76 | 71 | expect { job.execute({}) }.not_to raise_error
|
77 | 72 | end
|
78 | 73 |
|
79 | 74 | it "logs a summary after translation" do
|
| 75 | + post.update(locale: "es") |
80 | 76 | DiscourseTranslator::PostTranslator.stubs(:translate)
|
81 | 77 | DiscourseTranslator::VerboseLogger.expects(:log).with(includes("Translated 1 posts to en, ja"))
|
82 | 78 |
|
83 | 79 | job.execute({})
|
84 | 80 | end
|
| 81 | + |
| 82 | + context "translation scenarios" do |
| 83 | + it "scenario 1: skips post when locale is not set" do |
| 84 | + DiscourseTranslator::PostTranslator.expects(:translate).never |
| 85 | + |
| 86 | + job.execute({}) |
| 87 | + end |
| 88 | + |
| 89 | + it "scenario 2: returns post with locale 'es' if localizations for en/ja do not exist" do |
| 90 | + post = Fabricate(:post, locale: "es") |
| 91 | + |
| 92 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "en").once |
| 93 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "ja").once |
| 94 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "de").once |
| 95 | + |
| 96 | + job.execute({}) |
| 97 | + end |
| 98 | + |
| 99 | + it "scenario 3: returns post with locale 'en' if ja localization does not exist" do |
| 100 | + post = Fabricate(:post, locale: "en") |
| 101 | + |
| 102 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "ja").once |
| 103 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "de").once |
| 104 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "en").never |
| 105 | + |
| 106 | + job.execute({}) |
| 107 | + end |
| 108 | + |
| 109 | + it "scenario 4: skips post with locale 'en' if 'ja' localization already exists" do |
| 110 | + post = Fabricate(:post, locale: "en") |
| 111 | + Fabricate(:post_localization, post: post, locale: "ja") |
| 112 | + |
| 113 | + DiscourseTranslator::PostTranslator.expects(:translate).never |
| 114 | + |
| 115 | + job.execute({}) |
| 116 | + end |
| 117 | + end |
85 | 118 | end
|
0 commit comments