<commit_before>import pytest
import sqlalchemy as sa

from sqlalchemy_utils.functions.sort_query import make_order_by_deterministic

from .. import assert_contains


@pytest.fixture
def Article(Base):
    class Article(Base):
        __tablename__ = 'article'
        id = sa.Column(sa.Integer, primary_key=True)
        author_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
        author = sa.orm.relationship('User')
    return Article


@pytest.fixture
def User(Base, Article):
    class User(Base):
        __tablename__ = 'user'
        id = sa.Column(sa.Integer, primary_key=True)
        name = sa.Column(sa.Unicode)
        email = sa.Column(sa.Unicode, unique=True)

        email_lower = sa.orm.column_property(
            sa.func.lower(name)
        )

    User.article_count = sa.orm.column_property(
        sa.select([sa.func.count()], from_obj=Article)
        .where(Article.author_id == User.id)
        .label('article_count')
    )
    return User


@pytest.fixture
def init_models(Article, User):
    pass


class TestMakeOrderByDeterministic(object):

    def test_column_property(self, session, User):
        query = session.query(User).order_by(User.email_lower)
        query = make_order_by_deterministic(query)
        assert_contains('lower("user".name), "user".id ASC', query)

    def test_unique_column(self, session, User):
        query = session.query(User).order_by(User.email)
        query = make_order_by_deterministic(query)

        assert str(query).endswith('ORDER BY "user".email')

    def test_non_unique_column(self, session, User):
        query = session.query(User).order_by(User.name)
        query = make_order_by_deterministic(query)
        assert_contains('ORDER BY "user".name, "user".id ASC', query)

    def test_descending_order_by(self, session, User):
        query = session.query(User).order_by(
            sa.desc(User.name)
        )
        query = make_order_by_deterministic(query)
        assert_contains('ORDER BY "user".name DESC, "user".id DESC', query)

    def test_ascending_order_by(self, session, User):
        query = session.query(User).order_by(
            sa.asc(User.name)
        )<commit_msg>Fix deterministic order test<commit_after>import pytest
import sqlalchemy as sa

from sqlalchemy_utils.functions.sort_query import make_order_by_deterministic

from .. import assert_contains


@pytest.fixture
def Article(Base):
    class Article(Base):
        __tablename__ = 'article'
        id = sa.Column(sa.Integer, primary_key=True)
        author_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
        author = sa.orm.relationship('User')
    return Article


@pytest.fixture
def User(Base, Article):
    class User(Base):
        __tablename__ = 'user'
        id = sa.Column(sa.Integer, primary_key=True)
        name = sa.Column(sa.Unicode)
        email = sa.Column(sa.Unicode, unique=True)

        email_lower = sa.orm.column_property(
            sa.func.lower(name)
        )

    User.article_count = sa.orm.column_property(
        sa.select([sa.func.count()], from_obj=Article)
        .where(Article.author_id == User.id)
        .label('article_count')
    )
    return User


@pytest.fixture
def init_models(Article, User):
    pass


class TestMakeOrderByDeterministic(object):

    def test_column_property(self, session, User):
        query = session.query(User).order_by(User.email_lower)
        query = make_order_by_deterministic(query)
        assert_contains('lower("user".name) AS lower_1', query)
        assert_contains('lower_1, "user".id ASC', query)

    def test_unique_column(self, session, User):
        query = session.query(User).order_by(User.email)
        query = make_order_by_deterministic(query)

        assert str(query).endswith('ORDER BY "user".email')

    def test_non_unique_column(self, session, User):
        query = session.query(User).order_by(User.name)
        query = make_order_by_deterministic(query)
        assert_contains('ORDER BY "user".name, "user".id ASC', query)

    def test_descending_order_by(self, session, User):
        query = session.query(User).order_by(
            sa.desc(User.name)
        )
        query = make_order_by_deterministic(query)
        assert_contains('ORDER BY "user".name DESC, "user".id DESC', query)

    def test_ascending_order_by(self, session, User):
        query = session.query(User).order_by(
            sa.asc(User.name)
        )
{'fraction_non_alphanumeric': 0.1150900900900901, 'fraction_numerical': 0.00045045045045045046, 'mean_word_length': 3.0818014705882355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6075273', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1401, 'n_words': 259}