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

Commit

Permalink
refactor(tests): use factory-boy fixtures instead of importing factories
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Apr 16, 2024
1 parent 1412759 commit ce04c1d
Show file tree
Hide file tree
Showing 38 changed files with 1,021 additions and 755 deletions.
18 changes: 11 additions & 7 deletions timed/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def superadmin_user(db):


@pytest.fixture
def external_employee(db):
def external_employee(db, employment_factory):
user = get_user_model().objects.create_user(
username="user",
password="123qweasd",
Expand All @@ -71,12 +71,12 @@ def external_employee(db):
is_superuser=False,
is_staff=False,
)
employment_factories.EmploymentFactory.create(user=user, is_external=True)
employment_factory(user=user, is_external=True)
return user


@pytest.fixture
def internal_employee(db):
def internal_employee(db, employment_factory):
user = get_user_model().objects.create_user(
username="user",
password="123qweasd",
Expand All @@ -86,7 +86,7 @@ def internal_employee(db):
is_superuser=False,
is_staff=False,
)
employment_factories.EmploymentFactory.create(user=user, is_external=False)
employment_factory(user=user, is_external=False)
return user


Expand Down Expand Up @@ -146,7 +146,11 @@ def _autoclear_cache():


def setup_customer_and_employment_status(
user, is_assignee, is_customer, is_employed, is_external
user,
is_assignee,
is_customer,
is_employed,
is_external,
):
"""
Set up customer and employment status.
Expand All @@ -157,11 +161,11 @@ def setup_customer_and_employment_status(
assignee = None
employment = None
if is_assignee:
assignee = projects_factories.CustomerAssigneeFactory.create(
assignee = projects_factories.CustomerAssigneeFactory(
user=user, is_customer=is_customer
)
if is_employed:
employment = employment_factories.EmploymentFactory.create(
employment = employment_factories.EmploymentFactory(
user=user, is_external=is_external
)
return assignee, employment
73 changes: 43 additions & 30 deletions timed/employment/tests/test_absence_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@
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,
absence_credit_factory,
absence_factory,
absence_type_factory,
employment_factory,
):
day = date(2017, 2, 28)

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

AbsenceCreditFactory.create(date=day, user=user, days=5, absence_type=absence_type)
absence_credit_factory.create(
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.create(date=date.today(), 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)

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

Expand Down Expand Up @@ -56,25 +57,33 @@ 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,
absence_factory,
absence_type_factory,
employment_factory,
report_factory,
user_factory,
):
day = date(2017, 2, 28)

user = UserFactory.create()
user = user_factory.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 +109,9 @@ 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_factory):
user = auth_client.user
absence_type = AbsenceTypeFactory.create()
absence_type = absence_type_factory.create()
url = reverse(
"absence-balance-detail",
args=["{0}_{1}_2017-03-01".format(user.id, absence_type.id)],
Expand All @@ -120,10 +129,12 @@ def test_absence_balance_detail(auth_client):
assert entry["attributes"]["used-duration"] is None


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

result = auth_client.get(
url, data={"user": unrelated_user.id, "date": "2017-01-03"}
Expand All @@ -132,10 +143,12 @@ 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_factory, user_factory
):
url = reverse("absence-balance-list")
absence_type = AbsenceTypeFactory.create()
unrelated_user = UserFactory.create()
absence_type = absence_type_factory.create()
unrelated_user = user_factory.create()

url = reverse(
"absence-balance-detail",
Expand Down
34 changes: 15 additions & 19 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,8 +9,8 @@ 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_factory):
absence_type = absence_type_factory.create()

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

Expand All @@ -38,9 +32,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 +44,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 +55,15 @@ 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, absence_credit_factory, user_factory
):
user = user_factory.create()
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
34 changes: 21 additions & 13 deletions timed/employment/tests/test_absence_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from rest_framework import status

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


@pytest.mark.parametrize(
Expand All @@ -17,7 +16,12 @@
],
)
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(
user=auth_client.user,
Expand All @@ -26,7 +30,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 +40,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 +63,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,17 +84,17 @@ 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_factory):
absence_type = absence_type_factory.create()

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_factory):
absence_type = absence_type_factory.create()

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

Expand Down
Loading

0 comments on commit ce04c1d

Please sign in to comment.