def test_use_most_frequent(session_cls): session_en = session_cls() translator_en = Translator(Translation, session_cls(), 'en') translator_en.bind(session_en) session_fr = session_cls() translator_fr = Translator(Translation, session_cls(), 'fr') translator_fr.bind(session_fr) def add_with_translation(en, fr): model = Model(name=en) session_en.add(model) session_en.commit() # translate into fr translatable = model.name translatable.pending_value = fr translator_fr.save_translation(translatable) model = Model(name='a') session_en.add(model) session_en.commit() add_with_translation('a', '1') translator_fr.suggest_translation(model.name, 'en', 'fr') == '1' add_with_translation('a', '2') add_with_translation('a', '2') translator_fr.suggest_translation(model.name, 'en', 'fr') == '2'
def test_delete_on_delete(self, session, session_cls): session1 = session_cls() translator1 = Translator(Translation, session_cls(), 'language1') translator1.bind(session1) session2 = session_cls() translator2 = Translator(Translation, session_cls(), 'language2') translator2.bind(session2) instance = Model(id=1) session.add(instance) session.commit() instance = session1.query(Model).get(1) instance.name = 'name' session1.commit() instance = session2.query(Model).get(1) instance.name = 'name' session2.commit() assert translator1.session.query(Translation).count() == 2 session2.delete(instance) assert translator1.session.query(Translation).count() == 2 session2.commit() translator1.session.rollback() # expire caches assert translator1.session.query(Translation).count() == 0
def test_basic(session_cls): session_en = session_cls() translator_en = Translator(Translation, session_cls(), 'en') translator_en.bind(session_en) session_fr = session_cls() translator_fr = Translator(Translation, session_cls(), 'fr') translator_fr.bind(session_fr) model = Model(name='name') session_en.add(model) session_en.commit() # translate into fr translatable = model.name translatable.pending_value = 'namë_fr' translator_fr.save_translation(translatable) new_model = Model(name='name') session_en.add(new_model) session_en.commit() suggestion = translator_fr.suggest_translation( new_model.name, from_language='en', to_language='fr') assert suggestion == 'namë_fr'
def test_basic(session_cls): session_en = session_cls() translator_en = Translator(Translation, session_cls(), 'en') translator_en.bind(session_en) session_fr = session_cls() translator_fr = Translator(Translation, session_cls(), 'fr') translator_fr.bind(session_fr) model = Model(name='name') session_en.add(model) session_en.commit() # translate into fr translatable = model.name translatable.pending_value = 'namë_fr' translator_fr.save_translation(translatable) new_model = Model(name='name') session_en.add(new_model) session_en.commit() suggestion = translator_fr.suggest_translation(new_model.name, from_language='en', to_language='fr') assert suggestion == 'namë_fr'
def bound_session(request, session, session_cls): # importing at the module level messes up coverage from taal import Translator from tests.models import Translation translator = Translator(Translation, session_cls(), 'language') translator.bind(session) return session
def test_labeled_hierarchy(session, translating_type_heirarchy, bound_manager): manager = bound_manager translator = Translator(Translation, session, 'en') translator.bind(manager) hierarchy = manager.get_labeled_type_hierarchy() entity = next(hierarchy) assert isinstance(entity[1], TranslatableString)
def bound_manager(request, session_cls, translating_manager): # importing at the module level messes up coverage from taal import Translator from tests.models import Translation manager = translating_manager translator = Translator(Translation, session_cls(), 'language') translator.bind(manager) return manager
def test_unknown_language(session_cls): session = session_cls() translator = Translator(Translation, session_cls(), 'en') translator.bind(session) model = Model(name='name') session.add(model) session.commit() assert translator.suggest_translation(model.name, 'en', 'foo') is None assert translator.suggest_translation(model.name, 'foo', 'en') is None
def test_set_from_other_model(session, session_cls): model1 = Model(name='name') model2 = RequiredModel(name=model1.name) translator = Translator(Translation, session_cls(), 'language') translator.bind(session) session.add(model1) session.add(model2) session.commit() assert translator.session.query( Translation.context).distinct().count() == 2
def test_query_values(session_cls): session1 = session_cls() session2 = session_cls() translator = Translator(Translation, session_cls(), 'language') translator.bind(session1) translator.bind(session2) model = Model(name='name') session1.add(model) session1.commit() (value,) = session2.query(Model.name).one() assert value == PlaceholderValue
def test_query_values(session_cls): session1 = session_cls() session2 = session_cls() translator = Translator(Translation, session_cls(), 'language') translator.bind(session1) translator.bind(session2) model = Model(name='name') session1.add(model) session1.commit() (value, ) = session2.query(Model.name).one() assert value == PlaceholderValue
def test_merge_from_other_session(session_cls): """ regression test """ session1 = session_cls() session2 = session_cls() translator = Translator(Translation, session_cls(), 'language') translator.bind(session1) translator.bind(session2) instance = Model(name='name') session1.add(instance) session1.commit() session2.merge(instance) session2.commit()
def test_removing_translations(session, session_cls, first, second): translator = Translator(Translation, session_cls(), 'language') translator.bind(session) instance = Model(name=first) session.add(instance) session.commit() expected_count = 1 if is_translatable_value(first) else 0 assert translator.session.query(Translation).count() == expected_count instance.name = second session.commit() expected_count = 1 if is_translatable_value(second) else 0 assert translator.session.query(Translation).count() == expected_count
def test_load(session, session_cls, value): instance = Model(name=value) translator = Translator(Translation, session_cls(), 'language') translator.bind(session) session.add(instance) session.commit() pk = instance.id # make a new session session = session_cls() translator.bind(session) instance = session.query(Model).get(pk) if value is None: assert instance.name is None else: assert isinstance(instance.name, TranslatableString)
def test_corrupt_db(session, session_cls): class CorruptModel(Base): __tablename__ = "models" id = Column(Integer, primary_key=True) name = Column(Text) identifier = Column(Text) corrupt = CorruptModel(id=1, name='name') session.add(corrupt) session.commit() translator = Translator(Translation, session_cls(), 'language') translator.bind(session) with pytest.raises(RuntimeError): session.query(Model).get(1)
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_savepoints(self, session, session_cls): translator = Translator(Translation, session_cls(), 'language') translator.bind(session) instance1 = Model(name='instance 1') session.add(instance1) session.begin_nested() instance2 = Model(name='instance 2') session.add(instance2) session.flush() assert session in flush_log assert len(flush_log[session]) == 2 session.rollback() session.commit() assert session.query(Model).count() == 1 assert translator.session.query(Translation).count() == 1
def test_deleting(session, session_cls): translator = Translator(Translation, session_cls(), 'language') translator.bind(session) instance = Model(name='name') assert translator.session.query(Translation).count() == 0 session.add(instance) assert translator.session.query(Translation).count() == 0 session.commit() assert translator.session.query(Translation).count() == 1 session.delete(instance) assert translator.session.query(Translation).count() == 1 session.rollback() assert translator.session.query(Translation).count() == 1 session.commit() assert translator.session.query(Translation).count() == 1 session.delete(instance) assert translator.session.query(Translation).count() == 1 session.commit() assert translator.session.query(Translation).count() == 0
def test_load_and_save(translating_manager, session): translator = Translator(Translation, session, 'language') manager = translating_manager translator.bind(manager) manager.save(CustomFieldsEntity) obj = CustomFieldsEntity(id=1, identifier='foo', name='name') manager.save(obj) loaded = manager.serialize( manager.get(CustomFieldsEntity, id=1) ) loaded['identifier'] = 'bar' manager.save( manager.deserialize(loaded) ) reloaded = manager.get(CustomFieldsEntity, id=1) assert translator.translate( manager.serialize(reloaded)['name'] ) == 'name'
def test_verify(self, session, session_cls): # check the test setup works translator = Translator(Translation, session_cls(), 'language') translator.bind(session) assert session not in flush_log assert translator.session not in flush_log instance1 = Model(name='instance 1') session.add(instance1) session.flush() assert session in flush_log assert len(flush_log[session]) == 1 session.commit() assert session not in flush_log instance2 = Model(name='instance 2') session.add(instance2) session.commit() assert session not in flush_log assert translator.session.query(Translation).count() == 2
def test_values(self, session_cls): session1 = session_cls() translator1 = Translator(Translation, session_cls(), 'language1') translator1.bind(session1) session2 = session_cls() translator2 = Translator(Translation, session_cls(), 'language2') translator2.bind(session2) instance = Model(name='name') session1.add(instance) session1.commit() pk = instance.id loaded = session2.query(Model).get(pk) assert loaded.name is not None instance.name = None session1.commit() session2.rollback() # expire isn't enough to trigger reloading assert loaded.name is None assert translator1.session.query(Translation).count() == 0
def test_serialize(session, translating_type_heirarchy, bound_manager): manager = bound_manager translator = Translator(Translation, session, 'en') translator.bind(manager) obj = CustomFieldsEntity(id=1, name='English name', extra1="", extra2=None) manager.save(CustomFieldsEntity) manager.save(obj) retrieved = manager.get(CustomFieldsEntity, id=1) assert retrieved.name == PlaceholderValue assert retrieved.extra1 == PlaceholderValue assert retrieved.extra2 is None serialized = manager.serialize(retrieved) assert isinstance(serialized['name'], TranslatableString) assert isinstance(serialized['extra1'], TranslatableString) assert serialized['extra2'] is None translated = translator.translate(serialized) assert translated['name'] == 'English name' assert translated['extra1'] == "" assert translated['extra2'] is None
def test_bind_unknown(): translator = Translator(None, None, None) with pytest.raises(BindError): translator.bind(None)