def _create_get_all_translations(): return [ _create_get_translation(Uid('1')), _create_get_translation(Uid('2')), _create_get_translation(Uid('3')), _create_get_translation(Uid('4')), ]
def mock_storage_adapter() -> SupportsPerformingTranslations: yield mock.Mock( store_uid=MagicMock(side_effect=lambda _: None), retrieve_all_uids=MagicMock(side_effect=lambda: [ Uid('1'), Uid('2'), Uid('3'), Uid('4'), ]), )
def get_translation(self, uid: Uid) -> Optional[Translation]: return { Uid('abc321'): Translation(uid=Uid('abc321'), status=StatusOption.completed, text='text', translated_text='short text'), Uid('xyz543'): Translation(uid=Uid('xyz543'), status=StatusOption.completed, text='text', translated_text='a very long piece of text'), }.get(uid, None)
def get_all_translations(self) -> List[Translation]: return [ Translation( uid=Uid('abc321'), status=StatusOption.completed, text=None, translated_text=None, ), Translation( uid=Uid('xyz543'), status=StatusOption.completed, text=None, translated_text=None, ) ]
def test_it_returns_the_response_uid_on_success( create_adapter: SupportsPerformingTranslations, mock_requests: mock.Mock, ): # arrange text = 'example text' json_response = { "balance": 99943.0, "client": "username", "price": 6.0, "source_language": "en", "status": "new", "target_language": "pt", "text": "Hello, world!", "text_format": "text", "uid": "ac1a53a264" } mock_response = mock.Mock( status_code=201, json=lambda: json_response ) mock_requests.post.return_value = mock_response # act result = create_adapter.translate(text=text) # assert assert_that(result, equal_to(Uid('ac1a53a264')))
def _create_get_translation(uid: Optional[str] = None, ) -> Translation: return Translation( uid=Uid(uid), status=StatusOption.unknown, text='text', translated_text=translation_uid_map[uid], )
def test_it_gets_full_translation_for_each_translation( mock_translation_adapter: mock.Mock, mock_storage_adapter: mock.Mock, ): # arrange test_controller = controller.Controller( translation_adapter=mock_translation_adapter, storage_adapter=mock_storage_adapter, ) # act test_controller.get_translations() # assert mock_translation_adapter.get_translation.assert_has_calls([ call(Uid('1')), call(Uid('2')), call(Uid('3')), call(Uid('4')), ], any_order=True)
def test_it_returns_none_if_translation_is_not_found( mock_requests: mock.Mock, create_adapter: SupportsPerformingTranslations, ): # arrange uid = Uid('ac1a53a264') mock_requests.get.return_value = mock.Mock(status_code=404) # act result = create_adapter.get_translation(uid=uid) # assert assert_that(result, is_(None))
def test_it_sorts_translations_by_translation_length_longest_to_shortest( mock_translation_adapter: mock.Mock, mock_storage_adapter: mock.Mock, ): # arrange test_controller = controller.Controller( translation_adapter=mock_translation_adapter, storage_adapter=mock_storage_adapter, ) # act result = test_controller.get_translations() # assert translated_texts = [(t.uid, t.translated_text) for t in result] assert_that( translated_texts, equal_to([ (Uid('2'), 'this is the longest string at a huge 39'), (Uid('4'), 'this string has length 25'), (Uid('1'), 'shorter just 15'), (Uid('3'), None), ]))
def test_it_throws_if_request_goes_wrong( mock_requests: mock.Mock, create_adapter: SupportsPerformingTranslations, ): # arrange uid = Uid('ac1a53a264') mock_requests.get.return_value = mock.Mock( status_code=500, text='error message', ) # act with pytest.raises(TranslationFailedException) as e: create_adapter.get_translation(uid=uid) # assert assert_that(str(e), contains_string('error message'))
def test_it_returns_translation_data( mock_requests: mock.Mock, create_adapter: SupportsPerformingTranslations, mock_successful_get_response: mock.Mock, ): # arrange uid = Uid('ac1a53a264') mock_requests.get.return_value = mock_successful_get_response # act result = create_adapter.get_translation(uid=uid) # assert assert_that(result, equal_to(Translation( uid=uid, status=StatusOption.translating, text='Hello, world!', translated_text=None, )))
def test_it_gets_from_url_with_headers( mock_requests: mock.Mock, create_adapter: SupportsPerformingTranslations, mock_successful_get_response: mock.Mock, ): # arrange uid = Uid('ac1a53a264') mock_requests.get.return_value = mock_successful_get_response # act create_adapter.get_translation(uid=uid) # assert mock_requests.get.assert_called_once_with( url='example.com/translation/ac1a53a264/', headers={ 'Authorization': 'ApiKey charles:123xyz', 'Content-Type': 'application/json', }, )
def translate( text: str, user_name: str, api_key: str, base_url: str, ) -> Uid: response = requests.post( url=f'{base_url}/translation', headers=_create_headers(user_name=user_name, api_key=api_key), data=json.dumps({ 'source_language': 'en', 'target_language': 'es', 'text_format': 'text', 'text': text, }, sort_keys=True) ) if response.status_code != 201: raise TranslationFailedException(response.text) json_response = response.json() return Uid(json_response['uid'])
def translate(self, text: str) -> Uid: return Uid('xyz543')
def retrieve_all_uids(db: SQLAlchemy) -> List[Uid]: uids = db.session.query(Translation).all() return [Uid(uid.uid) for uid in uids]
from typing import Optional, Mapping from unittest import mock from unittest.mock import call, MagicMock import pytest from hamcrest import equal_to, assert_that from unbabel.types import Translation, StatusOption, Uid, SupportsPerformingTranslations import unbabel.controller as controller translation_uid_map: Mapping[str, Optional[str]] = { Uid('1'): 'shorter just 15', Uid('2'): 'this is the longest string at a huge 39', Uid('3'): None, Uid('4'): 'this string has length 25' } def _create_get_translation(uid: Optional[str] = None, ) -> Translation: return Translation( uid=Uid(uid), status=StatusOption.unknown, text='text', translated_text=translation_uid_map[uid], ) def _create_uid(): return 'xyz543'