Files
deb-python-sqlalchemy-utils/tests/functions/test_render.py
Jacob Magnusson 815f07d6c1 Use pytest fixtures to reduce complexity and repetition
Also:

Allow override of database name and user in tests (important for me as I would have to mess with my PSQL and MySQL database users otherwise)
Use dict.items instead of six.iteritems as it sporadically caused RuntimeError: dictionary changed size during iteration in Python 2.6 tests.
Fix typo DNS to DSN
Adds Python 3.5 to tox.ini
Added an .editorconfig
Import babel.dates in sqlalchemy_utils.i18n as an exception would be raised when using the latest versions of babel.
2016-01-19 10:52:30 +01:00

66 lines
1.9 KiB
Python

import pytest
import sqlalchemy as sa
from sqlalchemy_utils.functions import (
mock_engine,
render_expression,
render_statement
)
class TestRender(object):
@pytest.fixture
def User(self, Base):
class User(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)
name = sa.Column(sa.Unicode(255))
return User
@pytest.fixture
def init_models(self, User):
pass
def test_render_orm_query(self, session, User):
query = session.query(User).filter_by(id=3)
text = render_statement(query)
assert 'SELECT user.id, user.name' in text
assert 'FROM user' in text
assert 'WHERE user.id = 3' in text
def test_render_statement(self, session, User):
statement = User.__table__.select().where(User.id == 3)
text = render_statement(statement, bind=session.bind)
assert 'SELECT user.id, user.name' in text
assert 'FROM user' in text
assert 'WHERE user.id = 3' in text
def test_render_statement_without_mapper(self, session):
statement = sa.select([sa.text('1')])
text = render_statement(statement, bind=session.bind)
assert 'SELECT 1' in text
def test_render_ddl(self, engine, User):
expression = 'User.__table__.create(engine)'
stream = render_expression(expression, engine)
text = stream.getvalue()
assert 'CREATE TABLE user' in text
assert 'PRIMARY KEY' in text
def test_render_mock_ddl(self, engine, User):
# TODO: mock_engine doesn't seem to work with locally scoped variables.
self.engine = engine
with mock_engine('self.engine') as stream:
User.__table__.create(self.engine)
text = stream.getvalue()
assert 'CREATE TABLE user' in text
assert 'PRIMARY KEY' in text