Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

draft: use pytest-factoryboy fixtures instead of imports #1080

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions timed/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

import inspect
from typing import TYPE_CHECKING

import pytest
from django.contrib.auth import get_user_model
Expand All @@ -12,6 +15,22 @@
from timed.subscription import factories as subscription_factories
from timed.tracking import factories as tracking_factories

if TYPE_CHECKING:
from typing import Protocol

from timed.employment import models

class SetupCustomerAndEmploymentStatus(Protocol):
def __call__(
self,
user: models.User,
*,
is_assignee: bool,
is_customer: bool,
is_employed: bool,
is_external: bool,
) -> tuple[models.CustomerAssignee, models.Employment]: ...


def register_module(module):
for _name, obj in inspect.getmembers(module):
Expand Down Expand Up @@ -145,22 +164,33 @@ def _autoclear_cache():
cache.clear()


@pytest.fixture
def setup_customer_and_employment_status(
user, is_assignee, is_customer, is_employed, is_external
):
"""Set up customer and employment status.

Return a 2-tuple of assignee and employment, if they
were created
"""
assignee = None
employment = None
if is_assignee:
assignee = projects_factories.CustomerAssigneeFactory.create(
user=user, is_customer=is_customer
)
if is_employed:
employment = employment_factories.EmploymentFactory.create(
user=user, is_external=is_external
)
return assignee, employment
db, # noqa: ARG001
customer_assignee_factory,
employment_factory,
) -> SetupCustomerAndEmploymentStatus:
def _setup_customer_and_employment_status(
user: models.User,
*,
is_assignee: bool,
is_customer: bool,
is_employed: bool,
is_external: bool,
):
"""Set up customer and employment status.

Return a 2-tuple of assignee and employment, if they
were created
"""
assignee = None
employment = None
if is_assignee:
assignee = customer_assignee_factory.create(
user=user, is_customer=is_customer
)
if is_employed:
employment = employment_factory.create(user=user, is_external=is_external)
return assignee, employment

return _setup_customer_and_employment_status
69 changes: 37 additions & 32 deletions timed/employment/tests/test_absence_balance.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
from datetime import date, timedelta

import pytest
from django.urls import reverse
from rest_framework import status

from timed.employment.factories import (
AbsenceCreditFactory,
AbsenceTypeFactory,
EmploymentFactory,
UserFactory,
)
from timed.tracking.factories import AbsenceFactory, ReportFactory


def test_absence_balance_full_day(auth_client, django_assert_num_queries):
def test_absence_balance_full_day(
auth_client,
django_assert_num_queries,
employment_factory,
absence_type_factory,
absence_credit_factory,
absence_factory,
):
day = date(2017, 2, 28)

user = auth_client.user
EmploymentFactory.create(user=user, start_date=day)
absence_type = AbsenceTypeFactory.create()
employment_factory(user=user, start_date=day)
absence_type = absence_type_factory()

AbsenceCreditFactory.create(date=day, user=user, days=5, absence_type=absence_type)
absence_credit_factory(date=day, user=user, days=5, absence_type=absence_type)

# credit on different user, may not show up
AbsenceCreditFactory.create(date=date.today(), absence_type=absence_type)
absence_credit_factory(date=date.today(), absence_type=absence_type)

AbsenceFactory.create(date=day, user=user, absence_type=absence_type)
absence_factory(date=day, user=user, absence_type=absence_type)

AbsenceFactory.create(
date=day - timedelta(days=1), user=user, absence_type=absence_type
)
absence_factory(date=day - timedelta(days=1), user=user, absence_type=absence_type)

url = reverse("absence-balance-list")

Expand Down Expand Up @@ -56,25 +54,32 @@ def test_absence_balance_full_day(auth_client, django_assert_num_queries):
assert len(json["included"]) == 2


def test_absence_balance_fill_worktime(auth_client, django_assert_num_queries):
def test_absence_balance_fill_worktime(
auth_client,
django_assert_num_queries,
user,
employment_factory,
absence_type_factory,
report_factory,
absence_factory,
):
day = date(2017, 2, 28)

user = UserFactory.create()
user.supervisors.add(auth_client.user)
EmploymentFactory.create(
employment_factory.create(
user=user, start_date=day, worktime_per_day=timedelta(hours=5)
)
absence_type = AbsenceTypeFactory.create(fill_worktime=True)
absence_type = absence_type_factory.create(fill_worktime=True)

ReportFactory.create(
report_factory.create(
user=user, date=day + timedelta(days=1), duration=timedelta(hours=4)
)

AbsenceFactory.create(
absence_factory.create(
date=day + timedelta(days=1), user=user, absence_type=absence_type
)

AbsenceFactory.create(date=day, user=user, absence_type=absence_type)
absence_factory.create(date=day, user=user, absence_type=absence_type)

url = reverse("absence-balance-list")
with django_assert_num_queries(11):
Expand All @@ -100,9 +105,8 @@ def test_absence_balance_fill_worktime(auth_client, django_assert_num_queries):
assert entry["attributes"]["used-duration"] == "06:00:00"


def test_absence_balance_detail(auth_client):
def test_absence_balance_detail(auth_client, absence_type):
user = auth_client.user
absence_type = AbsenceTypeFactory.create()
url = reverse(
"absence-balance-detail",
args=[f"{user.id}_{absence_type.id}_2017-03-01"],
Expand All @@ -120,10 +124,10 @@ def test_absence_balance_detail(auth_client):
assert entry["attributes"]["used-duration"] is None


def test_absence_balance_list_none_supervisee(auth_client):
@pytest.mark.usefixtures("absence_type")
def test_absence_balance_list_none_supervisee(auth_client, user_factory):
url = reverse("absence-balance-list")
AbsenceTypeFactory.create()
unrelated_user = UserFactory.create()
unrelated_user = user_factory.create()

result = auth_client.get(
url, data={"user": unrelated_user.id, "date": "2017-01-03"}
Expand All @@ -132,10 +136,11 @@ def test_absence_balance_list_none_supervisee(auth_client):
assert len(result.json()["data"]) == 0


def test_absence_balance_detail_none_supervisee(auth_client):
def test_absence_balance_detail_none_supervisee(
auth_client, absence_type, user_factory
):
url = reverse("absence-balance-list")
absence_type = AbsenceTypeFactory.create()
unrelated_user = UserFactory.create()
unrelated_user = user_factory.create()

url = reverse(
"absence-balance-detail",
Expand Down
31 changes: 11 additions & 20 deletions timed/employment/tests/test_absence_credit.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
from django.urls import reverse
from rest_framework import status

from timed.employment.factories import (
AbsenceCreditFactory,
AbsenceTypeFactory,
UserFactory,
)


def test_absence_credit_create_authenticated(auth_client):
url = reverse("absence-credit-list")
Expand All @@ -15,9 +9,7 @@ def test_absence_credit_create_authenticated(auth_client):
assert result.status_code == status.HTTP_403_FORBIDDEN


def test_absence_credit_create_superuser(superadmin_client):
absence_type = AbsenceTypeFactory.create()

def test_absence_credit_create_superuser(superadmin_client, absence_type):
url = reverse("absence-credit-list")

data = {
Expand All @@ -38,9 +30,9 @@ def test_absence_credit_create_superuser(superadmin_client):
assert result.status_code == status.HTTP_201_CREATED


def test_absence_credit_get_authenticated(auth_client):
AbsenceCreditFactory.create_batch(2)
absence_credit = AbsenceCreditFactory.create(user=auth_client.user)
def test_absence_credit_get_authenticated(auth_client, absence_credit_factory):
absence_credit_factory.create_batch(2)
absence_credit = absence_credit_factory.create(user=auth_client.user)
url = reverse("absence-credit-list")

result = auth_client.get(url)
Expand All @@ -50,9 +42,9 @@ def test_absence_credit_get_authenticated(auth_client):
assert json["data"][0]["id"] == str(absence_credit.id)


def test_absence_credit_get_superuser(superadmin_client):
AbsenceCreditFactory.create_batch(2)
AbsenceCreditFactory.create(user=superadmin_client.user)
def test_absence_credit_get_superuser(superadmin_client, absence_credit_factory):
absence_credit_factory.create_batch(2)
absence_credit_factory.create(user=superadmin_client.user)
url = reverse("absence-credit-list")

result = superadmin_client.get(url)
Expand All @@ -61,13 +53,12 @@ def test_absence_credit_get_superuser(superadmin_client):
assert len(json["data"]) == 3


def test_absence_credit_get_supervisor(auth_client):
user = UserFactory.create()
def test_absence_credit_get_supervisor(auth_client, user, absence_credit_factory):
auth_client.user.supervisees.add(user)

AbsenceCreditFactory.create_batch(1)
AbsenceCreditFactory.create(user=auth_client.user)
AbsenceCreditFactory.create(user=user)
absence_credit_factory.create_batch(1)
absence_credit_factory.create(user=auth_client.user)
absence_credit_factory.create(user=user)
url = reverse("absence-credit-list")

result = auth_client.get(url)
Expand Down
37 changes: 20 additions & 17 deletions timed/employment/tests/test_absence_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
from django.urls import reverse
from rest_framework import status

from timed.conftest import setup_customer_and_employment_status
from timed.employment.factories import AbsenceTypeFactory, EmploymentFactory


@pytest.mark.parametrize(
("is_employed", "is_customer_assignee", "is_customer", "expected"),
Expand All @@ -17,7 +14,13 @@
],
)
def test_absence_type_list(
auth_client, is_employed, is_customer_assignee, is_customer, expected
auth_client,
is_employed,
is_customer_assignee,
is_customer,
expected,
absence_type_factory,
setup_customer_and_employment_status,
):
setup_customer_and_employment_status(
user=auth_client.user,
Expand All @@ -26,7 +29,7 @@ def test_absence_type_list(
is_employed=is_employed,
is_external=False,
)
AbsenceTypeFactory.create_batch(2)
absence_type_factory.create_batch(2)
url = reverse("absence-type-list")

response = auth_client.get(url)
Expand All @@ -36,9 +39,11 @@ def test_absence_type_list(
assert len(json["data"]) == expected


def test_absence_type_list_filter_fill_worktime(internal_employee_client):
absence_type = AbsenceTypeFactory.create(fill_worktime=True)
AbsenceTypeFactory.create()
def test_absence_type_list_filter_fill_worktime(
internal_employee_client, absence_type_factory
):
absence_type = absence_type_factory.create(fill_worktime=True)
absence_type_factory.create()

url = reverse("absence-type-list")

Expand All @@ -57,10 +62,12 @@ def test_absence_type_list_filter_fill_worktime(internal_employee_client):
(False, status.HTTP_404_NOT_FOUND),
],
)
def test_absence_type_detail(auth_client, is_employed, expected):
absence_type = AbsenceTypeFactory.create()
def test_absence_type_detail(
auth_client, is_employed, expected, absence_type_factory, employment_factory
):
absence_type = absence_type_factory.create()
if is_employed:
EmploymentFactory.create(user=auth_client.user)
employment_factory.create(user=auth_client.user)

url = reverse("absence-type-detail", args=[absence_type.id])

Expand All @@ -76,18 +83,14 @@ def test_absence_type_create(auth_client):
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED


def test_absence_type_update(auth_client):
absence_type = AbsenceTypeFactory.create()

def test_absence_type_update(auth_client, absence_type):
url = reverse("absence-type-detail", args=[absence_type.id])

response = auth_client.patch(url)
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED


def test_absence_type_delete(auth_client):
absence_type = AbsenceTypeFactory.create()

def test_absence_type_delete(auth_client, absence_type):
url = reverse("absence-type-detail", args=[absence_type.id])

response = auth_client.delete(url)
Expand Down
Loading
Loading