Files
deb-python-sqlalchemy-utils/tests/relationships/test_chained_join.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

107 lines
3.5 KiB
Python

import pytest
from sqlalchemy_utils.relationships import chained_join
from ..mixins import (
ThreeLevelDeepManyToMany,
ThreeLevelDeepOneToMany,
ThreeLevelDeepOneToOne
)
@pytest.mark.usefixtures('postgresql_dsn')
class TestChainedJoinFoDeepToManyToMany(ThreeLevelDeepManyToMany):
def test_simple_join(self, Catalog):
assert str(chained_join(Catalog.categories)) == (
'catalog_category JOIN category ON '
'category._id = catalog_category.category_id'
)
def test_two_relations(self, Catalog, Category):
sql = chained_join(
Catalog.categories,
Category.sub_categories
)
assert str(sql) == (
'catalog_category JOIN category ON category._id = '
'catalog_category.category_id JOIN category_subcategory ON '
'category._id = category_subcategory.category_id JOIN '
'sub_category ON sub_category._id = '
'category_subcategory.subcategory_id'
)
def test_three_relations(self, Catalog, Category, SubCategory):
sql = chained_join(
Catalog.categories,
Category.sub_categories,
SubCategory.products
)
assert str(sql) == (
'catalog_category JOIN category ON category._id = '
'catalog_category.category_id JOIN category_subcategory ON '
'category._id = category_subcategory.category_id JOIN sub_category'
' ON sub_category._id = category_subcategory.subcategory_id JOIN '
'subcategory_product ON sub_category._id = '
'subcategory_product.subcategory_id JOIN product ON product._id ='
' subcategory_product.product_id'
)
@pytest.mark.usefixtures('postgresql_dsn')
class TestChainedJoinForDeepOneToMany(ThreeLevelDeepOneToMany):
def test_simple_join(self, Catalog):
assert str(chained_join(Catalog.categories)) == 'category'
def test_two_relations(self, Catalog, Category):
sql = chained_join(
Catalog.categories,
Category.sub_categories
)
assert str(sql) == (
'category JOIN sub_category ON category._id = '
'sub_category._category_id'
)
def test_three_relations(self, Catalog, Category, SubCategory):
sql = chained_join(
Catalog.categories,
Category.sub_categories,
SubCategory.products
)
assert str(sql) == (
'category JOIN sub_category ON category._id = '
'sub_category._category_id JOIN product ON sub_category._id = '
'product._sub_category_id'
)
@pytest.mark.usefixtures('postgresql_dsn')
class TestChainedJoinForDeepOneToOne(ThreeLevelDeepOneToOne):
def test_simple_join(self, Catalog):
assert str(chained_join(Catalog.category)) == 'category'
def test_two_relations(self, Catalog, Category):
sql = chained_join(
Catalog.category,
Category.sub_category
)
assert str(sql) == (
'category JOIN sub_category ON category._id = '
'sub_category._category_id'
)
def test_three_relations(self, Catalog, Category, SubCategory):
sql = chained_join(
Catalog.category,
Category.sub_category,
SubCategory.product
)
assert str(sql) == (
'category JOIN sub_category ON category._id = '
'sub_category._category_id JOIN product ON sub_category._id = '
'product._sub_category_id'
)