-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdeepl_translator_spec.rb
More file actions
144 lines (118 loc) · 4.67 KB
/
Copy pathdeepl_translator_spec.rb
File metadata and controls
144 lines (118 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true
require "spec_helper"
require "deepl"
module Decidim
describe DeeplTranslator do
let(:title) { { en: "New Title" } }
let(:process) { build(:participatory_process, title:) }
let(:target_locale) { "fr" }
let(:source_locale) { "en" }
let(:translation) { double("translation", text: "Nouveau Titre") }
let(:translate_request) { instance_double(DeepL::Requests::Translate, request: translation) }
let(:api) { instance_double(DeepL::API) }
before do
allow(Decidim).to receive(:machine_translation_service_klass).and_return(DeeplTranslator)
allow(DeepL::API).to receive(:new).and_return(api)
allow(DeepL::Requests::Translate).to receive(:new).and_return(translate_request)
end
describe "When fields job is executed" do
before { clear_enqueued_jobs }
it "calls DeeplTranslator to create machine translations" do
expect(DeeplTranslator).to receive(:new).with(
process,
"title",
process["title"][source_locale],
target_locale,
source_locale
).and_call_original
process.save
MachineTranslationFieldsJob.perform_now(
process,
"title",
process["title"][source_locale],
target_locale,
source_locale
)
end
end
describe "#translate" do
subject { DeeplTranslator.new(process, "title", text, target_locale, source_locale).translate }
let(:text) { title[source_locale.to_sym] }
context "when translation is nil" do
let(:translate_request) { instance_double(DeepL::Requests::Translate, request: nil) }
it "does not enqueue a job" do
expect(Decidim::MachineTranslationSaveJob).not_to receive(:perform_later)
expect(subject).to be_nil
end
end
context "when text is empty" do
let(:text) { "" }
it "does not enqueue a job" do
expect(Decidim::MachineTranslationSaveJob).not_to receive(:perform_later)
expect(subject).to be_nil
end
end
context "when DeepL raises a non-quota error" do
before do
allow(DeepL::Requests::Translate).to receive(:new).and_raise(StandardError, "API failure")
end
it "logs the error and re-raises" do
expect(Rails.logger).to receive(:error).with(/\[DeeplTranslator\] StandardError - API failure/)
expect { subject }.to raise_error(StandardError, "API failure")
end
end
context "when DeepL raises QuotaExceeded" do
before do
allow(DeepL::Requests::Translate).to receive(:new).and_raise(DeepL::Exceptions::QuotaExceeded.new("Quota exceeded"))
end
it "logs the error and returns nil" do
expect(Rails.logger).to receive(:error).with(/\[DeeplTranslator\] Quota exceeded/)
expect(subject).to be_nil
end
end
context "when translation succeeds" do
it "enqueues MachineTranslationSaveJob with correct arguments" do
expect(Decidim::MachineTranslationSaveJob).to receive(:perform_later).with(
process,
"title",
target_locale,
"Nouveau Titre"
)
subject
end
end
context "when text is blank but not empty" do
let(:text) { " " }
it "does not enqueue a job" do
expect(Decidim::MachineTranslationSaveJob).not_to receive(:perform_later)
expect(subject).to be_nil
end
end
context "when translation text is blank" do
let(:translate_request) { instance_double(DeepL::Requests::Translate, request: double("translation", text: "")) }
it "does not enqueue a job" do
expect(Decidim::MachineTranslationSaveJob).not_to receive(:perform_later)
expect(subject).to be_nil
end
end
context "when DeepL raises EOFError" do
before do
allow(DeepL::Requests::Translate).to receive(:new).and_raise(EOFError, "end of file reached")
end
it "logs the error and re-raises" do
expect(Rails.logger).to receive(:error).with(/\[DeeplTranslator\] EOFError - end of file reached/)
expect { subject }.to raise_error(EOFError)
end
end
context "when DeepL raises IOError" do
before do
allow(DeepL::Requests::Translate).to receive(:new).and_raise(IOError, "stream closed in another thread")
end
it "logs the error and re-raises" do
expect(Rails.logger).to receive(:error).with(/\[DeeplTranslator\] IOError - stream closed in another thread/)
expect { subject }.to raise_error(IOError)
end
end
end
end
end