Skip to content

Commit

Permalink
Add Unit ordering by UnitGroup name
Browse files Browse the repository at this point in the history
  • Loading branch information
ranta authored and matti-lamppu committed Jun 11, 2024
1 parent 4205ab9 commit 95b8c0e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/graphql/types/unit/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Meta:
"rank",
"reservation_count",
"reservation_units_count",
("unit_groups__name", "unit_group_name"),
]

def filter_queryset(self, queryset):
Expand Down
34 changes: 33 additions & 1 deletion tests/test_graphql_api/test_unit/test_ordering.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from tests.factories import ReservationFactory, ReservationUnitFactory, UnitFactory, UserFactory
from tests.factories import ReservationFactory, ReservationUnitFactory, UnitFactory, UnitGroupFactory, UserFactory
from tests.helpers import UserType

from .helpers import units_query
Expand Down Expand Up @@ -91,3 +91,35 @@ def test_units__order__by_reservation_units_count(graphql):
assert response.node(1) == {"pk": unit_3.pk}
assert response.node(2) == {"pk": unit_2.pk}
assert response.node(3) == {"pk": unit_4.pk}


def test_units__order__unit_group_name(graphql):
unit_1 = UnitFactory.create(name="1")
unit_2 = UnitFactory.create(name="2")
unit_3 = UnitFactory.create(name="3")
unit_4 = UnitFactory.create(name="4")

UnitGroupFactory.create(name="AAA", units=[unit_1, unit_2])
UnitGroupFactory.create(name="BBB", units=[unit_4])
UnitGroupFactory.create(name="CCC", units=[unit_3, unit_1])

graphql.login_user_based_on_type(UserType.SUPERUSER)
response = graphql(units_query(order_by=["unitGroupNameAsc", "pkDesc"]))

assert response.has_errors is False, response.errors

assert len(response.edges) == 4
assert response.node(0) == {"pk": unit_2.pk}
assert response.node(1) == {"pk": unit_1.pk}
assert response.node(2) == {"pk": unit_4.pk}
assert response.node(3) == {"pk": unit_3.pk}

response = graphql(units_query(order_by=["unitGroupNameDesc", "pkDesc"]))

assert response.has_errors is False, response.errors

assert len(response.edges) == 4
assert response.node(0) == {"pk": unit_3.pk}
assert response.node(1) == {"pk": unit_1.pk}
assert response.node(2) == {"pk": unit_4.pk}
assert response.node(3) == {"pk": unit_2.pk}

0 comments on commit 95b8c0e

Please sign in to comment.