def test_save_translation(self, session, session_cls): translator = Translator(Translation, session, 'language') params = { 'context': 'context', 'message_id': 'message_id', } translatable = TranslatableString( pending_value='translation', **params) translator.save_translation(translatable) read_translatable = TranslatableString(**params) translation = translator.translate(read_translatable) assert translation == 'translation'
def test_dont_save_debug_translation(self, session): translator = Translator(Translation, session, SAMPLE_LANGUAGE, strategy=Translator.strategies.DEBUG_VALUE) translatable = TranslatableString(context=SAMPLE_CONTEXT, message_id=SAMPLE_MESSAGE_ID) debug_value = translator._get_debug_translation(translatable) translatable.pending_value = debug_value translator.save_translation(translatable) assert session.query(Translation).count() == 0
def test_none_value(self, session): translator = Translator(Translation, session, SAMPLE_LANGUAGE) translatable = TranslatableString(context=SAMPLE_CONTEXT, message_id=SAMPLE_MESSAGE_ID) translation = translator.translate(translatable) translation is None
def test_translate_structure(self, session): translation = Translation( context='context', message_id='message_id', language='language', value='translation') session.add(translation) session.commit() translator = Translator(Translation, session, 'language') translatable = TranslatableString( context='context', message_id='message_id') structure = { 'int': 1, 'str': 'str', 'list': [1, 'str', translatable], 'translatable': translatable, } translation = translator.translate(structure) assert translation == { 'int': 1, 'str': 'str', 'list': [1, 'str', 'translation'], 'translatable': 'translation', }
def test_dont_save_debug_translation(self, session): translator = Translator( Translation, session, SAMPLE_LANGUAGE, strategy=Translator.strategies.DEBUG_VALUE ) translatable = TranslatableString( context=SAMPLE_CONTEXT, message_id=SAMPLE_MESSAGE_ID) debug_value = translator._get_debug_translation(translatable) translatable.pending_value = debug_value translator.save_translation(translatable) assert session.query(Translation).count() == 0
def make_from_obj(manager, obj, attribute_name, pending_value=None): context = get_context(manager, obj, attribute_name) message_id = get_message_id(manager, obj) return TranslatableString( context=context, message_id=message_id, pending_value=pending_value )
def test_debug_value(self, session): translator = Translator(Translation, session, SAMPLE_LANGUAGE, strategy=Translator.strategies.DEBUG_VALUE) translatable = TranslatableString(context=SAMPLE_CONTEXT, message_id=SAMPLE_MESSAGE_ID) translation = translator.translate(translatable) assert "[Translation missing" in translation
def test_sentinel_value(self, session): translator = Translator(Translation, session, SAMPLE_LANGUAGE, strategy=Translator.strategies.SENTINEL_VALUE) translatable = TranslatableString(context=SAMPLE_CONTEXT, message_id=SAMPLE_MESSAGE_ID) translation = translator.translate(translatable) assert translation is TRANSLATION_MISSING
def test_update_translation(self, session, session_cls): translator = Translator(Translation, session, 'language') params = { 'context': 'context', 'message_id': 'message_id', } translatable = TranslatableString( pending_value='translation', **params) translator.save_translation(translatable) new_translator = Translator( Translation, session_cls(), 'language') new_translatable = TranslatableString( pending_value='new translation', **params) new_translator.save_translation(new_translatable) read_translatable = TranslatableString(**params) translation = translator.translate(read_translatable) assert translation == 'new translation' assert session.query(Translation).count() == 1
def test_translate(self, session): translation = Translation( context='context', message_id='message_id', language='language', value='translation') session.add(translation) session.commit() translator = Translator(Translation, session, 'language') translatable = TranslatableString( context='context', message_id='message_id') translation = translator.translate(translatable) assert translation == 'translation'
def test_get_translation(session, manager): manager.save(CustomFieldsEntity) item = CustomFieldsEntity() manager.save(item) create_translation_for_entity(session, manager, 'english', item, 'name', 'English name') context = get_context(manager, item, 'name') message_id = get_message_id(manager, item) translatable = TranslatableString(context=context, message_id=message_id) translator = Translator(Translation, session, 'english') translated_data = translator.translate(translatable) assert translated_data == 'English name'
def test_case_sensitivity(self, session): translation_lower = Translation( context='context', message_id='message_id', language='language', value='translation') translation_upper = Translation( context='CONTEXT', message_id='message_id', language='language', value='translation') session.add(translation_upper) session.add(translation_lower) session.commit() translator = Translator(Translation, session, 'language') translatable = TranslatableString( context='context', message_id='message_id') # this shuold just not raise translator.translate(translatable)
def test_translating_class_labels(session, translating_type_heirarchy, bound_manager): manager = bound_manager translator = Translator(Translation, session, 'en') translatable = TranslatableString( context=TYPE_CONTEXT, message_id='Entity', pending_value='English Entity') translator.save_translation(translatable) translator.bind(manager) hierarchy = manager.get_labeled_type_hierarchy() entity = next(hierarchy) translated = translator.translate(entity[1]) assert translated == 'English Entity'
def test_en_fallback(self, session): translation = Translation( context=SAMPLE_CONTEXT, message_id=SAMPLE_MESSAGE_ID, language='en', value='en fallback', ) session.add(translation) session.commit() en_fallback_strategy = FallbackLangStrategy('en') translator = Translator( Translation, session, SAMPLE_LANGUAGE, strategy=en_fallback_strategy, ) translatable = TranslatableString(context=SAMPLE_CONTEXT, message_id=SAMPLE_MESSAGE_ID) translation = translator.translate(translatable) assert translation == 'en fallback'
def test_repr(self, session): translatable = TranslatableString( context='my context', message_id='my message id') assert "my context" in repr(translatable) assert "my message id" in repr(translatable)
def change_instance_type(self, obj, type_id, updated_values=None): if updated_values is None: updated_values = {} updated_values = updated_values.copy() old_descriptor = self.type_registry.get_descriptor(type(obj)) new_descriptor = self.type_registry.get_descriptor_by_id(type_id) old_message_id = get_message_id(self, obj) old_translatables = {} # collect any translatable fields on the original object # also, replace any values with placeholders for the super() call for attr_name in iter_translatables(old_descriptor): attr = getattr(obj, attr_name) if is_translatable_value(attr): setattr(obj, attr_name, PLACEHOLDER) context = get_context(self, obj, attr_name) translatable = TaalTranslatableString(context, old_message_id, attr) old_translatables[attr_name] = translatable new_translatables = {} # collect any translatable fields from the new type # also, replace any values in updated_values with placeholders # for the super() call # note that we can't collect the context/message_id until after # we call super(), since they may be about to change # (context will definitely change, and message_id might, if we add or # remove unique attributes) for attr_name in iter_translatables(new_descriptor): attr = updated_values.get(attr_name, MISSING) if attr is None: continue if attr is MISSING: attr = None if is_translatable_value(attr): updated_values[attr_name] = PLACEHOLDER translatable = TaalTranslatableString(None, None, attr) new_translatables[attr_name] = translatable new_obj = super(Manager, self).change_instance_type(obj, type_id, updated_values) # we are now able to fill in context/message_id for the new object new_message_id = get_message_id(self, new_obj) for attr_name, translatable in new_translatables.items(): translatable.message_id = new_message_id translatable.context = get_context(self, new_obj, attr_name) to_delete = set(old_translatables) - set(new_translatables) to_rename = set(old_translatables) & set(new_translatables) to_add = set(new_translatables) - set(old_translatables) translator = get_translator(self) for key in to_delete: translatable = old_translatables[key] translator.delete_translations(translatable) for key in to_rename: old_translatable = old_translatables[key] new_translatable = new_translatables[key] translator.move_translations(old_translatable, new_translatable) if new_translatable.pending_value is not None: # updated_values contained a key for a field already existing # on the old type. save the updated translation translator.save_translation(new_translatable) for key in to_add: translatable = new_translatables[key] if translatable.pending_value is not None: translator.save_translation(translatable) return new_obj
def test_empty(self): s1 = TranslatableString() s2 = TranslatableString() assert s1 == s2
def test_not_equal(self): s1 = TranslatableString(context='c', message_id='m') s2 = TranslatableString(context='c', message_id='mmm') assert s1 != s2
def change_instance_type(self, obj, type_id, updated_values=None): if updated_values is None: updated_values = {} updated_values = updated_values.copy() old_descriptor = self.type_registry.get_descriptor(type(obj)) new_descriptor = self.type_registry.get_descriptor_by_id(type_id) old_message_id = get_message_id(self, obj) old_translatables = {} # collect any translatable fields on the original object # also, replace any values with placeholders for the super() call for attr_name in iter_translatables(old_descriptor): attr = getattr(obj, attr_name) if is_translatable_value(attr): setattr(obj, attr_name, PLACEHOLDER) context = get_context(self, obj, attr_name) translatable = TaalTranslatableString( context, old_message_id, attr) old_translatables[attr_name] = translatable new_translatables = {} # collect any translatable fields from the new type # also, replace any values in updated_values with placeholders # for the super() call # note that we can't collect the context/message_id until after # we call super(), since they may be about to change # (context will definitely change, and message_id might, if we add or # remove unique attributes) for attr_name in iter_translatables(new_descriptor): attr = updated_values.get(attr_name, MISSING) if attr is None: continue if attr is MISSING: attr = None if is_translatable_value(attr): updated_values[attr_name] = PLACEHOLDER translatable = TaalTranslatableString( None, None, attr) new_translatables[attr_name] = translatable new_obj = super(Manager, self).change_instance_type( obj, type_id, updated_values) # we are now able to fill in context/message_id for the new object new_message_id = get_message_id(self, new_obj) for attr_name, translatable in new_translatables.items(): translatable.message_id = new_message_id translatable.context = get_context(self, new_obj, attr_name) to_delete = set(old_translatables) - set(new_translatables) to_rename = set(old_translatables) & set(new_translatables) to_add = set(new_translatables) - set(old_translatables) translator = get_translator(self) for key in to_delete: translatable = old_translatables[key] translator.delete_translations(translatable) for key in to_rename: old_translatable = old_translatables[key] new_translatable = new_translatables[key] translator.move_translations(old_translatable, new_translatable) if new_translatable.pending_value is not None: # updated_values contained a key for a field already existing # on the old type. save the updated translation translator.save_translation(new_translatable) for key in to_add: translatable = new_translatables[key] if translatable.pending_value is not None: translator.save_translation(translatable) return new_obj
def test_save_empty(session): translatable = TranslatableString() translator = Translator(Translation, session, 'language') with pytest.raises(RuntimeError): translator.save_translation(translatable)