|
1
|
"""Tests for utils.translate.""" |
|
2
|
import os |
|
3
|
import tempfile |
|
4
|
import unittest |
|
5
|
from unittest.mock import patch |
|
6
|
|
|
7
|
|
|
8
|
class TestTranslate(unittest.TestCase): |
|
9
|
def test_translates_file_content(self): |
|
10
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False) as f: |
|
11
|
f.write("<p>Hello world</p>") |
|
12
|
path = f.name |
|
13
|
try: |
|
14
|
from hugoifier.utils.translate import translate |
|
15
|
with patch("hugoifier.utils.translate.call_ai", return_value="<p>Hola mundo</p>") as mock_ai: |
|
16
|
result = translate(path, target_language="Spanish") |
|
17
|
self.assertEqual(result, "<p>Hola mundo</p>") |
|
18
|
call_args = mock_ai.call_args[0][0] |
|
19
|
self.assertIn("Spanish", call_args) |
|
20
|
self.assertIn("Hello world", call_args) |
|
21
|
finally: |
|
22
|
os.unlink(path) |
|
23
|
|
|
24
|
def test_uses_target_language_param(self): |
|
25
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False) as f: |
|
26
|
f.write("<p>Bonjour</p>") |
|
27
|
path = f.name |
|
28
|
try: |
|
29
|
from hugoifier.utils.translate import translate |
|
30
|
with patch("hugoifier.utils.translate.call_ai", return_value="<p>Hallo</p>") as mock_ai: |
|
31
|
translate(path, target_language="German") |
|
32
|
call_args = mock_ai.call_args[0][0] |
|
33
|
self.assertIn("German", call_args) |
|
34
|
finally: |
|
35
|
os.unlink(path) |
|
36
|
|
|
37
|
def test_returns_error_on_missing_file(self): |
|
38
|
from hugoifier.utils.translate import translate |
|
39
|
result = translate("/nonexistent/file.html") |
|
40
|
self.assertIn("failed", result.lower()) |
|
41
|
|
|
42
|
|
|
43
|
if __name__ == "__main__": |
|
44
|
unittest.main() |
|
45
|
|