Skip to content

Commit

Permalink
Fix unit tests for translating text and HTML using LibreTranslate to …
Browse files Browse the repository at this point in the history
…mock the POST request
  • Loading branch information
drivard committed Dec 19, 2023
1 parent a287f33 commit b839844
Showing 1 changed file with 91 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from unittest import mock

from django.test import TestCase, override_settings
Expand Down Expand Up @@ -40,59 +42,107 @@ def test_language_code(self):
)
self.assertEqual(self.translator.language_code("foo-bar-baz"), "foo")

@mock.patch(
"wagtail_localize.machine_translators.libretranslate.LibreTranslator.translate",
return_value={
StringValue("Hello world!"): StringValue("Bonjour le monde!"),
StringValue("This is a sentence. This is another sentence."): StringValue(
"Ceci est une phrase. Ceci est une autre phrase."
),
},
)
def test_translate_text(self, mock_translate):
self.assertIsInstance(self.translator, LibreTranslator)
@mock.patch("requests.post")
def test_translate_text(self, mock_post):
# Mock the response of requests.post
mock_response = mock.Mock()
mock_response.json.return_value = {
"translatedText": [
"Bonjour le monde!",
"Ceci est une phrase. Ceci est une autre phrase.",
]
}
mock_response.raise_for_status = mock.Mock()
mock_post.return_value = mock_response

input_strings = [
StringValue("Hello world!"),
StringValue("This is a sentence. This is another sentence."),
]

translations = self.translator.translate(
self.english_locale,
self.french_locale,
{
StringValue("Hello world!"),
StringValue("This is a sentence. This is another sentence."),
},
self.english_locale, self.french_locale, input_strings
)

self.assertEqual(
translations,
{
StringValue("Hello world!"): StringValue("Bonjour le monde!"),
StringValue(
"This is a sentence. This is another sentence."
): StringValue("Ceci est une phrase. Ceci est une autre phrase."),
},
)

@mock.patch(
"wagtail_localize.machine_translators.libretranslate.LibreTranslator.translate",
return_value={
StringValue('<a id="a1">Hello !</a>. <b>This is a test</b>.'): StringValue(
"""<a id="a1">Bonjour !</a>. <b>C'est un test</b>."""
expected_translations = {
StringValue("Hello world!"): StringValue("Bonjour le monde!"),
StringValue("This is a sentence. This is another sentence."): StringValue(
"Ceci est une phrase. Ceci est une autre phrase."
),
},
)
def test_translate_html(self, mock_translate):
self.assertIsInstance(self.translator, LibreTranslator)
}

# Assertions to check if the translation is as expected
self.assertEqual(translations, expected_translations)

# Assert that requests.post was called with the correct arguments
mock_post.assert_called_once_with(
LIBRETRANSLATE_SETTINGS_ENDPOINT["OPTIONS"]["LIBRETRANSLATE_URL"]
+ "/translate",
data=json.dumps(
{
"q": [
"Hello world!",
"This is a sentence. This is another sentence.",
],
"source": "en",
"target": "fr",
"api_key": LIBRETRANSLATE_SETTINGS_ENDPOINT["OPTIONS"]["API_KEY"],
}
),
headers={"Content-Type": "application/json"},
timeout=10,
)

string, attrs = StringValue.from_source_html(
@mock.patch("requests.post")
def test_translate_html(self, mock_post):
# Mock the response of requests.post
mock_response = mock.Mock()
mock_response.json.return_value = {
"translatedText": ["""<a id="a1">Bonjour !</a>. <b>C'est un test</b>."""]
}
mock_response.raise_for_status = mock.Mock()
mock_post.return_value = mock_response

input_string, attrs = StringValue.from_source_html(
'<a href="https://en.wikipedia.org/wiki/World">Hello !</a>. <b>This is a test</b>.'
)

translations = self.translator.translate(
self.english_locale, self.french_locale, [string]
self.english_locale, self.french_locale, [input_string]
)

self.assertEqual(
translations[string].render_html(attrs),
"""<a href="https://en.wikipedia.org/wiki/World">Bonjour !</a>. <b>C'est un test</b>.""",
expected_translation = {
input_string: StringValue(
"""<a id="a1">Bonjour !</a>. <b>C'est un test</b>."""
)
}

# Assertions to check if the translation is as expected
self.assertEqual(translations, expected_translation)

# Additional assertion to check the rendered HTML
translated_string = translations[input_string]
rendered_html = translated_string.render_html(attrs)
expected_rendered_html = '<a href="https://en.wikipedia.org/wiki/World">Bonjour !</a>. <b>C\'est un test</b>.'

self.assertEqual(rendered_html, expected_rendered_html)

# Assert that requests.post was called with the correct arguments
mock_post.assert_called_once_with(
LIBRETRANSLATE_SETTINGS_ENDPOINT["OPTIONS"]["LIBRETRANSLATE_URL"]
+ "/translate",
data=json.dumps(
{
"q": [
'<a id="a1">Hello !</a>. <b>This is a test</b>.'
], # Use the string from StringValue
"source": "en",
"target": "fr",
"api_key": LIBRETRANSLATE_SETTINGS_ENDPOINT["OPTIONS"]["API_KEY"],
}
),
headers={"Content-Type": "application/json"},
timeout=10,
)

def test_can_translate(self):
Expand Down

0 comments on commit b839844

Please sign in to comment.