File indexing completed on 2024-04-28 17:02:32

0001 try:
0002     from mock import MagicMock, patch
0003 except ImportError:
0004     from unittest.mock import MagicMock, patch
0005 import pytest
0006 
0007 from scripts.pomtrans import Translator_apertium
0008 
0009 
0010 @pytest.fixture(
0011     scope="module",
0012     params=[
0013         # Apertium 3.8.3
0014         (
0015             ["first example", "second example"],
0016             "first example<br>.second example",  # expected request
0017             "Primer ejemplo<br>.Segundo ejemplo",  # response
0018             ["Primer ejemplo", "Segundo ejemplo"],
0019         ),
0020     ],
0021 )
0022 def translation_workflow(request):
0023     yield request.param
0024 
0025 
0026 @patch("scripts.pomtrans.collect_system")
0027 @patch("scripts.pomtrans.subprocess.call")
0028 def test_apertium_translation_workflow(
0029     subprocess_patch, collection_patch, translation_workflow
0030 ):
0031     (
0032         text,
0033         expected_translator_input,
0034         translator_output,
0035         expected_translation,
0036     ) = translation_workflow
0037 
0038     # patch two calls to 'collect_system':
0039     #  - the first call validates the language pair
0040     #  - the second call performs translations
0041     # each call returns stdout, stderr, and an exit code
0042     collection_patch.side_effect = [
0043         ("", "<stderr content>", 0),
0044         (translator_output, "", 0),
0045     ]
0046 
0047     options = MagicMock(
0048         data_directory=None,
0049         tmode=None,
0050         transerv_bin=None,
0051     )
0052 
0053     translator = Translator_apertium("eng", "spa", options=options)
0054     translation = translator.translate(text)
0055 
0056     setup_call, translation_call = collection_patch.call_args_list
0057     assert translation_call.kwargs["instr"] == expected_translator_input
0058     assert translation == expected_translation