Skip to content

Commit

Permalink
Merge bc90537 into c86a1da
Browse files Browse the repository at this point in the history
  • Loading branch information
blaise-io committed Nov 14, 2018
2 parents c86a1da + bc90537 commit e3f1485
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
18 changes: 13 additions & 5 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@pytest.fixture(scope='session')
def languages():
"""Supported languages."""
return ['en', 'pt']
return ['en', 'nl', 'pt']


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -68,7 +68,7 @@ def engine(request, model_class):
@pytest.fixture
def model_title():
"""Model title."""
return {'en': 'En Title', 'pt': 'Pt Title'}
return {'en': 'En Title', 'nl': 'Nl Title', 'pt': 'Pt Title'}


@pytest.fixture
Expand Down Expand Up @@ -110,7 +110,15 @@ def test_set(model):
assert model.title.get_dict() == {'en': 'New'}


def test_comparator(session, model, model_class):
"""Test field comparator."""
def test_startswith(session, model, model_class):
"""Test startswith comparator."""
assert model in session.query(model_class).filter(model_class.title.startswith('En Title'))
assert model not in session.query(model_class).filter(model_class.title.startswith('Pt Title'))
assert model in session.query(model_class).filter(model_class.title.startswith('Pt Title'))
assert model not in session.query(model_class).filter(model_class.title.startswith('Fr Title'))


def test_contains(session, model, model_class):
"""Test contains comparator."""
assert model in session.query(model_class).filter(model_class.title.contains('En'))
assert model in session.query(model_class).filter(model_class.title.contains('Pt'))
assert model not in session.query(model_class).filter(model_class.title.contains('Fr'))
18 changes: 10 additions & 8 deletions traduki/sqla.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sqlalchemy.orm import relationship
from sqlalchemy.orm.interfaces import AttributeExtension
from sqlalchemy.orm.properties import RelationshipProperty
from sqlalchemy.sql import operators as oper, functions as func
from sqlalchemy.sql import operators as oper, or_

from traduki import config
from traduki import helpers
Expand Down Expand Up @@ -95,9 +95,7 @@ class TranslationComparator(RelationshipProperty.Comparator):
All the `like` operations will look into next language if the specified language is not filled in.
"""

LIKE_OPS = set([
oper.like_op, oper.contains_op, oper.startswith_op,
oper.endswith_op])
LIKE_OPS = {oper.like_op, oper.contains_op, oper.startswith_op, oper.endswith_op}

# Only the operators in LIKE_OPS are allowed on this relationship
def operate(self, op, *other, **kw):
Expand All @@ -111,12 +109,16 @@ def contains(self, other, escape=None):
return self.operate(oper.contains_op, other, escape=escape)

def _do_compare(self, op, other, escape):
"""Perform coalesced comparison operations to the columns of Translation model.
Looking into the the next language if the given language is not filled in.
"""Perform comparison operations to the columns of the Translation model.
Looking into all languages using the OR operator.
"""
related = self.property.mapper.class_
cols = [getattr(related, lang) for lang in helpers.get_ordered_languages() if hasattr(related, lang)]
return self.has(op(func.coalesce(*cols), other, escape=escape))
ops = [
op(getattr(related, lang), other, escape=escape)
for lang in helpers.get_ordered_languages()
if hasattr(related, lang)
]
return self.has(or_(*ops))

class TranslationExtension(AttributeExtension):
"""AttributeExtension to override the behavior of .set, to accept a dict as new value."""
Expand Down

0 comments on commit e3f1485

Please sign in to comment.