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

67 lines
1.7 KiB
Python

import pytest
import sqlalchemy as sa
from sqlalchemy_utils import aggregated, TSVectorType
def tsvector_reduce_concat(vectors):
return sa.sql.expression.cast(
sa.func.coalesce(
sa.func.array_to_string(sa.func.array_agg(vectors), ' ')
),
TSVectorType
)
@pytest.fixture
def Product(Base):
class Product(Base):
__tablename__ = 'product'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
price = sa.Column(sa.Numeric)
catalog_id = sa.Column(sa.Integer, sa.ForeignKey('catalog.id'))
return Product
@pytest.fixture
def Catalog(Base, Product):
class Catalog(Base):
__tablename__ = 'catalog'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
@aggregated('products', sa.Column(TSVectorType))
def product_search_vector(self):
return tsvector_reduce_concat(
sa.func.to_tsvector(Product.name)
)
products = sa.orm.relationship('Product', backref='catalog')
return Catalog
@pytest.fixture
def init_models(Product, Catalog):
pass
@pytest.mark.usefixtures('postgresql_dsn')
class TestSearchVectorAggregates(object):
def test_assigns_aggregates_on_insert(self, session, Product, Catalog):
catalog = Catalog(
name=u'Some catalog'
)
session.add(catalog)
session.commit()
product = Product(
name=u'Product XYZ',
catalog=catalog
)
session.add(product)
session.commit()
session.refresh(catalog)
assert catalog.product_search_vector == "'product':1 'xyz':2"