Skip to content

Commit

Permalink
Add more filter options to unitsAll endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ranta committed Dec 11, 2024
1 parent a993056 commit e1e287f
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 124 deletions.
116 changes: 82 additions & 34 deletions tests/test_graphql_api/test_unit/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,42 @@
import freezegun
import pytest
from django.utils.timezone import get_default_timezone
from graphene_django_extensions.testing import build_query
from graphene_django.settings import graphene_settings

from tilavarauspalvelu.enums import ReservationKind

from tests.factories import ReservationFactory, ReservationUnitFactory, UnitFactory, UserFactory

from .helpers import units_query
from .helpers import units_all_query, units_query

# Applied to all tests
pytestmark = [
pytest.mark.django_db,
]


def test_units__filter__by_name(graphql):
@pytest.mark.parametrize("gql_query", [units_query, units_all_query])
def test_units__filter__by_name(graphql, gql_query):
unit = UnitFactory.create(name_fi="1111")
UnitFactory.create(name_fi="2222")
UnitFactory.create(name_fi="3333")

graphql.login_with_superuser()
response = graphql(units_query(nameFi="111"))
response = graphql(gql_query(nameFi="111"))

assert response.has_errors is False
assert response.has_errors is False, response.errors

assert len(response.edges) == 1
assert response.node(0) == {"pk": unit.pk}
if gql_query == units_query:
assert len(response.edges) == 1
assert response.node(0) == {"pk": unit.pk}
else:
assert len(response.first_query_object) == 1
assert response.first_query_object[0] == {"pk": unit.pk}


@freezegun.freeze_time("2021-01-01T12:00:00Z")
def test_units__filter__by_published_reservation_units(graphql):
@pytest.mark.parametrize("gql_query", [units_query, units_all_query])
def test_units__filter__by_published_reservation_units(graphql, gql_query):
unit_1 = UnitFactory.create(name="1")
unit_2 = UnitFactory.create(name="2")
unit_3 = UnitFactory.create(name="3")
Expand All @@ -53,18 +59,24 @@ def test_units__filter__by_published_reservation_units(graphql):

graphql.login_with_superuser()

query = build_query("units", connection=True, published_reservation_units=True, order_by="nameFiAsc")
response = graphql(query)
response = graphql(gql_query(published_reservation_units=True, order_by="nameFiAsc"))

assert response.has_errors is False

assert len(response.edges) == 3
assert response.node(0) == {"pk": unit_1.pk}
assert response.node(1) == {"pk": unit_2.pk}
assert response.node(2) == {"pk": unit_3.pk}
if gql_query == units_query:
assert len(response.edges) == 3
assert response.node(0) == {"pk": unit_1.pk}
assert response.node(1) == {"pk": unit_2.pk}
assert response.node(2) == {"pk": unit_3.pk}
else:
assert len(response.first_query_object) == 3
assert response.first_query_object[0] == {"pk": unit_1.pk}
assert response.first_query_object[1] == {"pk": unit_2.pk}
assert response.first_query_object[2] == {"pk": unit_3.pk}


def test_units__filter__by_own_reservations(graphql):
@pytest.mark.parametrize("gql_query", [units_query, units_all_query])
def test_units__filter__by_own_reservations(graphql, gql_query):
unit_1 = UnitFactory.create(name="1")
unit_2 = UnitFactory.create(name="2")
unit_3 = UnitFactory.create(name="3")
Expand All @@ -85,22 +97,32 @@ def test_units__filter__by_own_reservations(graphql):
graphql.force_login(user_1)

# Own reservations = True
response = graphql(units_query(own_reservations=True))
response = graphql(gql_query(own_reservations=True))
assert response.has_errors is False

assert len(response.edges) == 2
assert response.node(0) == {"pk": unit_1.pk}
assert response.node(1) == {"pk": unit_2.pk}
if gql_query == units_query:
assert len(response.edges) == 2
assert response.node(0) == {"pk": unit_1.pk}
assert response.node(1) == {"pk": unit_2.pk}
else:
assert len(response.first_query_object) == 2
assert response.first_query_object[0] == {"pk": unit_1.pk}
assert response.first_query_object[1] == {"pk": unit_2.pk}

# Own reservations = False
response = graphql(units_query(own_reservations=False))
response = graphql(gql_query(own_reservations=False))
assert response.has_errors is False

assert len(response.edges) == 1
assert response.node(0) == {"pk": unit_3.pk}
if gql_query == units_query:
assert len(response.edges) == 1
assert response.node(0) == {"pk": unit_3.pk}
else:
assert len(response.first_query_object) == 1
assert response.first_query_object[0] == {"pk": unit_3.pk}


def test_units__filter__by_only_direct_bookable(graphql):
@pytest.mark.parametrize("gql_query", [units_query, units_all_query])
def test_units__filter__by_only_direct_bookable(graphql, gql_query):
# Has only direct reservation unit, should be included
unit_1 = UnitFactory.create(name="1")
ReservationUnitFactory.create(unit=unit_1, reservation_kind=ReservationKind.DIRECT)
Expand All @@ -121,17 +143,24 @@ def test_units__filter__by_only_direct_bookable(graphql):
# Has no reservation units, should be excluded
UnitFactory.create(name="5")

query = units_query(only_direct_bookable=True)
query = gql_query(only_direct_bookable=True)
response = graphql(query)
assert response.has_errors is False

assert len(response.edges) == 3
assert response.node(0) == {"pk": unit_1.pk}
assert response.node(1) == {"pk": unit_3.pk}
assert response.node(2) == {"pk": unit_4.pk}
if gql_query == units_query:
assert len(response.edges) == 3
assert response.node(0) == {"pk": unit_1.pk}
assert response.node(1) == {"pk": unit_3.pk}
assert response.node(2) == {"pk": unit_4.pk}
else:
assert len(response.first_query_object) == 3
assert response.first_query_object[0] == {"pk": unit_1.pk}
assert response.first_query_object[1] == {"pk": unit_3.pk}
assert response.first_query_object[2] == {"pk": unit_4.pk}


def test_units__filter__by_only_seasonal_bookable(graphql):
@pytest.mark.parametrize("gql_query", [units_query, units_all_query])
def test_units__filter__by_only_seasonal_bookable(graphql, gql_query):
# Has only direct reservation unit, should be excluded
unit_1 = UnitFactory.create(name="1")
ReservationUnitFactory.create(unit=unit_1, reservation_kind=ReservationKind.DIRECT)
Expand All @@ -152,11 +181,30 @@ def test_units__filter__by_only_seasonal_bookable(graphql):
# Has no reservation units, should be excluded
UnitFactory.create(name="5")

query = units_query(only_seasonal_bookable=True)
query = gql_query(only_seasonal_bookable=True)
response = graphql(query)
assert response.has_errors is False

assert len(response.edges) == 3
assert response.node(0) == {"pk": unit_2.pk}
assert response.node(1) == {"pk": unit_3.pk}
assert response.node(2) == {"pk": unit_4.pk}
if gql_query == units_query:
assert len(response.edges) == 3
assert response.node(0) == {"pk": unit_2.pk}
assert response.node(1) == {"pk": unit_3.pk}
assert response.node(2) == {"pk": unit_4.pk}
else:
assert len(response.first_query_object) == 3
assert response.first_query_object[0] == {"pk": unit_2.pk}
assert response.first_query_object[1] == {"pk": unit_3.pk}
assert response.first_query_object[2] == {"pk": unit_4.pk}


def test_unit_all__no_pagination_limit(graphql):
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 1

UnitFactory.create_batch(2)

graphql.login_with_superuser()
query = units_all_query(fields="pk nameFi nameEn nameSv tprekId")
response = graphql(query)

assert response.has_errors is False, response.errors
assert len(response.first_query_object) == 2
53 changes: 0 additions & 53 deletions tests/test_graphql_api/test_unit/test_query_all.py

This file was deleted.

18 changes: 17 additions & 1 deletion tests/test_graphql_api/test_unit/test_query_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from tests.factories import PaymentMerchantFactory, UnitFactory, UnitGroupFactory, UserFactory

from .helpers import units_query
from .helpers import units_all_query, units_query

# Applied to all tests
pytestmark = [
Expand Down Expand Up @@ -91,3 +91,19 @@ def test_units__query__show_payment_merchant_with_permissions(graphql):

assert len(response.edges) == 1
assert response.node(0) == {"nameFi": unit.name, "paymentMerchant": {"name": unit.payment_merchant.name}}


def test_unit_all__filter__only_with_permission__general_admin(graphql):
unit_1 = UnitFactory.create()
unit_2 = UnitFactory.create()

user = UserFactory.create_with_general_role()
graphql.force_login(user)

query = units_all_query(onlyWithPermission=True)
response = graphql(query)

assert response.has_errors is False, response.errors
assert len(response.first_query_object) == 2
assert response.first_query_object[0] == {"pk": unit_1.pk}
assert response.first_query_object[1] == {"pk": unit_2.pk}
Loading

0 comments on commit e1e287f

Please sign in to comment.