Skip to content

Commit

Permalink
Merge branch 'master' of github.com:paylogic/traduki
Browse files Browse the repository at this point in the history
  • Loading branch information
bubenkoff committed Mar 7, 2014
2 parents f98727e + 0fdea47 commit a438bad
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 138 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ traduki usage example:
"""Language chain (fallback rule) callback for our project."""
return {'*': request.locale}
traduki.initialize(Base, ['en', 'ru'], get_current_language, get_language_chain)
i18n_attributes = traduki.initialize(Base, ['en', 'ru'], get_current_language, get_language_chain)
Session = sessionmaker(bind=engine)
sess = Session()
class MyModel(Base)
title_id = traduki.i18n_column(nullable=False, unique=False)
title = traduki.i18n_relation(title_id)
title_id = i18n_attributes.i18n_column(nullable=False, unique=False)
title = i18n_attributes.i18n_relation(title_id)
"""Title."""
my_object = MyModel()
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def run_tests(self):
author='Paylogic International',
author_email='[email protected]',
license='MIT',
url='https://github.com/paylogic/traduki',
install_requires=[
'SQLAlchemy'
],
Expand Down
90 changes: 51 additions & 39 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,32 @@
import traduki


@pytest.fixture
Base = declarative_base()


@pytest.fixture(scope='session')
def languages():
"""Supported languages."""
return ['en', 'pt']


@pytest.fixture
def model_class(languages):
"""Create test model class."""
Base = declarative_base()
@pytest.fixture(scope='session')
def i18n_attributes(languages):
"""i18n attributes"""
return traduki.initialize(Base, languages, lambda: 'en', lambda: {})

traduki.initialize(Base, languages, lambda: 'en', lambda: {})

@pytest.fixture(scope='session')
def model_class(i18n_attributes):
"""Create test model class."""

class TestModel(Base):
__tablename__ = 'test_table'

id = Column(Integer, primary_key=True)
id = Column(Integer, primary_key=True, autoincrement=True)

title_id = traduki.i18n_column(nullable=False, unique=False)
title = traduki.i18n_relation(title_id)
title_id = i18n_attributes.i18n_column(nullable=False, unique=False)
title = i18n_attributes.i18n_relation(title_id)
"""Title."""

def save():
Expand All @@ -38,22 +44,6 @@ def save():
return TestModel


@pytest.fixture
def model(model_class):
"""Create test model instance."""
return model_class(id=1)


@pytest.fixture
def model_form_class(model_class):
"""Create test model form class."""
class TestModelForm(traduki.SAModelForm):
class Meta:
model = model_class

return TestModelForm


@pytest.fixture
def query(model_class, session):
"""Create SQLAlchemy Query for model_class."""
Expand All @@ -67,38 +57,60 @@ def session(request, engine):
return session


@pytest.fixture
@pytest.fixture(scope='session')
def engine(request, model_class):
"""SQLAlchemy engine."""
engine = create_engine('sqlite:///:memory:')
model_class.metadata.create_all(engine)
return engine


def test_language_fields(model_class, languages):
"""Check that Translation class is properly generated during initialize."""
from traduki import sqla
assert set(dir(sqla.Translation)).issuperset(languages)
for lang in languages:
assert getattr(sqla.Translation, lang).type.__class__ == UnicodeText

@pytest.fixture
def model_title():
"""Model title."""
return {'en': 'En Title', 'pt': 'Pt Title'}

def test_i18n_field(query, model, session):
"""Test i18n field."""
model.title = {'en': 'En Title', 'pt': 'Pt Title'}

@pytest.fixture
def model(session, model_class, model_title):
"""Model instance."""
model = model_class(title=model_title)
session.add(model)
session.commit()
session.refresh(model)
return model


def test_language_fields(model_class, languages, i18n_attributes):
"""Check that Translation class is properly generated during initialize."""
assert set(dir(i18n_attributes.Translation)).issuperset(languages)
for lang in languages:
assert getattr(i18n_attributes.Translation, lang).type.__class__ == UnicodeText

assert model.title.get_dict() == {'en': 'En Title', 'pt': 'Pt Title'}

def test_i18n_field(query, model, model_title):
"""Test i18n field."""
assert model.title.get_dict() == model_title
assert model.title.get_text() == 'En Title'


def test_class_custom_table(languages):
"""Test initialization with custom table."""
Base = declarative_base()

traduki.initialize(Base, languages, lambda: 'en', lambda: {}, attributes={'__tablename__': 'custom_table'})
i18n_attributes = traduki.initialize(
Base, languages, lambda: 'en', lambda: {}, attributes={'__tablename__': 'custom_table'})

assert i18n_attributes.Translation.__tablename__ == 'custom_table'


def test_set(model):
"""Test changing of the field value."""
model.title = {'en': 'New'}
assert model.title.get_dict() == {'en': 'New'}


assert traduki.sqla.Translation.__tablename__ == 'custom_table'
def test_comparator(session, model, model_class):
"""Test field 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'))
4 changes: 2 additions & 2 deletions traduki/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from traduki.sqla import initialize, i18n_column, i18n_relation
from traduki.sqla import initialize

__all__ = (initialize.__name__, i18n_column.__name__, i18n_relation.__name__)
__all__ = (initialize.__name__)
Loading

0 comments on commit a438bad

Please sign in to comment.