Skip to content

Commit 3664d20

Browse files
committed
Add test for translate job
1 parent 6a5581a commit 3664d20

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

app/jobs/regular/translate_translatable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def execute(args)
1717
)
1818
end
1919

20-
topic_id = translatable.is_a?(Post) ? translatable.topic.id : translatable.id
21-
post_id = translatable.is_a?(Post) ? translatable.id : 1
20+
topic_id, post_id =
21+
translatable.is_a?(Post) ? [translatable.topic_id, translatable.id] : [translatable.id, 1]
2222
MessageBus.publish("/topic/#{topic_id}", type: :revised, id: post_id)
2323
end
2424
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Jobs::TranslateTranslatable do
4+
fab!(:post) { Fabricate(:post) }
5+
fab!(:topic) { Fabricate(:topic) }
6+
7+
before do
8+
SiteSetting.translator_enabled = true
9+
SiteSetting.translator = "Google"
10+
SiteSetting.automatic_translation_target_languages = "es|fr"
11+
end
12+
13+
describe "#execute" do
14+
it "does nothing when translator is disabled" do
15+
SiteSetting.translator_enabled = false
16+
expect(DiscourseTranslator::Google).not_to receive(:translate)
17+
18+
subject.execute(type: "Post", translatable_id: post.id)
19+
end
20+
21+
it "does nothing when target languages are empty" do
22+
SiteSetting.automatic_translation_target_languages = ""
23+
expect(DiscourseTranslator::Google).not_to receive(:translate)
24+
25+
subject.execute(type: "Post", translatable_id: post.id)
26+
end
27+
28+
it "translates posts to configured target languages" do
29+
expect(DiscourseTranslator::Google).to receive(:translate).with(post, :es)
30+
expect(DiscourseTranslator::Google).to receive(:translate).with(post, :fr)
31+
expect(MessageBus).to receive(:publish).with(
32+
"/topic/#{post.topic_id}",
33+
type: :revised,
34+
id: post.id,
35+
)
36+
37+
subject.execute(type: "Post", translatable_id: post.id)
38+
end
39+
40+
it "translates topics to configured target languages" do
41+
expect(DiscourseTranslator::Google).to receive(:translate).with(topic, :es)
42+
expect(DiscourseTranslator::Google).to receive(:translate).with(topic, :fr)
43+
expect(MessageBus).to receive(:publish).with("/topic/#{topic.id}", type: :revised, id: 1)
44+
45+
subject.execute(type: "Topic", translatable_id: topic.id)
46+
end
47+
48+
it "does nothing when translatable is not found" do
49+
expect(DiscourseTranslator::Google).not_to receive(:translate)
50+
expect(MessageBus).not_to receive(:publish)
51+
52+
subject.execute(type: "Post", translatable_id: -1)
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)