Skip to content

Commit

Permalink
Improve assertions in sorting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliotrigo committed Mar 5, 2019
1 parent ce1ca02 commit e9155eb
Showing 1 changed file with 71 additions and 49 deletions.
120 changes: 71 additions & 49 deletions test/interface/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@pytest.fixture
def multiple_foos_inserted(session, multiple_bars_with_no_nulls_inserted):
def multiple_foos_inserted(session):
foo_1 = Foo(id=1, bar_id=1, name='name_1', count=1)
foo_2 = Foo(id=2, bar_id=2, name='name_2', count=1)
foo_3 = Foo(id=3, bar_id=3, name='name_1', count=1)
Expand Down Expand Up @@ -161,15 +161,18 @@ def test_multiple_sort_fields(self, session):
sorted_query = apply_sort(query, order_by)
results = sorted_query.all()

assert len(results) == 8
assert results[0].id == 1
assert results[1].id == 3
assert results[2].id == 7
assert results[3].id == 5
assert results[4].id == 2
assert results[5].id == 6
assert results[6].id == 4
assert results[7].id == 8
assert [
(result.name, result.count, result.id) for result in results
] == [
('name_1', 5, 1),
('name_1', 3, 3),
('name_1', 2, 7),
('name_1', 2, 5),
('name_2', 10, 2),
('name_4', 15, 6),
('name_4', 12, 4),
('name_5', 1, 8),
]

def test_multiple_models(self, session):

Expand Down Expand Up @@ -236,9 +239,11 @@ def test_a_single_dict_can_be_supplied_as_sort_spec(self, session):

class TestAutoJoin:

@pytest.mark.usefixtures('multiple_foos_inserted')
@pytest.mark.usefixtures(
'multiple_bars_with_no_nulls_inserted',
'multiple_foos_inserted'
)
def test_auto_join(self, session):

query = session.query(Foo)
order_by = [
{'field': 'count', 'direction': 'desc'},
Expand All @@ -249,19 +254,24 @@ def test_auto_join(self, session):
sorted_query = apply_sort(query, order_by)
results = sorted_query.all()

assert len(results) == 8
assert results[0].id == 5
assert results[1].id == 7
assert results[2].id == 6
assert results[3].id == 8
assert results[4].id == 1
assert results[5].id == 3
assert results[6].id == 2
assert results[7].id == 4

@pytest.mark.usefixtures('multiple_foos_inserted')
def test_noop_if_query_contains_named_models(self, session):
assert [
(result.count, result.bar.name, result.id) for result in results
] == [
(2, 'name_1', 5),
(2, 'name_1', 7),
(2, 'name_4', 6),
(2, 'name_5', 8),
(1, 'name_1', 1),
(1, 'name_1', 3),
(1, 'name_2', 2),
(1, 'name_4', 4),
]

@pytest.mark.usefixtures(
'multiple_bars_with_no_nulls_inserted',
'multiple_foos_inserted'
)
def test_noop_if_query_contains_named_models(self, session):
query = session.query(Foo).join(Bar)
order_by = [
{'model': 'Foo', 'field': 'count', 'direction': 'desc'},
Expand All @@ -272,19 +282,24 @@ def test_noop_if_query_contains_named_models(self, session):
sorted_query = apply_sort(query, order_by)
results = sorted_query.all()

assert len(results) == 8
assert results[0].id == 5
assert results[1].id == 7
assert results[2].id == 6
assert results[3].id == 8
assert results[4].id == 1
assert results[5].id == 3
assert results[6].id == 2
assert results[7].id == 4

@pytest.mark.usefixtures('multiple_foos_inserted')
def test_auto_join_to_invalid_model(self, session):
assert [
(result.count, result.bar.name, result.id) for result in results
] == [
(2, 'name_1', 5),
(2, 'name_1', 7),
(2, 'name_4', 6),
(2, 'name_5', 8),
(1, 'name_1', 1),
(1, 'name_1', 3),
(1, 'name_2', 2),
(1, 'name_4', 4),
]

@pytest.mark.usefixtures(
'multiple_bars_with_no_nulls_inserted',
'multiple_foos_inserted'
)
def test_auto_join_to_invalid_model(self, session):
query = session.query(Foo)
order_by = [
{'model': 'Foo', 'field': 'count', 'direction': 'desc'},
Expand All @@ -297,9 +312,11 @@ def test_auto_join_to_invalid_model(self, session):

assert 'The query does not contain model `Qux`.' == err.value.args[0]

@pytest.mark.usefixtures('multiple_foos_inserted')
@pytest.mark.usefixtures(
'multiple_bars_with_no_nulls_inserted',
'multiple_foos_inserted'
)
def test_ambiguous_query(self, session):

query = session.query(Foo).join(Bar)
order_by = [
{'field': 'count', 'direction': 'asc'}, # ambiguous
Expand All @@ -310,9 +327,11 @@ def test_ambiguous_query(self, session):

assert 'Ambiguous spec. Please specify a model.' == err.value.args[0]

@pytest.mark.usefixtures('multiple_foos_inserted')
@pytest.mark.usefixtures(
'multiple_bars_with_no_nulls_inserted',
'multiple_foos_inserted'
)
def test_eager_load(self, session):

# behaves as if the joinedload wasn't present
query = session.query(Foo).options(joinedload(Foo.bar))
order_by = [
Expand All @@ -324,12 +343,15 @@ def test_eager_load(self, session):
sorted_query = apply_sort(query, order_by)
results = sorted_query.all()

assert len(results) == 8
assert results[0].id == 5
assert results[1].id == 7
assert results[2].id == 6
assert results[3].id == 8
assert results[4].id == 1
assert results[5].id == 3
assert results[6].id == 2
assert results[7].id == 4
assert [
(result.count, result.bar.name, result.id) for result in results
] == [
(2, 'name_1', 5),
(2, 'name_1', 7),
(2, 'name_4', 6),
(2, 'name_5', 8),
(1, 'name_1', 1),
(1, 'name_1', 3),
(1, 'name_2', 2),
(1, 'name_4', 4),
]

0 comments on commit e9155eb

Please sign in to comment.