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