From 1385228ebd561a9071613c4acdc655f7ff4903f9 Mon Sep 17 00:00:00 2001 From: Arthur Deierlein Date: Mon, 29 Apr 2024 18:20:52 +0200 Subject: [PATCH] draft: use pytest-factoryboy fixtures instead of imports --- timed/conftest.py | 66 ++++-- .../employment/tests/test_absence_balance.py | 69 +++--- timed/employment/tests/test_absence_credit.py | 31 +-- timed/employment/tests/test_absence_type.py | 37 +-- timed/employment/tests/test_employment.py | 112 ++++----- timed/employment/tests/test_location.py | 25 +- .../employment/tests/test_overtime_credit.py | 23 +- timed/employment/tests/test_public_holiday.py | 33 ++- timed/employment/tests/test_user.py | 125 +++++----- .../employment/tests/test_worktime_balance.py | 62 ++--- .../notifications/tests/test_budget_check.py | 16 +- .../tests/test_notify_changed_employments.py | 9 +- .../tests/test_notify_reviewers_unverified.py | 82 ++++--- .../test_notify_supervisors_shorttime.py | 27 ++- timed/projects/tests/test_billing_type.py | 10 +- timed/projects/tests/test_cost_center.py | 12 +- timed/projects/tests/test_customer.py | 40 ++-- .../projects/tests/test_customer_assignee.py | 13 +- timed/projects/tests/test_project.py | 73 +++--- timed/projects/tests/test_project_assignee.py | 14 +- timed/projects/tests/test_task.py | 54 +++-- timed/projects/tests/test_task_assignee.py | 14 +- .../reports/tests/test_customer_statistic.py | 58 +++-- timed/reports/tests/test_month_statistic.py | 17 +- timed/reports/tests/test_project_statistic.py | 50 ++-- timed/reports/tests/test_task_statistic.py | 44 ++-- timed/reports/tests/test_user_statistic.py | 17 +- timed/reports/tests/test_work_report.py | 66 ++++-- timed/reports/tests/test_year_statistic.py | 32 ++- timed/subscription/tests/test_order.py | 67 ++++-- timed/subscription/tests/test_package.py | 17 +- .../tests/test_subscription_project.py | 91 +++++--- timed/tests/test_authentication.py | 5 +- timed/tracking/tests/test_absence.py | 146 ++++++------ timed/tracking/tests/test_activity.py | 83 ++++--- timed/tracking/tests/test_attendance.py | 20 +- timed/tracking/tests/test_report.py | 219 ++++++++++-------- 37 files changed, 1068 insertions(+), 811 deletions(-) diff --git a/timed/conftest.py b/timed/conftest.py index 526996a0..7e6bc899 100644 --- a/timed/conftest.py +++ b/timed/conftest.py @@ -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 @@ -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): @@ -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 diff --git a/timed/employment/tests/test_absence_balance.py b/timed/employment/tests/test_absence_balance.py index 2d72b956..10a16baa 100644 --- a/timed/employment/tests/test_absence_balance.py +++ b/timed/employment/tests/test_absence_balance.py @@ -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") @@ -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): @@ -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"], @@ -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"} @@ -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", diff --git a/timed/employment/tests/test_absence_credit.py b/timed/employment/tests/test_absence_credit.py index b494f7c2..01028610 100644 --- a/timed/employment/tests/test_absence_credit.py +++ b/timed/employment/tests/test_absence_credit.py @@ -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") @@ -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 = { @@ -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) @@ -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) @@ -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) diff --git a/timed/employment/tests/test_absence_type.py b/timed/employment/tests/test_absence_type.py index 440fa891..c529b10a 100644 --- a/timed/employment/tests/test_absence_type.py +++ b/timed/employment/tests/test_absence_type.py @@ -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"), @@ -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, @@ -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) @@ -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") @@ -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]) @@ -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) diff --git a/timed/employment/tests/test_employment.py b/timed/employment/tests/test_employment.py index 06d24e92..87bd293d 100644 --- a/timed/employment/tests/test_employment.py +++ b/timed/employment/tests/test_employment.py @@ -6,11 +6,8 @@ from django.urls import reverse from rest_framework import status -from timed.employment import factories from timed.employment.admin import EmploymentForm -from timed.employment.factories import EmploymentFactory, LocationFactory, UserFactory from timed.employment.models import Employment -from timed.tracking.factories import ReportFactory def test_employment_create_authenticated(auth_client): @@ -20,9 +17,8 @@ def test_employment_create_authenticated(auth_client): assert result.status_code == status.HTTP_403_FORBIDDEN -def test_employment_create_superuser(superadmin_client): +def test_employment_create_superuser(superadmin_client, location): url = reverse("employment-list") - location = LocationFactory.create() data = { "data": { @@ -44,8 +40,8 @@ def test_employment_create_superuser(superadmin_client): assert result.status_code == status.HTTP_201_CREATED -def test_employment_update_end_before_start(superadmin_client): - employment = EmploymentFactory.create(user=superadmin_client.user) +def test_employment_update_end_before_start(superadmin_client, employment_factory): + employment = employment_factory.create(user=superadmin_client.user) data = { "data": { @@ -60,10 +56,10 @@ def test_employment_update_end_before_start(superadmin_client): assert result.status_code == status.HTTP_400_BAD_REQUEST -def test_employment_update_overlapping(superadmin_client): +def test_employment_update_overlapping(superadmin_client, employment_factory): user = superadmin_client.user - EmploymentFactory.create(user=user, end_date=None) - employment = EmploymentFactory.create(user=user) + employment_factory.create(user=user, end_date=None) + employment = employment_factory.create(user=user) data = { "data": { @@ -78,9 +74,9 @@ def test_employment_update_overlapping(superadmin_client): assert result.status_code == status.HTTP_400_BAD_REQUEST -def test_employment_list_authenticated(auth_client): - EmploymentFactory.create_batch(2) - employment = EmploymentFactory.create(user=auth_client.user) +def test_employment_list_authenticated(auth_client, employment_factory): + employment_factory.create_batch(2) + employment = employment_factory.create(user=auth_client.user) url = reverse("employment-list") @@ -91,9 +87,9 @@ def test_employment_list_authenticated(auth_client): assert json["data"][0]["id"] == str(employment.id) -def test_employment_list_superuser(superadmin_client): - EmploymentFactory.create_batch(2) - EmploymentFactory.create(user=superadmin_client.user) +def test_employment_list_superuser(superadmin_client, employment_factory): + employment_factory.create_batch(2) + employment_factory.create(user=superadmin_client.user) url = reverse("employment-list") @@ -103,11 +99,11 @@ def test_employment_list_superuser(superadmin_client): assert len(json["data"]) == 3 -def test_employment_list_filter_date(auth_client): - EmploymentFactory.create( +def test_employment_list_filter_date(auth_client, employment_factory): + employment_factory.create( user=auth_client.user, start_date=date(2017, 1, 1), end_date=date(2017, 4, 1) ) - employment = EmploymentFactory.create( + employment = employment_factory.create( user=auth_client.user, start_date=date(2017, 4, 2), end_date=None ) @@ -120,13 +116,12 @@ def test_employment_list_filter_date(auth_client): assert json["data"][0]["id"] == str(employment.id) -def test_employment_list_supervisor(auth_client): - user = UserFactory.create() +def test_employment_list_supervisor(auth_client, user, employment_factory): auth_client.user.supervisees.add(user) - EmploymentFactory.create_batch(1) - EmploymentFactory.create(user=auth_client.user) - EmploymentFactory.create(user=user) + employment_factory.create_batch(1) + employment_factory.create(user=auth_client.user) + employment_factory.create(user=user) url = reverse("employment-list") @@ -137,11 +132,10 @@ def test_employment_list_supervisor(auth_client): @pytest.mark.django_db() -def test_employment_unique_active(): +def test_employment_unique_active(user, employment_factory): """Should only be able to have one active employment per user.""" - user = UserFactory.create() - EmploymentFactory.create(user=user, end_date=None) - employment = EmploymentFactory.create(user=user) + employment_factory.create(user=user, end_date=None) + employment = employment_factory.create(user=user) form = EmploymentForm({"end_date": None}, instance=employment) with pytest.raises(ValueError): # noqa: PT011 @@ -149,8 +143,7 @@ def test_employment_unique_active(): @pytest.mark.django_db() -def test_employment_start_before_end(): - employment = EmploymentFactory.create() +def test_employment_start_before_end(employment): form = EmploymentForm( {"start_date": date(2009, 1, 1), "end_date": date(2016, 1, 1)}, instance=employment, @@ -161,10 +154,9 @@ def test_employment_start_before_end(): @pytest.mark.django_db() -def test_employment_get_at(): +def test_employment_get_at(user, employment_factory): """Should return the right employment on a date.""" - user = UserFactory.create() - employment = EmploymentFactory.create(user=user) + employment = employment_factory(user=user) assert Employment.objects.get_at(user, employment.start_date) == employment @@ -177,14 +169,19 @@ def test_employment_get_at(): @pytest.mark.django_db() -def test_worktime_balance_partial(): +def test_worktime_balance_partial( + overtime_credit_factory, + public_holiday_factory, + employment_factory, + report_factory, +): """ Test partial calculation of worktime balance. Partial is defined as a worktime balance of a time frame which is shorter than employment. """ - employment = factories.EmploymentFactory.create( + employment = employment_factory( start_date=date(2010, 1, 1), end_date=None, worktime_per_day=timedelta(hours=8) ) user = employment.user @@ -194,22 +191,20 @@ def test_worktime_balance_partial(): end = date(2017, 3, 26) # Overtime credit of 10.5 hours - factories.OvertimeCreditFactory.create( + overtime_credit_factory( user=user, date=start, duration=timedelta(hours=10, minutes=30) ) # One public holiday during workdays - factories.PublicHolidayFactory.create(date=start, location=employment.location) + public_holiday_factory(date=start, location=employment.location) # One public holiday on weekend - factories.PublicHolidayFactory.create( - date=start + timedelta(days=1), location=employment.location - ) + public_holiday_factory(date=start + timedelta(days=1), location=employment.location) # 5 workdays minus one holiday (32 hours) expected_expected = timedelta(hours=32) # reported 2 days each 10 hours for day in range(3, 5): - ReportFactory.create( + report_factory( user=user, date=start + timedelta(days=day), duration=timedelta(hours=10) ) # 10 hours reported time + 10.5 overtime credit @@ -224,9 +219,11 @@ def test_worktime_balance_partial(): @pytest.mark.django_db() -def test_worktime_balance_longer(): +def test_worktime_balance_longer( + employment_factory, overtime_credit_factory, public_holiday_factory, report_factory +): """Test calculation of worktime when frame is longer than employment.""" - employment = factories.EmploymentFactory.create( + employment = employment_factory( start_date=date(2017, 3, 21), end_date=date(2017, 3, 27), worktime_per_day=timedelta(hours=8), @@ -238,34 +235,30 @@ def test_worktime_balance_longer(): end = date(2017, 12, 31) # Overtime credit of 10.5 hours before employment - factories.OvertimeCreditFactory.create( + overtime_credit_factory( user=user, date=start, duration=timedelta(hours=10, minutes=30) ) # Overtime credit of during employment - factories.OvertimeCreditFactory.create( + overtime_credit_factory( user=user, date=employment.start_date, duration=timedelta(hours=10, minutes=30) ) # One public holiday during employment - factories.PublicHolidayFactory.create( - date=employment.start_date, location=employment.location - ) + public_holiday_factory(date=employment.start_date, location=employment.location) # One public holiday before employment started - factories.PublicHolidayFactory.create( - date=date(2017, 3, 20), location=employment.location - ) + public_holiday_factory(date=date(2017, 3, 20), location=employment.location) # 5 workdays minus one holiday (32 hours) expected_expected = timedelta(hours=32) # reported 2 days each 10 hours for day in range(3, 5): - ReportFactory.create( + report_factory( user=user, date=employment.start_date + timedelta(days=day), duration=timedelta(hours=10), ) # reported time not on current employment - ReportFactory.create(user=user, date=date(2017, 1, 5), duration=timedelta(hours=10)) + report_factory(user=user, date=date(2017, 1, 5), duration=timedelta(hours=10)) # 10 hours reported time + 10.5 overtime credit expected_reported = timedelta(hours=30, minutes=30) expected_balance = expected_reported - expected_expected @@ -278,24 +271,21 @@ def test_worktime_balance_longer(): @pytest.mark.django_db() -def test_employment_for_user(): - user = factories.UserFactory.create() +def test_employment_for_user(user, employment_factory): # employment overlapping time frame (early start) - factories.EmploymentFactory.create( + employment_factory.create( start_date=date(2017, 1, 1), end_date=date(2017, 2, 28), user=user ) # employment overlapping time frame (early end) - factories.EmploymentFactory.create( + employment_factory.create( start_date=date(2017, 3, 1), end_date=date(2017, 3, 31), user=user ) # employment within time frame - factories.EmploymentFactory.create( + employment_factory.create( start_date=date(2017, 4, 1), end_date=date(2017, 4, 30), user=user ) # employment without end date - factories.EmploymentFactory.create( - start_date=date(2017, 5, 1), end_date=None, user=user - ) + employment_factory.create(start_date=date(2017, 5, 1), end_date=None, user=user) employments = Employment.objects.for_user(user, date(2017, 2, 1), date(2017, 12, 1)) diff --git a/timed/employment/tests/test_location.py b/timed/employment/tests/test_location.py index ec0e8cca..a09cfdac 100644 --- a/timed/employment/tests/test_location.py +++ b/timed/employment/tests/test_location.py @@ -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 EmploymentFactory, LocationFactory - @pytest.mark.django_db() @pytest.mark.usefixtures("location") @@ -19,7 +16,12 @@ ], ) def test_location_list( - auth_client, is_employed, is_customer_assignee, is_customer, expected + auth_client, + is_employed, + is_customer_assignee, + is_customer, + expected, + setup_customer_and_employment_status, ): setup_customer_and_employment_status( user=auth_client.user, @@ -46,10 +48,11 @@ def test_location_list( (False, status.HTTP_404_NOT_FOUND), ], ) -def test_location_detail(auth_client, is_employed, expected): - location = LocationFactory.create() +def test_location_detail( + auth_client, is_employed, expected, location, employment_factory +): if is_employed: - EmploymentFactory.create(user=auth_client.user) + employment_factory.create(user=auth_client.user) url = reverse("location-detail", args=[location.id]) @@ -64,18 +67,14 @@ def test_location_create(auth_client): assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED -def test_location_update(auth_client): - location = LocationFactory.create() - +def test_location_update(auth_client, location): url = reverse("location-detail", args=[location.id]) response = auth_client.patch(url) assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED -def test_location_delete(auth_client): - location = LocationFactory.create() - +def test_location_delete(auth_client, location): url = reverse("location-detail", args=[location.id]) response = auth_client.delete(url) diff --git a/timed/employment/tests/test_overtime_credit.py b/timed/employment/tests/test_overtime_credit.py index f5ec531b..8adb1bed 100644 --- a/timed/employment/tests/test_overtime_credit.py +++ b/timed/employment/tests/test_overtime_credit.py @@ -3,8 +3,6 @@ from django.urls import reverse from rest_framework import status -from timed.employment.factories import OvertimeCreditFactory, UserFactory - def test_overtime_credit_create_authenticated(auth_client): url = reverse("overtime-credit-list") @@ -31,9 +29,9 @@ def test_overtime_credit_create_superuser(superadmin_client): assert result.status_code == status.HTTP_201_CREATED -def test_overtime_credit_get_authenticated(auth_client): - OvertimeCreditFactory.create_batch(2) - overtime_credit = OvertimeCreditFactory.create(user=auth_client.user) +def test_overtime_credit_get_authenticated(auth_client, overtime_credit_factory): + overtime_credit_factory.create_batch(2) + overtime_credit = overtime_credit_factory(user=auth_client.user) url = reverse("overtime-credit-list") result = auth_client.get(url) @@ -43,9 +41,9 @@ def test_overtime_credit_get_authenticated(auth_client): assert json["data"][0]["id"] == str(overtime_credit.id) -def test_overtime_credit_get_superuser(superadmin_client): - OvertimeCreditFactory.create_batch(2) - OvertimeCreditFactory.create(user=superadmin_client.user) +def test_overtime_credit_get_superuser(superadmin_client, overtime_credit_factory): + overtime_credit_factory.create_batch(2) + overtime_credit_factory(user=superadmin_client.user) url = reverse("overtime-credit-list") result = superadmin_client.get(url) @@ -54,13 +52,12 @@ def test_overtime_credit_get_superuser(superadmin_client): assert len(json["data"]) == 3 -def test_overtime_credit_get_supervisor(auth_client): - user = UserFactory.create() +def test_overtime_credit_get_supervisor(auth_client, user, overtime_credit_factory): auth_client.user.supervisees.add(user) - OvertimeCreditFactory.create_batch(1) - OvertimeCreditFactory.create(user=auth_client.user) - OvertimeCreditFactory.create(user=user) + overtime_credit_factory() + overtime_credit_factory(user=auth_client.user) + overtime_credit_factory(user=user) url = reverse("overtime-credit-list") result = auth_client.get(url) diff --git a/timed/employment/tests/test_public_holiday.py b/timed/employment/tests/test_public_holiday.py index 4a19dd85..f59f4d12 100644 --- a/timed/employment/tests/test_public_holiday.py +++ b/timed/employment/tests/test_public_holiday.py @@ -4,10 +4,8 @@ from django.urls import reverse from rest_framework import status -from timed.conftest import setup_customer_and_employment_status -from timed.employment.factories import EmploymentFactory, PublicHolidayFactory - +@pytest.mark.usefixtures("public_holiday_factory") @pytest.mark.parametrize( ("is_employed", "is_customer_assignee", "is_customer", "expected"), [ @@ -19,7 +17,12 @@ ], ) def test_public_holiday_list( - auth_client, is_employed, is_customer_assignee, is_customer, expected + auth_client, + is_employed, + is_customer_assignee, + is_customer, + expected, + setup_customer_and_employment_status, ): setup_customer_and_employment_status( user=auth_client.user, @@ -28,7 +31,6 @@ def test_public_holiday_list( is_employed=is_employed, is_external=False, ) - PublicHolidayFactory.create() url = reverse("public-holiday-list") response = auth_client.get(url) @@ -45,10 +47,11 @@ def test_public_holiday_list( (False, status.HTTP_404_NOT_FOUND), ], ) -def test_public_holiday_detail(auth_client, is_employed, expected): - public_holiday = PublicHolidayFactory.create() +def test_public_holiday_detail( + auth_client, is_employed, expected, public_holiday, employment_factory +): if is_employed: - EmploymentFactory.create(user=auth_client.user) + employment_factory.create(user=auth_client.user) url = reverse("public-holiday-detail", args=[public_holiday.id]) @@ -63,27 +66,23 @@ def test_public_holiday_create(auth_client): assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED -def test_public_holiday_update(auth_client): - public_holiday = PublicHolidayFactory.create() - +def test_public_holiday_update(auth_client, public_holiday): url = reverse("public-holiday-detail", args=[public_holiday.id]) response = auth_client.patch(url) assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED -def test_public_holiday_delete(auth_client): - public_holiday = PublicHolidayFactory.create() - +def test_public_holiday_delete(auth_client, public_holiday): url = reverse("public-holiday-detail", args=[public_holiday.id]) response = auth_client.delete(url) assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED -def test_public_holiday_year_filter(internal_employee_client): - PublicHolidayFactory.create(date=date(2017, 1, 1)) - public_holiday = PublicHolidayFactory.create(date=date(2018, 1, 1)) +def test_public_holiday_year_filter(internal_employee_client, public_holiday_factory): + public_holiday_factory.create(date=date(2017, 1, 1)) + public_holiday = public_holiday_factory.create(date=date(2018, 1, 1)) url = reverse("public-holiday-list") diff --git a/timed/employment/tests/test_user.py b/timed/employment/tests/test_user.py index 4ecb970e..ea1b3e96 100644 --- a/timed/employment/tests/test_user.py +++ b/timed/employment/tests/test_user.py @@ -4,18 +4,6 @@ from django.urls import reverse from rest_framework import status -from timed.employment.factories import ( - AbsenceTypeFactory, - EmploymentFactory, - UserFactory, -) -from timed.projects.factories import ( - CustomerAssigneeFactory, - ProjectAssigneeFactory, - ProjectFactory, -) -from timed.tracking.factories import AbsenceFactory, ReportFactory - def test_user_list_unauthenticated(client): url = reverse("user-list") @@ -24,16 +12,15 @@ def test_user_list_unauthenticated(client): @pytest.mark.django_db() -def test_user_update_unauthenticated(client): - user = UserFactory.create() +def test_user_update_unauthenticated(client, user): url = reverse("user-detail", args=[user.id]) response = client.patch(url) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.django_db() -def test_user_list(internal_employee_client, django_assert_num_queries): - UserFactory.create_batch(2) +def test_user_list(internal_employee_client, django_assert_num_queries, user_factory): + user_factory.create_batch(2) url = reverse("user-list") @@ -46,8 +33,8 @@ def test_user_list(internal_employee_client, django_assert_num_queries): assert len(json["data"]) == 3 -def test_user_list_external_employee(external_employee_client): - UserFactory.create_batch(2) +def test_user_list_external_employee(external_employee_client, user_factory): + user_factory.create_batch(2) url = reverse("user-list") @@ -116,9 +103,8 @@ def test_user_update_owner(internal_employee_client): assert not user.is_staff -def test_user_update_other(internal_employee_client): +def test_user_update_other(internal_employee_client, user): """User may not change other user.""" - user = UserFactory.create() url = reverse("user-detail", args=[user.id]) res = internal_employee_client.patch(url) @@ -135,10 +121,9 @@ def test_user_delete_authenticated(internal_employee_client): assert response.status_code == status.HTTP_403_FORBIDDEN -def test_user_delete_superuser(superadmin_client): +def test_user_delete_superuser(superadmin_client, user, employment_factory): """Should not be able delete a user.""" - user = UserFactory.create() - EmploymentFactory.create(user=superadmin_client.user) + employment_factory.create(user=superadmin_client.user) url = reverse("user-detail", args=[user.id]) @@ -147,11 +132,12 @@ def test_user_delete_superuser(superadmin_client): @pytest.mark.django_db() -def test_user_delete_with_reports_superuser(superadmin_client): +def test_user_delete_with_reports_superuser( + superadmin_client, user, report_factory, employment_factory +): """Test that user with reports may not be deleted.""" - user = UserFactory.create() - ReportFactory.create(user=user) - EmploymentFactory.create(user=superadmin_client.user) + report_factory.create(user=user) + employment_factory.create(user=superadmin_client.user) url = reverse("user-detail", args=[user.id]) @@ -159,11 +145,11 @@ def test_user_delete_with_reports_superuser(superadmin_client): assert response.status_code == status.HTTP_403_FORBIDDEN -def test_user_supervisor_filter(internal_employee_client): +def test_user_supervisor_filter(internal_employee_client, user_factory): """Should filter users by supervisor.""" - supervisees = UserFactory.create_batch(5) + supervisees = user_factory.create_batch(5) - UserFactory.create_batch(5) + user_factory.create_batch(5) internal_employee_client.user.supervisees.add(*supervisees) internal_employee_client.user.save() @@ -176,14 +162,17 @@ def test_user_supervisor_filter(internal_employee_client): @pytest.mark.freeze_time("2018-01-07") -def test_user_transfer(superadmin_client): - user = UserFactory.create() - EmploymentFactory.create(user=superadmin_client.user) - EmploymentFactory.create(user=user, start_date=date(2017, 12, 28), percentage=100) - AbsenceTypeFactory.create(fill_worktime=True) - AbsenceTypeFactory.create(fill_worktime=False) - absence_type = AbsenceTypeFactory.create(fill_worktime=False) - AbsenceFactory.create(user=user, absence_type=absence_type, date=date(2017, 12, 29)) +def test_user_transfer( + superadmin_client, user, employment_factory, absence_type_factory, absence_factory +): + employment_factory.create(user=superadmin_client.user) + employment_factory.create(user=user, start_date=date(2017, 12, 28), percentage=100) + absence_type_factory.create(fill_worktime=True) + absence_type_factory.create(fill_worktime=False) + absence_type = absence_type_factory.create(fill_worktime=False) + absence_factory.create( + user=user, absence_type=absence_type, date=date(2017, 12, 29) + ) url = reverse("user-transfer", args=[user.id]) response = superadmin_client.post(url) @@ -209,13 +198,14 @@ def test_user_transfer(superadmin_client): @pytest.mark.parametrize(("value", "expected"), [(1, 2), (0, 2)]) -def test_user_is_external_filter(internal_employee_client, value, expected): +def test_user_is_external_filter( + internal_employee_client, value, expected, user, user_factory, employment_factory +): """Should filter users if they have an internal employment.""" - user = UserFactory.create() - user2, user3 = UserFactory.create_batch(2) - EmploymentFactory.create(is_external=False, user=user) - EmploymentFactory.create(is_external=True, user=user2) - EmploymentFactory.create(is_external=True, user=user3) + user2, user3 = user_factory.create_batch(2) + employment_factory.create(is_external=False, user=user) + employment_factory.create(is_external=True, user=user2) + employment_factory.create(is_external=True, user=user3) response = internal_employee_client.get( reverse("user-list"), {"is_external": value} @@ -224,22 +214,30 @@ def test_user_is_external_filter(internal_employee_client, value, expected): @pytest.mark.parametrize(("value", "expected"), [(1, 1), (0, 4)]) -def test_user_is_reviewer_filter(internal_employee_client, value, expected): +def test_user_is_reviewer_filter( + internal_employee_client, + value, + expected, + user, + project, + user_factory, + project_assignee_factory, +): """Should filter users if they are a reviewer.""" - user = UserFactory.create() - project = ProjectFactory.create() - UserFactory.create_batch(3) - ProjectAssigneeFactory.create(user=user, project=project, is_reviewer=True) + user_factory.create_batch(3) + project_assignee_factory.create(user=user, project=project, is_reviewer=True) res = internal_employee_client.get(reverse("user-list"), {"is_reviewer": value}) assert len(res.json()["data"]) == expected @pytest.mark.parametrize(("value", "expected"), [(1, 1), (0, 5)]) -def test_user_is_supervisor_filter(internal_employee_client, value, expected): +def test_user_is_supervisor_filter( + internal_employee_client, value, expected, user_factory +): """Should filter useres if they are a supervisor.""" - users = UserFactory.create_batch(2) - UserFactory.create_batch(3) + users = user_factory.create_batch(2) + user_factory.create_batch(3) internal_employee_client.user.supervisees.add(*users) @@ -247,16 +245,20 @@ def test_user_is_supervisor_filter(internal_employee_client, value, expected): assert len(res.json()["data"]) == expected -def test_user_attributes(internal_employee_client, project): +def test_user_attributes( + internal_employee_client, + project, + user, + project_assignee_factory, +): """Should filter users if they are a reviewer.""" - user = UserFactory.create() url = reverse("user-detail", args=[user.id]) res = internal_employee_client.get(url) assert not res.json()["data"]["attributes"]["is-reviewer"] - ProjectAssigneeFactory.create(user=user, project=project, is_reviewer=True) + project_assignee_factory(user=user, project=project, is_reviewer=True) res = internal_employee_client.get(url) assert res.json()["data"]["attributes"]["is-reviewer"] @@ -292,11 +294,18 @@ def test_user_me_anonymous(client): ("is_customer", "expected", "status_code"), [(True, 1, status.HTTP_200_OK), (False, 0, status.HTTP_403_FORBIDDEN)], ) -def test_user_list_no_employment(auth_client, is_customer, expected, status_code): +def test_user_list_no_employment( + auth_client, + is_customer, + expected, + status_code, + user_factory, + customer_assignee_factory, +): user = auth_client.user - UserFactory.create_batch(2) + user_factory.create_batch(2) if is_customer: - CustomerAssigneeFactory.create(user=user, is_customer=True) + customer_assignee_factory.create(user=user, is_customer=True) url = reverse("user-list") diff --git a/timed/employment/tests/test_worktime_balance.py b/timed/employment/tests/test_worktime_balance.py index 5f055433..77e151e0 100644 --- a/timed/employment/tests/test_worktime_balance.py +++ b/timed/employment/tests/test_worktime_balance.py @@ -5,14 +5,6 @@ from django.utils.duration import duration_string from rest_framework import status -from timed.employment.factories import ( - EmploymentFactory, - OvertimeCreditFactory, - PublicHolidayFactory, - UserFactory, -) -from timed.tracking.factories import AbsenceFactory, ReportFactory - def test_worktime_balance_create(auth_client): url = reverse("worktime-balance-list") @@ -38,18 +30,26 @@ def test_worktime_balance_no_employment(auth_client, django_assert_num_queries): assert data["attributes"]["balance"] == "00:00:00" -def test_worktime_balance_with_employments(auth_client, django_assert_num_queries): +def test_worktime_balance_with_employments( + auth_client, + django_assert_num_queries, + employment_factory, + overtime_credit_factory, + public_holiday_factory, + report_factory, + absence_factory, +): # Calculate over one week start_date = date(2017, 3, 19) end_date = date(2017, 3, 26) - employment = EmploymentFactory.create( + employment = employment_factory( user=auth_client.user, start_date=start_date, worktime_per_day=timedelta(hours=8, minutes=30), end_date=date(2017, 3, 23), ) - EmploymentFactory.create( + employment_factory( user=auth_client.user, start_date=date(2017, 3, 24), worktime_per_day=timedelta(hours=8), @@ -57,32 +57,32 @@ def test_worktime_balance_with_employments(auth_client, django_assert_num_querie ) # Overtime credit of 10 hours - OvertimeCreditFactory.create( + overtime_credit_factory.create( user=auth_client.user, date=start_date, duration=timedelta(hours=10, minutes=30) ) # One public holiday during workdays - PublicHolidayFactory.create(date=start_date, location=employment.location) + public_holiday_factory.create(date=start_date, location=employment.location) # One public holiday on weekend - PublicHolidayFactory.create( + public_holiday_factory.create( date=start_date + timedelta(days=1), location=employment.location ) # 2x 10 hour reported worktime - ReportFactory.create( + report_factory.create( user=auth_client.user, date=start_date + timedelta(days=3), duration=timedelta(hours=10), ) - ReportFactory.create( + report_factory.create( user=auth_client.user, date=start_date + timedelta(days=4), duration=timedelta(hours=10), ) # one absence - AbsenceFactory.create(user=auth_client.user, date=start_date + timedelta(days=5)) + absence_factory.create(user=auth_client.user, date=start_date + timedelta(days=5)) url = reverse( "worktime-balance-detail", @@ -127,11 +127,11 @@ def test_worktime_balance_invalid_date(auth_client): assert result.status_code == status.HTTP_400_BAD_REQUEST -def test_user_worktime_list_superuser(auth_client): +def test_user_worktime_list_superuser(auth_client, user_factory): auth_client.user.is_superuser = True auth_client.user.save() - supervisee = UserFactory.create() - UserFactory.create() + supervisee = user_factory.create() + user_factory.create() auth_client.user.supervisees.add(supervisee) url = reverse("worktime-balance-list") @@ -144,9 +144,9 @@ def test_user_worktime_list_superuser(auth_client): assert len(json["data"]) == 3 -def test_worktime_balance_list_supervisor(auth_client): - supervisee = UserFactory.create() - UserFactory.create() +def test_worktime_balance_list_supervisor(auth_client, user_factory): + supervisee = user_factory.create() + user_factory.create() auth_client.user.supervisees.add(supervisee) url = reverse("worktime-balance-list") @@ -159,9 +159,9 @@ def test_worktime_balance_list_supervisor(auth_client): assert len(json["data"]) == 2 -def test_worktime_balance_list_filter_user(auth_client): - supervisee = UserFactory.create() - UserFactory.create() +def test_worktime_balance_list_filter_user(auth_client, user_factory): + supervisee = user_factory.create() + user_factory.create() auth_client.user.supervisees.add(supervisee) url = reverse("worktime-balance-list") @@ -190,24 +190,24 @@ def test_worktime_balance_list_last_reported_date_no_reports( @pytest.mark.freeze_time("2017-02-02") def test_worktime_balance_list_last_reported_date( - auth_client, django_assert_num_queries + auth_client, django_assert_num_queries, employment_factory, report_factory ): - EmploymentFactory.create( + employment_factory( user=auth_client.user, start_date=date(2017, 2, 1), end_date=date(2017, 2, 2), worktime_per_day=timedelta(hours=8), ) - ReportFactory.create( + report_factory( user=auth_client.user, date=date(2017, 2, 1), duration=timedelta(hours=10) ) # reports today and in the future should be ignored - ReportFactory.create( + report_factory( user=auth_client.user, date=date(2017, 2, 2), duration=timedelta(hours=10) ) - ReportFactory.create( + report_factory( user=auth_client.user, date=date(2017, 2, 3), duration=timedelta(hours=10) ) diff --git a/timed/notifications/tests/test_budget_check.py b/timed/notifications/tests/test_budget_check.py index d1dc1082..eab7e7cb 100644 --- a/timed/notifications/tests/test_budget_check.py +++ b/timed/notifications/tests/test_budget_check.py @@ -5,7 +5,6 @@ from django.utils.timezone import now from redminelib.exceptions import ResourceNotFoundError -from timed.notifications.factories import NotificationFactory from timed.notifications.models import Notification from timed.redmine.models import RedmineProject @@ -16,7 +15,12 @@ [(1, 0, 0), (3, 0, 0), (4, 30, 1), (8, 70, 2), (0, 0, 0)], ) def test_budget_check_1( - mocker, report_factory, duration, percentage_exceeded, notification_count + mocker, + report_factory, + duration, + percentage_exceeded, + notification_count, + notification_factory, ): """Test budget check.""" redmine_instance = mocker.MagicMock() @@ -36,7 +40,7 @@ def test_budget_check_1( report.delete() if percentage_exceeded == 70: - NotificationFactory( + notification_factory( project=project, notification_type=Notification.BUDGET_CHECK_30 ) @@ -57,7 +61,9 @@ def test_budget_check_1( @pytest.mark.django_db() -def test_budget_check_skip_notification(capsys, mocker, report_factory): +def test_budget_check_skip_notification( + capsys, mocker, report_factory, notification_factory +): redmine_instance = mocker.MagicMock() issue = mocker.MagicMock() redmine_instance.issue.get.return_value = issue @@ -71,7 +77,7 @@ def test_budget_check_skip_notification(capsys, mocker, report_factory): project.cost_center.name = "DEV_BUILD" project.cost_center.save() - notification = NotificationFactory( + notification = notification_factory( project=project, notification_type=Notification.BUDGET_CHECK_30, sent_at=now() ) diff --git a/timed/notifications/tests/test_notify_changed_employments.py b/timed/notifications/tests/test_notify_changed_employments.py index 9837bd62..598c3d76 100644 --- a/timed/notifications/tests/test_notify_changed_employments.py +++ b/timed/notifications/tests/test_notify_changed_employments.py @@ -3,22 +3,21 @@ import pytest from django.core.management import call_command -from timed.employment.factories import EmploymentFactory from timed.notifications.models import Notification @pytest.mark.django_db() -def test_notify_changed_employments(mailoutbox, freezer): +def test_notify_changed_employments(mailoutbox, freezer, employment_factory): email = "test@example.net" # employments changed too far in the past freezer.move_to("2017-08-27") - EmploymentFactory.create_batch(2) + employment_factory.create_batch(2) # employments which should show up in report freezer.move_to("2017-09-03") - finished = EmploymentFactory.create(end_date=date(2017, 10, 10), percentage=80) - new = EmploymentFactory.create(percentage=100) + finished = employment_factory(end_date=date(2017, 10, 10), percentage=80) + new = employment_factory(percentage=100) freezer.move_to("2017-09-04") call_command("notify_changed_employments", email=email) diff --git a/timed/notifications/tests/test_notify_reviewers_unverified.py b/timed/notifications/tests/test_notify_reviewers_unverified.py index 9a42c0ab..628a609d 100644 --- a/timed/notifications/tests/test_notify_reviewers_unverified.py +++ b/timed/notifications/tests/test_notify_reviewers_unverified.py @@ -3,15 +3,7 @@ import pytest from django.core.management import call_command -from timed.employment.factories import UserFactory from timed.notifications.models import Notification -from timed.projects.factories import ( - ProjectAssigneeFactory, - ProjectFactory, - TaskAssigneeFactory, - TaskFactory, -) -from timed.tracking.factories import ReportFactory @pytest.mark.django_db() @@ -25,25 +17,34 @@ ("", "This is a test"), ], ) -def test_notify_reviewers_with_cc_and_message(mailoutbox, cc, message): +def test_notify_reviewers_with_cc_and_message( + mailoutbox, + cc, + message, + user_factory, + project_factory, + task_factory, + report_factory, + project_assignee_factory, +): """Test time range 2017-7-1 till 2017-7-31.""" # a reviewer which will be notified - reviewer_work = UserFactory.create() - project_work = ProjectFactory.create() - ProjectAssigneeFactory.create( + reviewer_work = user_factory.create() + project_work = project_factory.create() + project_assignee_factory.create( user=reviewer_work, project=project_work, is_reviewer=True ) - task_work = TaskFactory.create(project=project_work) - ReportFactory.create(date=date(2017, 7, 1), task=task_work, verified_by=None) + task_work = task_factory.create(project=project_work) + report_factory.create(date=date(2017, 7, 1), task=task_work, verified_by=None) # a reviewer which doesn't have any unverfied reports - reviewer_no_work = UserFactory.create() - project_no_work = ProjectFactory.create() - ProjectAssigneeFactory.create( + reviewer_no_work = user_factory.create() + project_no_work = project_factory.create() + project_assignee_factory.create( user=reviewer_no_work, project=project_no_work, is_reviewer=True ) - task_no_work = TaskFactory.create(project=project_no_work) - ReportFactory.create( + task_no_work = task_factory.create(project=project_no_work) + report_factory.create( date=date(2017, 7, 1), task=task_no_work, verified_by=reviewer_no_work ) @@ -68,16 +69,23 @@ def test_notify_reviewers_with_cc_and_message(mailoutbox, cc, message): @pytest.mark.django_db() @pytest.mark.freeze_time("2017-8-4") -def test_notify_reviewers(mailoutbox): +def test_notify_reviewers( + mailoutbox, + user_factory, + project_factory, + task_factory, + project_assignee_factory, + report_factory, +): """Test time range 2017-7-1 till 2017-7-31.""" # a reviewer which will be notified - reviewer_work = UserFactory.create() - project_work = ProjectFactory.create() - ProjectAssigneeFactory.create( + reviewer_work = user_factory.create() + project_work = project_factory.create() + project_assignee_factory.create( user=reviewer_work, project=project_work, is_reviewer=True ) - task_work = TaskFactory.create(project=project_work) - ReportFactory.create(date=date(2017, 7, 1), task=task_work, verified_by=None) + task_work = task_factory.create(project=project_work) + report_factory.create(date=date(2017, 7, 1), task=task_work, verified_by=None) call_command("notify_reviewers_unverified") @@ -95,23 +103,31 @@ def test_notify_reviewers(mailoutbox): @pytest.mark.django_db() @pytest.mark.freeze_time("2017-8-4") -def test_notify_reviewers_reviewer_hierarchy(mailoutbox): +def test_notify_reviewers_reviewer_hierarchy( + mailoutbox, + user_factory, + project_factory, + task_factory, + project_assignee_factory, + task_assignee_factory, + report_factory, +): """Test notification with reviewer hierarchy. Test if only the lowest in reviewer hierarchy gets notified. """ # user that shouldn't be notified - project_reviewer = UserFactory.create() + project_reviewer = user_factory.create() # user that should be notified - task_reviewer = UserFactory.create() - project = ProjectFactory.create() - task = TaskFactory.create(project=project) - ProjectAssigneeFactory.create( + task_reviewer = user_factory.create() + project = project_factory.create() + task = task_factory.create(project=project) + project_assignee_factory.create( user=project_reviewer, project=project, is_reviewer=True ) - TaskAssigneeFactory.create(user=task_reviewer, task=task, is_reviewer=True) + task_assignee_factory.create(user=task_reviewer, task=task, is_reviewer=True) - ReportFactory.create(date=date(2017, 7, 1), task=task, verified_by=None) + report_factory.create(date=date(2017, 7, 1), task=task, verified_by=None) call_command("notify_reviewers_unverified") diff --git a/timed/notifications/tests/test_notify_supervisors_shorttime.py b/timed/notifications/tests/test_notify_supervisors_shorttime.py index ecab6494..4f507f8b 100644 --- a/timed/notifications/tests/test_notify_supervisors_shorttime.py +++ b/timed/notifications/tests/test_notify_supervisors_shorttime.py @@ -4,23 +4,26 @@ from dateutil.rrule import DAILY, FR, MO, rrule from django.core.management import call_command -from timed.employment.factories import EmploymentFactory, UserFactory from timed.notifications.models import Notification -from timed.projects.factories import TaskFactory -from timed.tracking.factories import ReportFactory @pytest.mark.django_db() @pytest.mark.freeze_time("2017-7-27") -def test_notify_supervisors(mailoutbox): +def test_notify_supervisors( + mailoutbox, + user_factory, + employment_factory, + task_factory, + report_factory, +): """Test time range 2017-7-17 till 2017-7-23.""" start = date(2017, 7, 14) # supervisee with short time - supervisee = UserFactory.create() - supervisor = UserFactory.create() + supervisee = user_factory.create() + supervisor = user_factory.create() supervisee.supervisors.add(supervisor) - EmploymentFactory.create(user=supervisee, start_date=start, percentage=100) + employment_factory.create(user=supervisee, start_date=start, percentage=100) workdays = rrule( DAILY, dtstart=start, @@ -28,9 +31,9 @@ def test_notify_supervisors(mailoutbox): # range is excluding last byweekday=range(MO.weekday, FR.weekday + 1), ) - task = TaskFactory.create() + task = task_factory.create() for dt in workdays: - ReportFactory.create( + report_factory.create( user=supervisee, date=dt, task=task, duration=timedelta(hours=7) ) @@ -50,10 +53,10 @@ def test_notify_supervisors(mailoutbox): @pytest.mark.django_db() -def test_notify_supervisors_no_employment(mailoutbox): +def test_notify_supervisors_no_employment(mailoutbox, user_factory): """Check that supervisees without employment do not notify supervisor.""" - supervisee = UserFactory.create() - supervisor = UserFactory.create() + supervisee = user_factory.create() + supervisor = user_factory.create() supervisee.supervisors.add(supervisor) call_command("notify_supervisors_shorttime") diff --git a/timed/projects/tests/test_billing_type.py b/timed/projects/tests/test_billing_type.py index 2f4dd6cb..e1b86bfc 100644 --- a/timed/projects/tests/test_billing_type.py +++ b/timed/projects/tests/test_billing_type.py @@ -2,8 +2,7 @@ from django.urls import reverse from rest_framework.status import HTTP_200_OK, HTTP_403_FORBIDDEN -from timed.conftest import setup_customer_and_employment_status -from timed.projects import factories, models +from timed.projects import models @pytest.mark.parametrize( @@ -39,6 +38,9 @@ def test_billing_type_list( customer_visible, expected, status_code, + setup_customer_and_employment_status, + customer_factory, + project_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -51,8 +53,8 @@ def test_billing_type_list( if is_customer_assignee: customer = models.Customer.objects.get(customer_assignees__user=user) else: - customer = factories.CustomerFactory.create() - project = factories.ProjectFactory.create( + customer = customer_factory.create() + project = project_factory.create( customer_visible=customer_visible, customer=customer ) diff --git a/timed/projects/tests/test_cost_center.py b/timed/projects/tests/test_cost_center.py index 46c26ae9..eb2b2b39 100644 --- a/timed/projects/tests/test_cost_center.py +++ b/timed/projects/tests/test_cost_center.py @@ -2,9 +2,6 @@ from django.urls import reverse from rest_framework.status import HTTP_200_OK, HTTP_403_FORBIDDEN -from timed.conftest import setup_customer_and_employment_status -from timed.projects.factories import CostCenterFactory - @pytest.mark.parametrize( ("is_employed", "is_customer_assignee", "is_customer", "status_code"), @@ -17,10 +14,15 @@ ], ) def test_cost_center_list( - auth_client, is_employed, is_customer_assignee, is_customer, status_code + auth_client, + is_employed, + is_customer_assignee, + is_customer, + status_code, + cost_center, + setup_customer_and_employment_status, ): user = auth_client.user - cost_center = CostCenterFactory.create() setup_customer_and_employment_status( user=user, is_assignee=is_customer_assignee, diff --git a/timed/projects/tests/test_customer.py b/timed/projects/tests/test_customer.py index 07de2da1..8fa633bd 100644 --- a/timed/projects/tests/test_customer.py +++ b/timed/projects/tests/test_customer.py @@ -4,12 +4,10 @@ from django.urls import reverse from rest_framework import status -from timed.projects.factories import CustomerAssigneeFactory, CustomerFactory - -def test_customer_list_not_archived(internal_employee_client): - CustomerFactory.create(archived=True) - customer = CustomerFactory.create(archived=False) +def test_customer_list_not_archived(internal_employee_client, customer_factory): + customer_factory.create(archived=True) + customer = customer_factory.create(archived=False) url = reverse("customer-list") @@ -21,9 +19,7 @@ def test_customer_list_not_archived(internal_employee_client): assert json["data"][0]["id"] == str(customer.id) -def test_customer_detail(internal_employee_client): - customer = CustomerFactory.create() - +def test_customer_detail(internal_employee_client, customer): url = reverse("customer-detail", args=[customer.id]) response = internal_employee_client.get(url) @@ -37,18 +33,14 @@ def test_customer_create(auth_client): assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED -def test_customer_update(auth_client): - customer = CustomerFactory.create() - +def test_customer_update(auth_client, customer): url = reverse("customer-detail", args=[customer.id]) response = auth_client.patch(url) assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED -def test_customer_delete(auth_client): - customer = CustomerFactory.create() - +def test_customer_delete(auth_client, customer): url = reverse("customer-detail", args=[customer.id]) response = auth_client.delete(url) @@ -57,10 +49,10 @@ def test_customer_delete(auth_client): @pytest.mark.parametrize(("is_assigned", "expected"), [(True, 1), (False, 0)]) def test_customer_list_external_employee( - external_employee_client, is_assigned, expected + external_employee_client, is_assigned, expected, customer_factory ): - CustomerFactory.create_batch(4) - customer = CustomerFactory.create() + customer_factory.create_batch(4) + customer = customer_factory.create() if is_assigned: customer.assignees.add(external_employee_client.user) @@ -77,11 +69,17 @@ def test_customer_list_external_employee( ("is_customer", "expected"), [(True, 1), (False, 0)], ) -def test_customer_list_no_employment(auth_client, is_customer, expected): - CustomerFactory.create_batch(4) - customer = CustomerFactory.create() +def test_customer_list_no_employment( + auth_client, + is_customer, + expected, + customer_factory, + customer_assignee_factory, +): + customer_factory.create_batch(4) + customer = customer_factory.create() if is_customer: - CustomerAssigneeFactory.create( + customer_assignee_factory.create( user=auth_client.user, is_customer=True, customer=customer ) diff --git a/timed/projects/tests/test_customer_assignee.py b/timed/projects/tests/test_customer_assignee.py index 0a761999..2eca0ea1 100644 --- a/timed/projects/tests/test_customer_assignee.py +++ b/timed/projects/tests/test_customer_assignee.py @@ -2,9 +2,6 @@ from django.urls import reverse from rest_framework.status import HTTP_200_OK -from timed.conftest import setup_customer_and_employment_status -from timed.projects.factories import CustomerAssigneeFactory - @pytest.mark.parametrize( ("is_employed", "is_external", "is_customer_assignee", "is_customer", "expected"), @@ -20,9 +17,15 @@ ], ) def test_customer_assignee_list( - auth_client, is_employed, is_external, is_customer_assignee, is_customer, expected + auth_client, + is_employed, + is_external, + is_customer_assignee, + is_customer, + expected, + setup_customer_and_employment_status, + customer_assignee, ): - customer_assignee = CustomerAssigneeFactory.create() user = auth_client.user setup_customer_and_employment_status( user=user, diff --git a/timed/projects/tests/test_project.py b/timed/projects/tests/test_project.py index 955ff59f..965dac8d 100644 --- a/timed/projects/tests/test_project.py +++ b/timed/projects/tests/test_project.py @@ -6,20 +6,11 @@ from django.urls import reverse from rest_framework import status -from timed.employment.factories import UserFactory -from timed.projects.factories import ( - CustomerAssigneeFactory, - ProjectAssigneeFactory, - ProjectFactory, - TaskAssigneeFactory, - TaskFactory, -) from timed.projects.serializers import ProjectSerializer -def test_project_list_not_archived(internal_employee_client): - project = ProjectFactory.create(archived=False) - ProjectFactory.create(archived=True) +def test_project_list_not_archived(internal_employee_client, project, project_factory): + project_factory.create(archived=True) url = reverse("project-list") @@ -32,10 +23,13 @@ def test_project_list_not_archived(internal_employee_client): def test_project_list_include( - internal_employee_client, django_assert_num_queries, project + internal_employee_client, + django_assert_num_queries, + project, + user, + project_assignee_factory, ): - user = UserFactory.create() - ProjectAssigneeFactory.create(user=user, project=project, is_reviewer=True) + project_assignee_factory.create(user=user, project=project, is_reviewer=True) url = reverse("project-list") @@ -119,10 +113,13 @@ def test_project_delete(auth_client, project): @pytest.mark.parametrize(("is_assigned", "expected"), [(True, 1), (False, 0)]) def test_project_list_external_employee( - external_employee_client, is_assigned, expected + external_employee_client, + is_assigned, + expected, + project_factory, ): - ProjectFactory.create_batch(4) - project = ProjectFactory.create() + project_factory.create_batch(4) + project = project_factory.create() if is_assigned: project.assignees.add(external_employee_client.user) @@ -135,11 +132,13 @@ def test_project_list_external_employee( assert len(json["data"]) == expected -def test_project_filter(internal_employee_client): +def test_project_filter( + internal_employee_client, project_factory, project_assignee_factory +): user = internal_employee_client.user - proj1, proj2, *_ = ProjectFactory.create_batch(4) - ProjectAssigneeFactory.create(project=proj1, user=user, is_reviewer=True) - ProjectAssigneeFactory.create(project=proj1, user=user, is_manager=True) + proj1, proj2, *_ = project_factory.create_batch(4) + project_assignee_factory.create(project=proj1, user=user, is_reviewer=True) + project_assignee_factory.create(project=proj1, user=user, is_manager=True) url = reverse("project-list") @@ -156,8 +155,8 @@ def test_project_filter(internal_employee_client): assert len(json["data"]) == 1 -def test_project_multi_number_value_filter(internal_employee_client): - proj1, proj2, *_ = ProjectFactory.create_batch(4) +def test_project_multi_number_value_filter(internal_employee_client, project_factory): + proj1, proj2, *_ = project_factory.create_batch(4) url = reverse("project-list") @@ -198,10 +197,17 @@ def test_project_update_billed_flag(report_factory): (False, False, 0), ], ) -def test_project_list_no_employment(auth_client, project, is_customer, expected): - ProjectFactory.create_batch(4) +def test_project_list_no_employment( + auth_client, + project, + is_customer, + expected, + project_factory, + customer_assignee_factory, +): + project_factory.create_batch(4) if is_customer: - CustomerAssigneeFactory.create( + customer_assignee_factory.create( user=auth_client.user, is_customer=True, customer=project.customer ) @@ -224,19 +230,24 @@ def test_project_list_no_employment(auth_client, project, is_customer, expected) ], ) def test_project_activate_remaining_effort( - internal_employee_client, assignee_level, status_code + internal_employee_client, + assignee_level, + status_code, + task, + customer_assignee_factory, + project_assignee_factory, + task_assignee_factory, ): - task = TaskFactory.create() user = internal_employee_client.user if assignee_level == "customer": - CustomerAssigneeFactory( + customer_assignee_factory( user=user, customer=task.project.customer, is_manager=True ) elif assignee_level == "project": - ProjectAssigneeFactory(user=user, project=task.project, is_manager=True) + project_assignee_factory(user=user, project=task.project, is_manager=True) elif assignee_level == "task": - TaskAssigneeFactory(user=user, task=task, is_manager=True) + task_assignee_factory(user=user, task=task, is_manager=True) data = { "data": { diff --git a/timed/projects/tests/test_project_assignee.py b/timed/projects/tests/test_project_assignee.py index 30766088..666144cd 100644 --- a/timed/projects/tests/test_project_assignee.py +++ b/timed/projects/tests/test_project_assignee.py @@ -2,9 +2,6 @@ from django.urls import reverse from rest_framework.status import HTTP_200_OK -from timed.conftest import setup_customer_and_employment_status -from timed.projects.factories import ProjectAssigneeFactory - @pytest.mark.parametrize( ("is_employed", "is_external", "is_customer_assignee", "is_customer", "expected"), @@ -20,7 +17,14 @@ ], ) def test_project_assignee_list( - auth_client, is_employed, is_external, is_customer_assignee, is_customer, expected + auth_client, + is_employed, + is_external, + is_customer_assignee, + is_customer, + expected, + setup_customer_and_employment_status, + project_assignee_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -30,7 +34,7 @@ def test_project_assignee_list( is_employed=is_employed, is_external=is_external, ) - project_assignee = ProjectAssigneeFactory.create() + project_assignee = project_assignee_factory.create() url = reverse("project-assignee-list") res = auth_client.get(url) diff --git a/timed/projects/tests/test_task.py b/timed/projects/tests/test_task.py index df79ca74..6dc9ce2d 100644 --- a/timed/projects/tests/test_task.py +++ b/timed/projects/tests/test_task.py @@ -6,13 +6,6 @@ from django.urls import reverse from rest_framework import status -from timed.employment.factories import EmploymentFactory -from timed.projects.factories import ( - CustomerAssigneeFactory, - ProjectFactory, - TaskFactory, -) - def test_task_list_not_archived(internal_employee_client, task_factory): task = task_factory(archived=False) @@ -129,15 +122,22 @@ def test_task_create( ], ) def test_task_update( - auth_client, task, task_assignee, project_assignee, different_project, expected + auth_client, + task, + task_assignee, + project_assignee, + different_project, + expected, + employment_factory, + project_factory, ): user = auth_client.user - EmploymentFactory.create(user=user) + employment_factory.create(user=user) task_assignee.task = task task_assignee.user = user task_assignee.save() if different_project: - project = ProjectFactory.create() + project = project_factory.create() project_assignee.project = project project_assignee.user = user project_assignee.save() @@ -169,12 +169,12 @@ def test_task_update( (False, False, True, status.HTTP_403_FORBIDDEN), ], ) -def test_task_delete(auth_client, task, project_assignee, expected): +def test_task_delete(auth_client, task, project_assignee, expected, employment_factory): user = auth_client.user project_assignee.project = task.project project_assignee.user = user project_assignee.save() - EmploymentFactory.create(user=user) + employment_factory.create(user=user) url = reverse("task-detail", args=[task.id]) @@ -207,9 +207,14 @@ def test_task_detail_with_reports(internal_employee_client, task, report_factory @pytest.mark.parametrize(("is_assigned", "expected"), [(True, 1), (False, 0)]) -def test_task_list_external_employee(external_employee_client, is_assigned, expected): - TaskFactory.create_batch(4) - task = TaskFactory.create() +def test_task_list_external_employee( + external_employee_client, + is_assigned, + expected, + task_factory, +): + task_factory.create_batch(4) + task = task_factory.create() if is_assigned: task.assignees.add(external_employee_client.user) @@ -231,11 +236,18 @@ def test_task_list_external_employee(external_employee_client, is_assigned, expe (False, True, 0), ], ) -def test_task_list_no_employment(auth_client, is_customer, customer_visible, expected): - TaskFactory.create_batch(4) - task = TaskFactory.create() +def test_task_list_no_employment( + auth_client, + is_customer, + customer_visible, + expected, + task_factory, + customer_assignee_factory, +): + task_factory.create_batch(4) + task = task_factory.create() if is_customer: - CustomerAssigneeFactory.create( + customer_assignee_factory.create( user=auth_client.user, is_customer=True, customer=task.project.customer ) if customer_visible: @@ -251,8 +263,8 @@ def test_task_list_no_employment(auth_client, is_customer, customer_visible, exp assert len(json["data"]) == expected -def test_task_multi_number_value_filter(internal_employee_client): - task1, task2, *_ = TaskFactory.create_batch(4) +def test_task_multi_number_value_filter(internal_employee_client, task_factory): + task1, task2, *_ = task_factory.create_batch(4) url = reverse("task-list") diff --git a/timed/projects/tests/test_task_assignee.py b/timed/projects/tests/test_task_assignee.py index ce1253e2..7ca70022 100644 --- a/timed/projects/tests/test_task_assignee.py +++ b/timed/projects/tests/test_task_assignee.py @@ -2,9 +2,6 @@ from django.urls import reverse from rest_framework.status import HTTP_200_OK -from timed.conftest import setup_customer_and_employment_status -from timed.projects.factories import TaskAssigneeFactory - @pytest.mark.parametrize( ("is_employed", "is_external", "is_customer_assignee", "is_customer", "expected"), @@ -20,7 +17,14 @@ ], ) def test_task_assignee_list( - auth_client, is_employed, is_external, is_customer_assignee, is_customer, expected + auth_client, + is_employed, + is_external, + is_customer_assignee, + is_customer, + expected, + setup_customer_and_employment_status, + task_assignee_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -30,7 +34,7 @@ def test_task_assignee_list( is_employed=is_employed, is_external=is_external, ) - task_assignee = TaskAssigneeFactory.create() + task_assignee = task_assignee_factory.create() url = reverse("task-assignee-list") res = auth_client.get(url) diff --git a/timed/reports/tests/test_customer_statistic.py b/timed/reports/tests/test_customer_statistic.py index c914732f..580c4220 100644 --- a/timed/reports/tests/test_customer_statistic.py +++ b/timed/reports/tests/test_customer_statistic.py @@ -4,11 +4,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 EmploymentFactory, UserFactory -from timed.projects.factories import CostCenterFactory, TaskAssigneeFactory, TaskFactory -from timed.tracking.factories import ReportFactory - @pytest.mark.parametrize( ("is_employed", "is_customer_assignee", "is_customer", "expected", "status_code"), @@ -28,6 +23,8 @@ def test_customer_statistic_list( expected, status_code, django_assert_num_queries, + setup_customer_and_employment_status, + report_factory, ): user = auth_client.user @@ -44,9 +41,9 @@ def test_customer_statistic_list( # list as well third_customer = assignee.customer if assignee else None - report = ReportFactory.create(duration=timedelta(hours=1)) - ReportFactory.create(duration=timedelta(hours=2), task=report.task) - report2 = ReportFactory.create(duration=timedelta(hours=4)) + report = report_factory.create(duration=timedelta(hours=1)) + report_factory.create(duration=timedelta(hours=2), task=report.task) + report2 = report_factory.create(duration=timedelta(hours=4)) url = reverse("customer-statistic-list") with django_assert_num_queries(expected): @@ -90,7 +87,17 @@ def test_customer_statistic_list( ("filter", "expected_result"), [("from_date", 5), ("customer", 3), ("cost_center", 3), ("reviewer", 3)], ) -def test_customer_statistic_filtered(auth_client, filter, expected_result): # noqa: A002 +def test_customer_statistic_filtered( + auth_client, + filter, # noqa: A002 + expected_result, + setup_customer_and_employment_status, + cost_center_factory, + task_factory, + report_factory, + user_factory, + task_assignee_factory, +): user = auth_client.user setup_customer_and_employment_status( user=user, @@ -99,15 +106,20 @@ def test_customer_statistic_filtered(auth_client, filter, expected_result): # n is_employed=True, is_external=False, ) + cost_center = cost_center_factory() + task_z = task_factory.create(name="Z", cost_center=cost_center) + task_test = task_factory.create(name="Test") + reviewer = task_assignee_factory( + user=user_factory(), task=task_test, is_reviewer=True + ) - cost_center = CostCenterFactory() - task_z = TaskFactory.create(name="Z", cost_center=cost_center) - task_test = TaskFactory.create(name="Test") - reviewer = TaskAssigneeFactory(user=UserFactory(), task=task_test, is_reviewer=True) - - ReportFactory.create(duration=timedelta(hours=1), date="2022-08-05", task=task_test) - ReportFactory.create(duration=timedelta(hours=2), date="2022-08-30", task=task_test) - ReportFactory.create(duration=timedelta(hours=3), date="2022-09-01", task=task_z) + report_factory.create( + duration=timedelta(hours=1), date="2022-08-05", task=task_test + ) + report_factory.create( + duration=timedelta(hours=2), date="2022-08-30", task=task_test + ) + report_factory.create(duration=timedelta(hours=3), date="2022-09-01", task=task_z) filter_values = { "from_date": "2022-08-20", # last two reports @@ -137,11 +149,17 @@ def test_customer_statistic_filtered(auth_client, filter, expected_result): # n ], ) def test_customer_statistic_detail( - auth_client, is_employed, expected, status_code, django_assert_num_queries + auth_client, + is_employed, + expected, + status_code, + django_assert_num_queries, + employment_factory, + report_factory, ): if is_employed: - EmploymentFactory.create(user=auth_client.user) - report = ReportFactory.create(duration=timedelta(hours=1)) + employment_factory.create(user=auth_client.user) + report = report_factory.create(duration=timedelta(hours=1)) url = reverse("customer-statistic-detail", args=[report.task.project.customer.id]) with django_assert_num_queries(expected): diff --git a/timed/reports/tests/test_month_statistic.py b/timed/reports/tests/test_month_statistic.py index 27f463ec..3de7366f 100644 --- a/timed/reports/tests/test_month_statistic.py +++ b/timed/reports/tests/test_month_statistic.py @@ -4,9 +4,6 @@ from django.urls import reverse from rest_framework import status -from timed.conftest import setup_customer_and_employment_status -from timed.tracking.factories import ReportFactory - @pytest.mark.parametrize( ("is_employed", "is_customer_assignee", "is_customer", "expected"), @@ -19,7 +16,13 @@ ], ) def test_month_statistic_list( - auth_client, is_employed, is_customer_assignee, is_customer, expected + auth_client, + is_employed, + is_customer_assignee, + is_customer, + expected, + setup_customer_and_employment_status, + report_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -30,9 +33,9 @@ def test_month_statistic_list( is_external=False, ) - ReportFactory.create(duration=timedelta(hours=1), date=date(2016, 1, 1)) - ReportFactory.create(duration=timedelta(hours=1), date=date(2015, 12, 4)) - ReportFactory.create(duration=timedelta(hours=2), date=date(2015, 12, 31)) + report_factory.create(duration=timedelta(hours=1), date=date(2016, 1, 1)) + report_factory.create(duration=timedelta(hours=1), date=date(2015, 12, 4)) + report_factory.create(duration=timedelta(hours=2), date=date(2015, 12, 31)) url = reverse("month-statistic-list") result = auth_client.get(url, data={"ordering": "year,month"}) diff --git a/timed/reports/tests/test_project_statistic.py b/timed/reports/tests/test_project_statistic.py index dc2baf2b..3484cce5 100644 --- a/timed/reports/tests/test_project_statistic.py +++ b/timed/reports/tests/test_project_statistic.py @@ -4,11 +4,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 UserFactory -from timed.projects.factories import CostCenterFactory, TaskAssigneeFactory, TaskFactory -from timed.tracking.factories import ReportFactory - @pytest.mark.parametrize( ("is_employed", "is_customer_assignee", "is_customer", "expected", "status_code"), @@ -28,6 +23,9 @@ def test_project_statistic_list( expected, status_code, django_assert_num_queries, + setup_customer_and_employment_status, + report_factory, + task_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -37,13 +35,13 @@ def test_project_statistic_list( is_employed=is_employed, is_external=False, ) - report = ReportFactory.create(duration=timedelta(hours=1)) + report = report_factory.create(duration=timedelta(hours=1)) project = report.task.project - ReportFactory.create(duration=timedelta(hours=2), task=report.task) - report2 = ReportFactory.create(duration=timedelta(hours=4)) + report_factory.create(duration=timedelta(hours=2), task=report.task) + report2 = report_factory.create(duration=timedelta(hours=4)) project_2 = report2.task.project - task = TaskFactory(project=report.task.project) - ReportFactory.create(duration=timedelta(hours=2), task=task) + task = task_factory(project=report.task.project) + report_factory.create(duration=timedelta(hours=2), task=task) url = reverse("project-statistic-list") with django_assert_num_queries(expected): @@ -108,7 +106,17 @@ def test_project_statistic_list( ("filter", "expected_result"), [("from_date", 5), ("customer", 3), ("cost_center", 3), ("reviewer", 3)], ) -def test_project_statistic_filtered(auth_client, filter, expected_result): # noqa: A002 +def test_project_statistic_filtered( + auth_client, + filter, # noqa: A002 + expected_result, + setup_customer_and_employment_status, + cost_center_factory, + task_factory, + task_assignee_factory, + report_factory, + user_factory, +): user = auth_client.user setup_customer_and_employment_status( user=user, @@ -118,14 +126,20 @@ def test_project_statistic_filtered(auth_client, filter, expected_result): # no is_external=False, ) - cost_center = CostCenterFactory() - task_z = TaskFactory.create(name="Z", cost_center=cost_center) - task_test = TaskFactory.create(name="Test") - reviewer = TaskAssigneeFactory(user=UserFactory(), task=task_test, is_reviewer=True) + cost_center = cost_center_factory() + task_z = task_factory.create(name="Z", cost_center=cost_center) + task_test = task_factory.create(name="Test") + reviewer = task_assignee_factory( + user=user_factory(), task=task_test, is_reviewer=True + ) - ReportFactory.create(duration=timedelta(hours=1), date="2022-08-05", task=task_test) - ReportFactory.create(duration=timedelta(hours=2), date="2022-08-30", task=task_test) - ReportFactory.create(duration=timedelta(hours=3), date="2022-09-01", task=task_z) + report_factory.create( + duration=timedelta(hours=1), date="2022-08-05", task=task_test + ) + report_factory.create( + duration=timedelta(hours=2), date="2022-08-30", task=task_test + ) + report_factory.create(duration=timedelta(hours=3), date="2022-09-01", task=task_z) filter_values = { "from_date": "2022-08-20", # last two reports diff --git a/timed/reports/tests/test_task_statistic.py b/timed/reports/tests/test_task_statistic.py index f80f36fa..c72ad11f 100644 --- a/timed/reports/tests/test_task_statistic.py +++ b/timed/reports/tests/test_task_statistic.py @@ -4,11 +4,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 UserFactory -from timed.projects.factories import CostCenterFactory, TaskAssigneeFactory, TaskFactory -from timed.tracking.factories import ReportFactory - @pytest.mark.parametrize( ("is_employed", "is_customer_assignee", "is_customer", "expected", "status_code"), @@ -28,6 +23,9 @@ def test_task_statistic_list( expected, status_code, django_assert_num_queries, + setup_customer_and_employment_status, + report_factory, + task_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -37,11 +35,11 @@ def test_task_statistic_list( is_employed=is_employed, is_external=False, ) - task_z = TaskFactory.create(name="Z") - task_test = TaskFactory.create(name="Test") - ReportFactory.create(duration=timedelta(hours=1), task=task_test) - ReportFactory.create(duration=timedelta(hours=2), task=task_test) - ReportFactory.create(duration=timedelta(hours=2), task=task_z) + task_z = task_factory.create(name="Z") + task_test = task_factory.create(name="Test") + report_factory.create(duration=timedelta(hours=1), task=task_test) + report_factory.create(duration=timedelta(hours=2), task=task_test) + report_factory.create(duration=timedelta(hours=2), task=task_z) url = reverse("task-statistic-list") with django_assert_num_queries(expected): @@ -100,6 +98,12 @@ def test_task_statistic_filtered( auth_client, filter, # noqa: A002 expected_result, + setup_customer_and_employment_status, + cost_center_factory, + user_factory, + task_assignee_factory, + task_factory, + report_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -110,14 +114,20 @@ def test_task_statistic_filtered( is_external=False, ) - cost_center = CostCenterFactory() - task_z = TaskFactory.create(name="Z", cost_center=cost_center) - task_test = TaskFactory.create(name="Test") - reviewer = TaskAssigneeFactory(user=UserFactory(), task=task_test, is_reviewer=True) + cost_center = cost_center_factory() + task_z = task_factory.create(name="Z", cost_center=cost_center) + task_test = task_factory.create(name="Test") + reviewer = task_assignee_factory( + user=user_factory(), task=task_test, is_reviewer=True + ) - ReportFactory.create(duration=timedelta(hours=1), date="2022-08-05", task=task_test) - ReportFactory.create(duration=timedelta(hours=2), date="2022-08-30", task=task_test) - ReportFactory.create(duration=timedelta(hours=3), date="2022-09-01", task=task_z) + report_factory.create( + duration=timedelta(hours=1), date="2022-08-05", task=task_test + ) + report_factory.create( + duration=timedelta(hours=2), date="2022-08-30", task=task_test + ) + report_factory.create(duration=timedelta(hours=3), date="2022-09-01", task=task_z) filter_values = { "from_date": "2022-08-20", # last two reports diff --git a/timed/reports/tests/test_user_statistic.py b/timed/reports/tests/test_user_statistic.py index f10d968d..788e3960 100644 --- a/timed/reports/tests/test_user_statistic.py +++ b/timed/reports/tests/test_user_statistic.py @@ -4,9 +4,6 @@ from django.urls import reverse from rest_framework import status -from timed.conftest import setup_customer_and_employment_status -from timed.tracking.factories import ReportFactory - @pytest.mark.parametrize( ("is_employed", "is_customer_assignee", "is_customer", "status_code"), @@ -19,7 +16,13 @@ ], ) def test_user_statistic_list( - auth_client, is_employed, is_customer_assignee, is_customer, status_code + auth_client, + is_employed, + is_customer_assignee, + is_customer, + status_code, + setup_customer_and_employment_status, + report_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -29,9 +32,9 @@ def test_user_statistic_list( is_employed=is_employed, is_external=False, ) - ReportFactory.create(duration=timedelta(hours=1), user=user) - ReportFactory.create(duration=timedelta(hours=2), user=user) - report = ReportFactory.create(duration=timedelta(hours=2)) + report_factory(duration=timedelta(hours=1), user=user) + report_factory(duration=timedelta(hours=2), user=user) + report = report_factory(duration=timedelta(hours=2)) url = reverse("user-statistic-list") result = auth_client.get(url, data={"ordering": "duration", "include": "user"}) diff --git a/timed/reports/tests/test_work_report.py b/timed/reports/tests/test_work_report.py index fc002b95..4b414ec0 100644 --- a/timed/reports/tests/test_work_report.py +++ b/timed/reports/tests/test_work_report.py @@ -7,11 +7,7 @@ from django.urls import reverse from rest_framework import status -from timed.conftest import setup_customer_and_employment_status -from timed.employment.factories import EmploymentFactory -from timed.projects.factories import CustomerFactory, ProjectFactory, TaskFactory from timed.reports.views import WorkReportViewSet -from timed.tracking.factories import ReportFactory @pytest.mark.freeze_time("2017-09-01") @@ -33,6 +29,11 @@ def test_work_report_single_project( expected, status_code, django_assert_num_queries, + setup_customer_and_employment_status, + customer_factory, + project_factory, + task_factory, + report_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -43,11 +44,11 @@ def test_work_report_single_project( is_external=False, ) # spaces should be replaced with underscore - customer = CustomerFactory.create(name="Customer Name") + customer = customer_factory.create(name="Customer Name") # slashes should be dropped from file name - project = ProjectFactory.create(customer=customer, name="Project/") - task = TaskFactory.create(project=project) - ReportFactory.create_batch( + project = project_factory.create(customer=customer, name="Project/") + task = task_factory.create(project=project) + report_factory.create_batch( 5, user=user, verified_by=user, @@ -55,7 +56,7 @@ def test_work_report_single_project( date=date(2017, 8, 17), not_billable=True, ) - ReportFactory.create_batch( + report_factory.create_batch( 5, user=user, verified_by=user, @@ -98,19 +99,28 @@ def test_work_report_single_project( ], ) def test_work_report_multiple_projects( - auth_client, is_employed, status_code, expected, django_assert_num_queries + auth_client, + is_employed, + status_code, + expected, + django_assert_num_queries, + employment_factory, + customer_factory, + task_factory, + project_factory, + report_factory, ): num_projects = 2 user = auth_client.user if is_employed: - EmploymentFactory.create(user=user) - customer = CustomerFactory.create(name="Customer") + employment_factory.create(user=user) + customer = customer_factory.create(name="Customer") report_date = date(2017, 8, 17) for i in range(num_projects): - project = ProjectFactory.create(customer=customer, name=f"Project{i}") - task = TaskFactory.create(project=project) - ReportFactory.create_batch(10, user=user, task=task, date=report_date) + project = project_factory.create(customer=customer, name=f"Project{i}") + task = task_factory.create(project=project) + report_factory.create_batch(10, user=user, task=task, date=report_date) url = reverse("work-report-list") with django_assert_num_queries(expected): @@ -145,14 +155,16 @@ def test_work_report_empty(auth_client): ("Customer$Name", "Project", "1708-20170818-CustomerName-Project.ods"), ], ) -def test_generate_work_report_name(customer_name, project_name, expected): +def test_generate_work_report_name( + customer_name, project_name, expected, customer_factory, project_factory +): test_date = date(2017, 8, 18) view = WorkReportViewSet() # spaces should be replaced with underscore - customer = CustomerFactory.create(name=customer_name) + customer = customer_factory.create(name=customer_name) # slashes should be dropped from file name - project = ProjectFactory.create(customer=customer, name=project_name) + project = project_factory.create(customer=customer, name=project_name) name = view._generate_workreport_name(test_date, project) # noqa: SLF001 assert name == expected @@ -169,17 +181,25 @@ def test_generate_work_report_name(customer_name, project_name, expected): ], ) def test_work_report_count( - internal_employee_client, settings, settings_count, given_count, expected_status + internal_employee_client, + settings, + settings_count, + given_count, + expected_status, + customer_factory, + project_factory, + task_factory, + report_factory, ): user = internal_employee_client.user - customer = CustomerFactory.create(name="Customer") + customer = customer_factory.create(name="Customer") report_date = date(2017, 8, 17) settings.WORK_REPORTS_EXPORT_MAX_COUNT = settings_count - project = ProjectFactory.create(customer=customer) - task = TaskFactory.create(project=project) - ReportFactory.create_batch(given_count, user=user, task=task, date=report_date) + project = project_factory.create(customer=customer) + task = task_factory.create(project=project) + report_factory.create_batch(given_count, user=user, task=task, date=report_date) url = reverse("work-report-list") res = internal_employee_client.get( diff --git a/timed/reports/tests/test_year_statistic.py b/timed/reports/tests/test_year_statistic.py index 37534863..ace8756a 100644 --- a/timed/reports/tests/test_year_statistic.py +++ b/timed/reports/tests/test_year_statistic.py @@ -4,10 +4,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 EmploymentFactory -from timed.tracking.factories import ReportFactory - @pytest.mark.parametrize( ("is_employed", "is_customer_assignee", "is_customer", "expected"), @@ -20,7 +16,13 @@ ], ) def test_year_statistic_list( - auth_client, is_employed, is_customer_assignee, is_customer, expected + auth_client, + is_employed, + is_customer_assignee, + is_customer, + expected, + setup_customer_and_employment_status, + report_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -31,9 +33,9 @@ def test_year_statistic_list( is_external=False, ) - ReportFactory.create(duration=timedelta(hours=1), date=date(2017, 1, 1)) - ReportFactory.create(duration=timedelta(hours=1), date=date(2015, 2, 28)) - ReportFactory.create(duration=timedelta(hours=1), date=date(2015, 12, 31)) + report_factory(duration=timedelta(hours=1), date=date(2017, 1, 1)) + report_factory(duration=timedelta(hours=1), date=date(2015, 2, 28)) + report_factory(duration=timedelta(hours=1), date=date(2015, 12, 31)) url = reverse("year-statistic-list") result = auth_client.get(url, data={"ordering": "year"}) @@ -64,11 +66,17 @@ def test_year_statistic_list( (False, status.HTTP_403_FORBIDDEN), ], ) -def test_year_statistic_detail(auth_client, is_employed, expected): +def test_year_statistic_detail( + auth_client, + is_employed, + expected, + employment_factory, + report_factory, +): if is_employed: - EmploymentFactory.create(user=auth_client.user) - ReportFactory.create(duration=timedelta(hours=1), date=date(2015, 2, 28)) - ReportFactory.create(duration=timedelta(hours=1), date=date(2015, 12, 31)) + employment_factory.create(user=auth_client.user) + report_factory.create(duration=timedelta(hours=1), date=date(2015, 2, 28)) + report_factory.create(duration=timedelta(hours=1), date=date(2015, 12, 31)) url = reverse("year-statistic-detail", args=[2015]) result = auth_client.get(url, data={"ordering": "year"}) diff --git a/timed/subscription/tests/test_order.py b/timed/subscription/tests/test_order.py index 26ca7307..c1b5b4bc 100644 --- a/timed/subscription/tests/test_order.py +++ b/timed/subscription/tests/test_order.py @@ -5,8 +5,6 @@ from rest_framework import status from timed.notifications.models import Notification -from timed.projects.factories import CustomerAssigneeFactory, ProjectFactory -from timed.subscription import factories @pytest.mark.parametrize( @@ -18,13 +16,19 @@ (False, False, False), ], ) -def test_order_list(auth_client, is_customer, is_accountant, is_superuser): +def test_order_list( + auth_client, + is_customer, + is_accountant, + is_superuser, + order, + customer_assignee_factory, +): """Test which user can see orders.""" - order = factories.OrderFactory.create() user = auth_client.user if is_customer: - CustomerAssigneeFactory.create( + customer_assignee_factory.create( customer=order.project.customer, user=user, is_customer=True ) elif is_accountant: @@ -60,10 +64,16 @@ def test_order_list(auth_client, is_customer, is_accountant, is_superuser): ], ) def test_order_delete( - auth_client, is_customer, is_accountant, is_superuser, confirmed, expected + auth_client, + is_customer, + is_accountant, + is_superuser, + confirmed, + expected, + order, + customer_assignee_factory, ): """Test which user can delete orders, confirmed or not.""" - order = factories.OrderFactory() if confirmed: order.acknowledged = True order.save() @@ -71,7 +81,7 @@ def test_order_delete( user = auth_client.user if is_customer: - CustomerAssigneeFactory.create( + customer_assignee_factory.create( customer=order.project.customer, user=user, is_customer=True ) elif is_accountant: @@ -97,10 +107,15 @@ def test_order_delete( ], ) def test_order_confirm( - auth_client, is_superuser, is_accountant, is_customer, status_code + auth_client, + is_superuser, + is_accountant, + is_customer, + status_code, + order, + customer_assignee_factory, ): """Test which user may confirm orders.""" - order = factories.OrderFactory.create() user = auth_client.user if is_superuser: @@ -110,7 +125,7 @@ def test_order_confirm( user.is_accountant = True user.save() elif is_customer: - CustomerAssigneeFactory.create( + customer_assignee_factory.create( user=user, is_customer=True, customer=order.project.customer ) @@ -164,16 +179,18 @@ def test_order_create( mail_sent, project_estimate, expected, + project_factory, + customer_assignee_factory, ): """Test which user may create orders. Additionally test if for creation of acknowledged/confirmed orders. """ user = auth_client.user - project = ProjectFactory.create(estimated_time=project_estimate) + project = project_factory.create(estimated_time=project_estimate) if is_customer: - CustomerAssigneeFactory.create( + customer_assignee_factory.create( user=user, is_customer=True, customer=project.customer ) elif is_accountant: @@ -227,11 +244,17 @@ def test_order_create( ], ) def test_order_create_duration( - auth_client, mailoutbox, duration, expected, status_code + auth_client, + mailoutbox, + duration, + expected, + status_code, + project_factory, + customer_assignee_factory, ): user = auth_client.user - project = ProjectFactory.create(estimated_time=timedelta(hours=1)) - CustomerAssigneeFactory.create( + project = project_factory.create(estimated_time=timedelta(hours=1)) + customer_assignee_factory.create( user=user, is_customer=True, customer=project.customer ) @@ -277,17 +300,23 @@ def test_order_create_duration( ], ) def test_order_update( - auth_client, is_customer, is_accountant, is_superuser, acknowledged, expected + auth_client, + is_customer, + is_accountant, + is_superuser, + acknowledged, + expected, + order, + customer_assignee_factory, ): user = auth_client.user - order = factories.OrderFactory.create() if acknowledged: order.acknowledged = True order.save() if is_customer: - CustomerAssigneeFactory.create( + customer_assignee_factory.create( user=user, is_customer=True, customer=order.project.customer ) elif is_accountant: diff --git a/timed/subscription/tests/test_package.py b/timed/subscription/tests/test_package.py index 60f5ccba..c0659043 100644 --- a/timed/subscription/tests/test_package.py +++ b/timed/subscription/tests/test_package.py @@ -1,13 +1,10 @@ +import pytest from django.urls import reverse from rest_framework.status import HTTP_200_OK -from timed.projects.factories import BillingTypeFactory, CustomerFactory, ProjectFactory -from timed.subscription.factories import PackageFactory - +@pytest.mark.usefixtures("package") def test_subscription_package_list(auth_client): - PackageFactory.create() - url = reverse("subscription-package-list") res = auth_client.get(url) @@ -17,11 +14,11 @@ def test_subscription_package_list(auth_client): assert len(json["data"]) == 1 -def test_subscription_package_filter_customer(auth_client): - customer = CustomerFactory.create() - billing_type = BillingTypeFactory.create() - package = PackageFactory.create(billing_type=billing_type) - ProjectFactory.create_batch(2, billing_type=billing_type, customer=customer) +def test_subscription_package_filter_customer( + auth_client, customer, billing_type, package_factory, project_factory +): + package = package_factory.create(billing_type=billing_type) + project_factory.create_batch(2, billing_type=billing_type, customer=customer) url = reverse("subscription-package-list") diff --git a/timed/subscription/tests/test_subscription_project.py b/timed/subscription/tests/test_subscription_project.py index be215656..3d9dae27 100644 --- a/timed/subscription/tests/test_subscription_project.py +++ b/timed/subscription/tests/test_subscription_project.py @@ -4,49 +4,54 @@ from django.urls import reverse from rest_framework.status import HTTP_200_OK, HTTP_404_NOT_FOUND -from timed.employment.factories import EmploymentFactory -from timed.projects.factories import ( - BillingTypeFactory, - CustomerAssigneeFactory, - CustomerFactory, - ProjectFactory, - TaskFactory, -) -from timed.subscription.factories import OrderFactory, PackageFactory -from timed.tracking.factories import ReportFactory - @pytest.mark.parametrize(("is_external", "expected"), [(True, 0), (False, 1)]) -def test_subscription_project_list(auth_client, is_external, expected): - employment = EmploymentFactory.create(user=auth_client.user, is_external=False) +def test_subscription_project_list( + auth_client, + is_external, + expected, + employment_factory, + customer_factory, + billing_type_factory, + project_factory, + package_factory, + task_factory, + report_factory, + order_factory, +): + employment = employment_factory.create(user=auth_client.user, is_external=False) if is_external: employment.is_external = True employment.save() - customer = CustomerFactory.create() - billing_type = BillingTypeFactory() - project = ProjectFactory.create( + customer = customer_factory.create() + billing_type = billing_type_factory() + project = project_factory.create( billing_type=billing_type, customer=customer, customer_visible=True ) - PackageFactory.create_batch(2, billing_type=billing_type) + package_factory.create_batch(2, billing_type=billing_type) # create spent hours - task = TaskFactory.create(project=project) - TaskFactory.create(project=project) - ReportFactory.create(task=task, duration=timedelta(hours=2)) - ReportFactory.create(task=task, duration=timedelta(hours=3)) + task = task_factory.create(project=project) + task_factory.create(project=project) + report_factory.create(task=task, duration=timedelta(hours=2)) + report_factory.create(task=task, duration=timedelta(hours=3)) # not billable reports should not be included in spent hours - ReportFactory.create(not_billable=True, task=task, duration=timedelta(hours=4)) + report_factory.create(not_billable=True, task=task, duration=timedelta(hours=4)) # project of same customer but without customer_visible set # should not appear - ProjectFactory.create(customer=customer) + project_factory.create(customer=customer) # create purchased time - OrderFactory.create(project=project, acknowledged=True, duration=timedelta(hours=2)) - OrderFactory.create(project=project, acknowledged=True, duration=timedelta(hours=4)) + order_factory.create( + project=project, acknowledged=True, duration=timedelta(hours=2) + ) + order_factory.create( + project=project, acknowledged=True, duration=timedelta(hours=4) + ) # report on different project should not be included in spent time - ReportFactory.create(duration=timedelta(hours=2)) + report_factory.create(duration=timedelta(hours=2)) # not acknowledged order should not be included in purchased time - OrderFactory.create(project=project, duration=timedelta(hours=2)) + order_factory.create(project=project, duration=timedelta(hours=2)) url = reverse("subscription-project-list") @@ -73,21 +78,30 @@ def test_subscription_project_list(auth_client, is_external, expected): ], ) def test_subscription_project_detail( - auth_client, is_customer, project_of_customer, has_employment, is_external, expected + auth_client, + is_customer, + project_of_customer, + has_employment, + is_external, + expected, + billing_type, + project_factory, + package_factory, + employment_factory, + customer_assignee_factory, ): user = auth_client.user - billing_type = BillingTypeFactory() - project = ProjectFactory.create(billing_type=billing_type, customer_visible=True) - PackageFactory.create_batch(2, billing_type=billing_type) + project = project_factory.create(billing_type=billing_type, customer_visible=True) + package_factory.create_batch(2, billing_type=billing_type) if has_employment: - employment = EmploymentFactory.create(user=user, is_external=False) + employment = employment_factory.create(user=user, is_external=False) if is_external: employment.is_external = True employment.save() if is_customer: - customer_assignee = CustomerAssigneeFactory(user=user, is_customer=True) + customer_assignee = customer_assignee_factory(user=user, is_customer=True) if project_of_customer: customer_assignee.customer = project.customer customer_assignee.save() @@ -101,13 +115,14 @@ def test_subscription_project_detail( assert json["data"]["id"] == str(project.id) -def test_subscription_project_list_user_is_customer(auth_client): - customer = CustomerFactory.create() - project = ProjectFactory.create(customer=customer, customer_visible=True) - ProjectFactory.create_batch(4, customer_visible=True) +def test_subscription_project_list_user_is_customer( + auth_client, customer, project_factory, customer_assignee_factory +): + project = project_factory.create(customer=customer, customer_visible=True) + project_factory.create_batch(4, customer_visible=True) user = auth_client.user - CustomerAssigneeFactory.create(user=user, customer=customer, is_customer=True) + customer_assignee_factory.create(user=user, customer=customer, is_customer=True) url = reverse("subscription-project-list") diff --git a/timed/tests/test_authentication.py b/timed/tests/test_authentication.py index 82dc75bb..2ffda595 100644 --- a/timed/tests/test_authentication.py +++ b/timed/tests/test_authentication.py @@ -10,8 +10,6 @@ from rest_framework.exceptions import AuthenticationFailed from rest_framework.reverse import reverse -from timed.employment.factories import UserFactory - @pytest.mark.django_db() @pytest.mark.parametrize("is_id_token", [True, False]) @@ -91,9 +89,8 @@ def test_authentication_new_user( @pytest.mark.django_db() -def test_authentication_update_user_data(rf, requests_mock, settings): +def test_authentication_update_user_data(rf, requests_mock, settings, user): user_model = get_user_model() - user = UserFactory.create() userinfo = { "sub": user.username, diff --git a/timed/tracking/tests/test_absence.py b/timed/tracking/tests/test_absence.py index ccde010e..1421cb78 100644 --- a/timed/tracking/tests/test_absence.py +++ b/timed/tracking/tests/test_absence.py @@ -4,34 +4,34 @@ from django.urls import reverse from rest_framework import status -from timed.employment.factories import ( - AbsenceTypeFactory, - EmploymentFactory, - PublicHolidayFactory, - UserFactory, -) -from timed.tracking.factories import AbsenceFactory, ReportFactory - @pytest.mark.parametrize( "is_external", [True, False], ) -def test_absence_list_authenticated(auth_client, is_external): - absence = AbsenceFactory.create(user=auth_client.user) +def test_absence_list_authenticated( + auth_client, + is_external, + absence_factory, + employment_factory, + public_holiday_factory, +): + absence = absence_factory.create(user=auth_client.user) # overlapping absence with public holidays need to be hidden - overlap_absence = AbsenceFactory.create( + overlap_absence = absence_factory.create( user=auth_client.user, date=datetime.date(2018, 1, 1) ) - employment = EmploymentFactory.create( + employment = employment_factory.create( user=overlap_absence.user, start_date=datetime.date(2017, 12, 31) ) if is_external: employment.is_external = True employment.save() - PublicHolidayFactory.create(date=overlap_absence.date, location=employment.location) + public_holiday_factory.create( + date=overlap_absence.date, location=employment.location + ) url = reverse("absence-list") response = auth_client.get(url) @@ -44,8 +44,8 @@ def test_absence_list_authenticated(auth_client, is_external): assert json["data"][0]["id"] == str(absence.id) -def test_absence_list_superuser(superadmin_client): - AbsenceFactory.create_batch(2) +def test_absence_list_superuser(superadmin_client, absence_factory): + absence_factory.create_batch(2) url = reverse("absence-list") response = superadmin_client.get(url) @@ -55,12 +55,11 @@ def test_absence_list_superuser(superadmin_client): assert len(json["data"]) == 2 -def test_absence_list_supervisor(internal_employee_client): - user = UserFactory.create() +def test_absence_list_supervisor(internal_employee_client, user, absence_factory): internal_employee_client.user.supervisees.add(user) - AbsenceFactory.create(user=internal_employee_client.user) - AbsenceFactory.create(user=user) + absence_factory.create(user=internal_employee_client.user) + absence_factory.create(user=user) url = reverse("absence-list") response = internal_employee_client.get(url) @@ -69,13 +68,15 @@ def test_absence_list_supervisor(internal_employee_client): assert len(json["data"]) == 2 -def test_absence_list_supervisee(internal_employee_client): - AbsenceFactory.create(user=internal_employee_client.user) +def test_absence_list_supervisee( + internal_employee_client, absence_factory, user_factory +): + absence_factory.create(user=internal_employee_client.user) - supervisors = UserFactory.create_batch(2) + supervisors = user_factory.create_batch(2) supervisors[0].supervisees.add(internal_employee_client.user) - AbsenceFactory.create(user=supervisors[0]) + absence_factory.create(user=supervisors[0]) url = reverse("absence-list") @@ -86,7 +87,7 @@ def test_absence_list_supervisee(internal_employee_client): # absences of multiple supervisors shouldn't affect supervisee supervisors[1].supervisees.add(internal_employee_client.user) - AbsenceFactory.create(user=supervisors[1]) + absence_factory.create(user=supervisors[1]) response = internal_employee_client.get(url) assert response.status_code == status.HTTP_200_OK @@ -94,8 +95,8 @@ def test_absence_list_supervisee(internal_employee_client): assert len(json["data"]) == 1 -def test_absence_detail(internal_employee_client): - absence = AbsenceFactory.create(user=internal_employee_client.user) +def test_absence_detail(internal_employee_client, absence_factory): + absence = absence_factory.create(user=internal_employee_client.user) url = reverse("absence-detail", args=[absence.id]) @@ -110,13 +111,15 @@ def test_absence_detail(internal_employee_client): ("is_external", "expected"), [(False, status.HTTP_201_CREATED), (True, status.HTTP_403_FORBIDDEN)], ) -def test_absence_create(auth_client, is_external, expected): +def test_absence_create( + auth_client, is_external, expected, employment_factory, absence_type_factory +): user = auth_client.user date = datetime.date(2017, 5, 4) - employment = EmploymentFactory.create( + employment = employment_factory.create( user=user, start_date=date, worktime_per_day=datetime.timedelta(hours=8) ) - absence_type = AbsenceTypeFactory.create() + absence_type = absence_type_factory.create() if is_external: employment.is_external = True @@ -148,13 +151,13 @@ def test_absence_create(auth_client, is_external, expected): ) -def test_absence_update_owner(auth_client): +def test_absence_update_owner(auth_client, absence_factory, employment_factory): user = auth_client.user date = datetime.date(2017, 5, 3) - absence = AbsenceFactory.create( + absence = absence_factory.create( user=auth_client.user, date=datetime.date(2016, 5, 3) ) - EmploymentFactory.create( + employment_factory.create( user=user, start_date=date, worktime_per_day=datetime.timedelta(hours=8) ) @@ -175,12 +178,13 @@ def test_absence_update_owner(auth_client): assert json["data"]["attributes"]["date"] == "2017-05-03" -def test_absence_update_superadmin_date(superadmin_client): +def test_absence_update_superadmin_date( + superadmin_client, user, absence_factory, employment_factory +): """Test that superadmin may not change date of absence.""" - user = UserFactory.create() date = datetime.date(2017, 5, 3) - absence = AbsenceFactory.create(user=user, date=datetime.date(2016, 5, 3)) - EmploymentFactory.create( + absence = absence_factory.create(user=user, date=datetime.date(2016, 5, 3)) + employment_factory.create( user=user, start_date=date, worktime_per_day=datetime.timedelta(hours=8) ) @@ -198,13 +202,13 @@ def test_absence_update_superadmin_date(superadmin_client): assert response.status_code == status.HTTP_400_BAD_REQUEST -def test_absence_update_superadmin_type(superadmin_client): +def test_absence_update_superadmin_type( + superadmin_client, user, absence_type, absence_factory, employment_factory +): """Test that superadmin may not change type of absence.""" - user = UserFactory.create() date = datetime.date(2017, 5, 3) - absence_type = AbsenceTypeFactory.create() - absence = AbsenceFactory.create(user=user, date=datetime.date(2016, 5, 3)) - EmploymentFactory.create( + absence = absence_factory.create(user=user, date=datetime.date(2016, 5, 3)) + employment_factory.create( user=user, start_date=date, worktime_per_day=datetime.timedelta(hours=8) ) @@ -227,8 +231,8 @@ def test_absence_update_superadmin_type(superadmin_client): assert response.status_code == status.HTTP_400_BAD_REQUEST -def test_absence_delete_owner(internal_employee_client): - absence = AbsenceFactory.create(user=internal_employee_client.user) +def test_absence_delete_owner(internal_employee_client, absence_factory): + absence = absence_factory.create(user=internal_employee_client.user) url = reverse("absence-detail", args=[absence.id]) @@ -236,10 +240,9 @@ def test_absence_delete_owner(internal_employee_client): assert response.status_code == status.HTTP_204_NO_CONTENT -def test_absence_delete_superuser(superadmin_client): +def test_absence_delete_superuser(superadmin_client, user, absence_factory): """Test that superuser may not delete absences of other users.""" - user = UserFactory.create() - absence = AbsenceFactory.create(user=user) + absence = absence_factory.create(user=user) url = reverse("absence-detail", args=[absence.id]) @@ -247,16 +250,21 @@ def test_absence_delete_superuser(superadmin_client): assert response.status_code == status.HTTP_403_FORBIDDEN -def test_absence_fill_worktime(auth_client): +def test_absence_fill_worktime( + auth_client, + employment_factory, + absence_type_factory, + report_factory, +): """Should create an absence which fills the worktime.""" date = datetime.date(2017, 5, 10) user = auth_client.user - EmploymentFactory.create( + employment_factory.create( user=user, start_date=date, worktime_per_day=datetime.timedelta(hours=8) ) - absence_type = AbsenceTypeFactory.create(fill_worktime=True) + absence_type = absence_type_factory.create(fill_worktime=True) - ReportFactory.create(user=user, date=date, duration=datetime.timedelta(hours=5)) + report_factory.create(user=user, date=date, duration=datetime.timedelta(hours=5)) data = { "data": { @@ -280,7 +288,12 @@ def test_absence_fill_worktime(auth_client): assert json["data"]["attributes"]["duration"] == "03:00:00" -def test_absence_fill_worktime_reported_time_to_long(auth_client): +def test_absence_fill_worktime_reported_time_to_long( + auth_client, + employment_factory, + absence_type_factory, + report_factory, +): """ Verify absence fill worktime is zero when reported time is too long. @@ -288,12 +301,12 @@ def test_absence_fill_worktime_reported_time_to_long(auth_client): """ date = datetime.date(2017, 5, 10) user = auth_client.user - EmploymentFactory.create( + employment_factory.create( user=user, start_date=date, worktime_per_day=datetime.timedelta(hours=8) ) - absence_type = AbsenceTypeFactory.create(fill_worktime=True) + absence_type = absence_type_factory.create(fill_worktime=True) - ReportFactory.create( + report_factory.create( user=user, date=date, duration=datetime.timedelta(hours=8, minutes=30) ) @@ -319,12 +332,15 @@ def test_absence_fill_worktime_reported_time_to_long(auth_client): assert json["data"]["attributes"]["duration"] == "00:00:00" -def test_absence_weekend(auth_client): +def test_absence_weekend( + auth_client, + absence_type, + employment_factory, +): """Should not be able to create an absence on a weekend.""" date = datetime.date(2017, 5, 14) user = auth_client.user - absence_type = AbsenceTypeFactory.create() - EmploymentFactory.create( + employment_factory.create( user=user, start_date=date, worktime_per_day=datetime.timedelta(hours=8) ) @@ -347,15 +363,16 @@ def test_absence_weekend(auth_client): assert response.status_code == status.HTTP_400_BAD_REQUEST -def test_absence_public_holiday(auth_client): +def test_absence_public_holiday( + auth_client, absence_type, employment_factory, public_holiday_factory +): """Should not be able to create an absence on a public holiday.""" date = datetime.date(2017, 5, 16) user = auth_client.user - absence_type = AbsenceTypeFactory.create() - employment = EmploymentFactory.create( + employment = employment_factory.create( user=user, start_date=date, worktime_per_day=datetime.timedelta(hours=8) ) - PublicHolidayFactory.create(location=employment.location, date=date) + public_holiday_factory.create(location=employment.location, date=date) data = { "data": { @@ -376,9 +393,8 @@ def test_absence_public_holiday(auth_client): assert response.status_code == status.HTTP_400_BAD_REQUEST -def test_absence_create_unemployed(auth_client): +def test_absence_create_unemployed(auth_client, absence_type): """Test creation of absence fails on unemployed day.""" - absence_type = AbsenceTypeFactory.create() data = { "data": { @@ -399,9 +415,9 @@ def test_absence_create_unemployed(auth_client): assert response.status_code == status.HTTP_403_FORBIDDEN -def test_absence_detail_unemployed(internal_employee_client): +def test_absence_detail_unemployed(internal_employee_client, absence_factory): """Test creation of absence fails on unemployed day.""" - absence = AbsenceFactory.create(user=internal_employee_client.user) + absence = absence_factory.create(user=internal_employee_client.user) url = reverse("absence-detail", args=[absence.id]) diff --git a/timed/tracking/tests/test_activity.py b/timed/tracking/tests/test_activity.py index 8bd11fe6..a3dcd021 100644 --- a/timed/tracking/tests/test_activity.py +++ b/timed/tracking/tests/test_activity.py @@ -4,12 +4,9 @@ from django.urls import reverse from rest_framework import status -from timed.employment.factories import EmploymentFactory -from timed.tracking.factories import ActivityFactory - -def test_activity_list(internal_employee_client): - activity = ActivityFactory.create(user=internal_employee_client.user) +def test_activity_list(internal_employee_client, activity_factory): + activity = activity_factory.create(user=internal_employee_client.user) url = reverse("activity-list") response = internal_employee_client.get(url) @@ -20,8 +17,8 @@ def test_activity_list(internal_employee_client): assert json["data"][0]["id"] == str(activity.id) -def test_activity_detail(internal_employee_client): - activity = ActivityFactory.create(user=internal_employee_client.user) +def test_activity_detail(internal_employee_client, activity_factory): + activity = activity_factory.create(user=internal_employee_client.user) url = reverse("activity-detail", args=[activity.id]) @@ -43,10 +40,12 @@ def test_activity_detail(internal_employee_client): (False, True, False, status.HTTP_201_CREATED), ], ) -def test_activity_create(auth_client, is_external, task_assignee, expected): +def test_activity_create( + auth_client, is_external, task_assignee, expected, employment_factory +): """Should create a new activity and automatically set the user.""" user = auth_client.user - employment = EmploymentFactory(user=user) + employment = employment_factory(user=user) if is_external: employment.is_external = True @@ -79,9 +78,13 @@ def test_activity_create(auth_client, is_external, task_assignee, expected): assert int(json["data"]["relationships"]["user"]["data"]["id"]) == int(user.id) -def test_activity_create_no_task_external_employee(auth_client, task_assignee): +def test_activity_create_no_task_external_employee( + auth_client, + task_assignee, + employment_factory, +): user = auth_client.user - EmploymentFactory(user=user) + employment_factory(user=user) task_assignee.user = user task_assignee.save() @@ -120,12 +123,19 @@ def test_activity_create_no_task_external_employee(auth_client, task_assignee): (False, True, False, status.HTTP_200_OK), ], ) -def test_activity_update(auth_client, is_external, task_assignee, expected): +def test_activity_update( + auth_client, + is_external, + task_assignee, + expected, + activity_factory, + employment_factory, +): user = auth_client.user - activity = ActivityFactory.create(user=user, task=task_assignee.task) + activity = activity_factory.create(user=user, task=task_assignee.task) task_assignee.user = user task_assignee.save() - employment = EmploymentFactory(user=user) + employment = employment_factory(user=user) if is_external: employment.is_external = True @@ -166,13 +176,20 @@ def test_activity_update(auth_client, is_external, task_assignee, expected): (False, True, False, status.HTTP_204_NO_CONTENT), ], ) -def test_activity_delete(auth_client, is_external, task_assignee, expected): +def test_activity_delete( + auth_client, + is_external, + task_assignee, + expected, + activity_factory, + employment_factory, +): user = auth_client.user task_assignee.user = user task_assignee.save() - activity = ActivityFactory.create(user=user, task=task_assignee.task) + activity = activity_factory.create(user=user, task=task_assignee.task) - employment = EmploymentFactory(user=user) + employment = employment_factory(user=user) if is_external: employment.is_external = True @@ -184,10 +201,10 @@ def test_activity_delete(auth_client, is_external, task_assignee, expected): assert response.status_code == expected -def test_activity_list_filter_active(internal_employee_client): +def test_activity_list_filter_active(internal_employee_client, activity_factory): user = internal_employee_client.user - activity1 = ActivityFactory.create(user=user) - activity2 = ActivityFactory.create(user=user, to_time=None, task=activity1.task) + activity1 = activity_factory.create(user=user) + activity2 = activity_factory.create(user=user, to_time=None, task=activity1.task) url = reverse("activity-list") @@ -198,11 +215,11 @@ def test_activity_list_filter_active(internal_employee_client): assert json["data"][0]["id"] == str(activity2.id) -def test_activity_list_filter_day(internal_employee_client): +def test_activity_list_filter_day(internal_employee_client, activity_factory): user = internal_employee_client.user day = date(2016, 2, 2) - ActivityFactory.create(date=day - timedelta(days=1), user=user) - activity = ActivityFactory.create(date=day, user=user) + activity_factory.create(date=day - timedelta(days=1), user=user) + activity = activity_factory.create(date=day, user=user) url = reverse("activity-list") response = internal_employee_client.get(url, data={"day": day.strftime("%Y-%m-%d")}) @@ -236,9 +253,9 @@ def test_activity_create_no_task(internal_employee_client): assert json["data"]["relationships"]["task"]["data"] is None -def test_activity_active_unique(internal_employee_client): +def test_activity_active_unique(internal_employee_client, activity_factory): """Should not be able to have two active blocks.""" - ActivityFactory.create(user=internal_employee_client.user, to_time=None) + activity_factory.create(user=internal_employee_client.user, to_time=None) data = { "data": { @@ -261,9 +278,9 @@ def test_activity_active_unique(internal_employee_client): assert json["errors"][0]["detail"] == ("A user can only have one active activity") -def test_activity_to_before_from(internal_employee_client): +def test_activity_to_before_from(internal_employee_client, activity_factory): """Test that to is not before from.""" - activity = ActivityFactory.create( + activity = activity_factory.create( user=internal_employee_client.user, from_time=time(7, 30), to_time=None ) @@ -286,9 +303,9 @@ def test_activity_to_before_from(internal_employee_client): ) -def test_activity_not_editable(internal_employee_client): +def test_activity_not_editable(internal_employee_client, activity_factory): """Test that transferred activities are read only.""" - activity = ActivityFactory.create( + activity = activity_factory.create( user=internal_employee_client.user, transferred=True ) @@ -305,9 +322,9 @@ def test_activity_not_editable(internal_employee_client): assert response.status_code == status.HTTP_403_FORBIDDEN -def test_activity_retrievable_not_editable(internal_employee_client): +def test_activity_retrievable_not_editable(internal_employee_client, activity_factory): """Test that transferred activities are still retrievable.""" - activity = ActivityFactory.create( + activity = activity_factory.create( user=internal_employee_client.user, transferred=True ) @@ -317,8 +334,8 @@ def test_activity_retrievable_not_editable(internal_employee_client): assert response.status_code == status.HTTP_200_OK -def test_activity_active_update(internal_employee_client): - activity = ActivityFactory.create(user=internal_employee_client.user, to_time=None) +def test_activity_active_update(internal_employee_client, activity_factory): + activity = activity_factory.create(user=internal_employee_client.user, to_time=None) data = { "data": { diff --git a/timed/tracking/tests/test_attendance.py b/timed/tracking/tests/test_attendance.py index bd0de250..b43c174e 100644 --- a/timed/tracking/tests/test_attendance.py +++ b/timed/tracking/tests/test_attendance.py @@ -2,12 +2,10 @@ from django.urls import reverse from rest_framework import status -from timed.tracking.factories import AttendanceFactory - -def test_attendance_list(internal_employee_client): - AttendanceFactory.create() - attendance = AttendanceFactory.create(user=internal_employee_client.user) +def test_attendance_list(internal_employee_client, attendance_factory): + attendance_factory.create() + attendance = attendance_factory.create(user=internal_employee_client.user) url = reverse("attendance-list") response = internal_employee_client.get(url) @@ -18,8 +16,8 @@ def test_attendance_list(internal_employee_client): assert json["data"][0]["id"] == str(attendance.id) -def test_attendance_detail(internal_employee_client): - attendance = AttendanceFactory.create(user=internal_employee_client.user) +def test_attendance_detail(internal_employee_client, attendance_factory): + attendance = attendance_factory.create(user=internal_employee_client.user) url = reverse("attendance-detail", args=[attendance.id]) response = internal_employee_client.get(url) @@ -69,9 +67,9 @@ def test_attendance_create( assert json["data"]["relationships"]["user"]["data"]["id"] == str(user.id) -def test_attendance_update(internal_employee_client): +def test_attendance_update(internal_employee_client, attendance_factory): user = internal_employee_client.user - attendance = AttendanceFactory.create(user=user) + attendance = attendance_factory.create(user=user) data = { "data": { @@ -93,8 +91,8 @@ def test_attendance_update(internal_employee_client): ) -def test_attendance_delete(internal_employee_client): - attendance = AttendanceFactory.create(user=internal_employee_client.user) +def test_attendance_delete(internal_employee_client, attendance_factory): + attendance = attendance_factory.create(user=internal_employee_client.user) url = reverse("attendance-detail", args=[attendance.id]) diff --git a/timed/tracking/tests/test_report.py b/timed/tracking/tests/test_report.py index d30f69c7..bfd08605 100644 --- a/timed/tracking/tests/test_report.py +++ b/timed/tracking/tests/test_report.py @@ -8,14 +8,6 @@ from django.utils.duration import duration_string from rest_framework import status -from timed.employment.factories import EmploymentFactory, UserFactory -from timed.projects.factories import ( - CustomerAssigneeFactory, - ProjectAssigneeFactory, - TaskAssigneeFactory, - TaskFactory, -) - def test_report_list( internal_employee_client, @@ -272,20 +264,24 @@ def test_report_list_filter_id_empty( def test_report_list_filter_reviewer( internal_employee_client, report_factory, + project_assignee_factory, + task_factory, + user_factory, + task_assignee_factory, ): user = internal_employee_client.user report = report_factory.create(user=user) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) # add new task to the project - task2 = TaskFactory.create(project=report.task.project) + task2 = task_factory.create(project=report.task.project) report_factory.create(user=user, task=task2) # add task assignee with reviewer role to the new task - user2 = UserFactory.create() - TaskAssigneeFactory.create(user=user2, task=task2, is_reviewer=True) + user2 = user_factory.create() + task_assignee_factory.create(user=user2, task=task2, is_reviewer=True) url = reverse("report-list") @@ -348,7 +344,10 @@ def test_report_list_filter_not_editable_owner( def test_report_list_filter_editable_reviewer( - internal_employee_client, report_factory, user_factory + internal_employee_client, + report_factory, + user_factory, + project_assignee_factory, ): user = internal_employee_client.user # not editable report @@ -361,16 +360,16 @@ def test_report_list_filter_editable_reviewer( # reviewers and report is created by current user report = report_factory.create(user=user) other_user = user_factory.create() - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=other_user, project=report.task.project, is_reviewer=True ) # 3rd case: report by other user and current user # is the reviewer reviewer_report = report_factory.create() - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=reviewer_report.task.project, is_reviewer=True ) @@ -382,8 +381,10 @@ def test_report_list_filter_editable_reviewer( assert len(json["data"]) == 3 -def test_report_list_filter_editable_superuser(superadmin_client, report_factory): - EmploymentFactory.create(user=superadmin_client.user) +def test_report_list_filter_editable_superuser( + superadmin_client, report_factory, employment_factory +): + employment_factory.create(user=superadmin_client.user) report = report_factory.create() url = reverse("report-list") @@ -395,8 +396,10 @@ def test_report_list_filter_editable_superuser(superadmin_client, report_factory assert json["data"][0]["id"] == str(report.id) -def test_report_list_filter_not_editable_superuser(superadmin_client, report_factory): - EmploymentFactory.create(user=superadmin_client.user) +def test_report_list_filter_not_editable_superuser( + superadmin_client, report_factory, employment_factory +): + employment_factory.create(user=superadmin_client.user) report_factory.create() url = reverse("report-list") @@ -494,7 +497,9 @@ def test_report_detail( (False, False, True, False, status.HTTP_201_CREATED), ], ) -def test_report_create(auth_client, task_factory, task_assignee, is_external, expected): +def test_report_create( + auth_client, task_factory, task_assignee, is_external, expected, employment_factory +): """Should create a new report and automatically set the user.""" user = auth_client.user task = task_factory.create() @@ -502,10 +507,7 @@ def test_report_create(auth_client, task_factory, task_assignee, is_external, ex task_assignee.task = task task_assignee.save() - if is_external: - EmploymentFactory.create(user=user, is_external=True) - else: - EmploymentFactory.create(user=user, is_external=False) + employment_factory.create(user=user, is_external=is_external) data = { "data": { @@ -611,9 +613,11 @@ def test_report_update_bulk_verify_non_reviewer( assert response.status_code == status.HTTP_400_BAD_REQUEST -def test_report_update_bulk_verify_superuser(superadmin_client, report_factory): +def test_report_update_bulk_verify_superuser( + superadmin_client, report_factory, employment_factory +): user = superadmin_client.user - EmploymentFactory.create(user=user) + employment_factory.create(user=user) report = report_factory.create(user=user) url = reverse("report-bulk") @@ -630,12 +634,11 @@ def test_report_update_bulk_verify_superuser(superadmin_client, report_factory): def test_report_update_bulk_verify_reviewer( - internal_employee_client, - report_factory, + internal_employee_client, report_factory, project_assignee_factory ): user = internal_employee_client.user report = report_factory.create(user=user) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) @@ -659,9 +662,11 @@ def test_report_update_bulk_verify_reviewer( assert report.comment == "some comment" -def test_report_update_bulk_reset_verify(superadmin_client, report_factory): +def test_report_update_bulk_reset_verify( + superadmin_client, report_factory, employment_factory +): user = superadmin_client.user - EmploymentFactory.create(user=user) + employment_factory.create(user=user) report = report_factory.create(verified_by=user) url = reverse("report-bulk") @@ -760,12 +765,11 @@ def test_report_update_owner( def test_report_update_date_reviewer( - internal_employee_client, - report_factory, + internal_employee_client, report_factory, project_assignee_factory ): user = internal_employee_client.user report = report_factory.create() - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) @@ -784,12 +788,11 @@ def test_report_update_date_reviewer( def test_report_update_duration_reviewer( - internal_employee_client, - report_factory, + internal_employee_client, report_factory, project_assignee_factory ): user = internal_employee_client.user report = report_factory.create(duration=timedelta(hours=2)) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) @@ -827,12 +830,11 @@ def test_report_update_by_user( def test_report_update_verified_and_review_reviewer( - internal_employee_client, - report_factory, + internal_employee_client, report_factory, project_assignee_factory ): user = internal_employee_client.user report = report_factory.create(duration=timedelta(hours=2)) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) @@ -876,13 +878,11 @@ def test_report_set_verified_by_user( def test_report_update_reviewer( - internal_employee_client, - report_factory, - mailoutbox, + internal_employee_client, report_factory, mailoutbox, project_assignee_factory ): user = internal_employee_client.user report = report_factory.create(user=user) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) @@ -926,9 +926,11 @@ def test_report_update_supervisor( assert response.status_code == status.HTTP_200_OK -def test_report_verify_other_user(superadmin_client, report_factory, user_factory): +def test_report_verify_other_user( + superadmin_client, report_factory, user_factory, employment_factory +): """Verify that superuser may not verify to other user.""" - EmploymentFactory.create(user=superadmin_client.user) + employment_factory.create(user=superadmin_client.user) user = user_factory.create() report = report_factory.create() @@ -950,12 +952,14 @@ def test_report_verify_other_user(superadmin_client, report_factory, user_factor def test_report_reset_verified_by_reviewer( internal_employee_client, report_factory, + user_factory, + project_assignee_factory, ): """Test that reviewer may not change verified report.""" user = internal_employee_client.user - reviewer = UserFactory.create() + reviewer = user_factory.create() report = report_factory.create(user=user, verified_by=reviewer) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=reviewer, project=report.task.project, is_reviewer=True ) @@ -977,13 +981,12 @@ def test_report_reset_verified_by_reviewer( def test_report_reset_verified_and_billed_by_reviewer( - internal_employee_client, - report_factory, + internal_employee_client, report_factory, project_assignee_factory ): """Test that reviewer may not change verified and billed report.""" user = internal_employee_client.user report = report_factory.create(user=user, verified_by=user) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) # Billed is not set on create because the factory doesnt seem to work with that @@ -1029,7 +1032,14 @@ def test_report_reset_verified_and_billed_by_reviewer( ], ) def test_report_delete_own_report( - auth_client, report_factory, task_assignee, is_external, verified, expected + auth_client, + report_factory, + task_assignee, + is_external, + verified, + expected, + user_factory, + employment_factory, ): user = auth_client.user task_assignee.user = user @@ -1037,13 +1047,10 @@ def test_report_delete_own_report( report = report_factory.create(user=user, task=task_assignee.task) if verified: - report.verified_by = UserFactory.create() + report.verified_by = user_factory.create() report.save() - if is_external: - EmploymentFactory.create(user=user, is_external=True) - else: - EmploymentFactory.create(user=user, is_external=False) + employment_factory.create(user=user, is_external=is_external) url = reverse("report-detail", args=[report.id]) response = auth_client.delete(url) @@ -1074,23 +1081,26 @@ def test_report_delete_own_report( ], ) def test_report_delete_not_report_owner( - auth_client, report_factory, task_assignee, is_external, verified + auth_client, + report_factory, + task_assignee, + is_external, + verified, + user_factory, + employment_factory, ): user = auth_client.user task_assignee.user = user task_assignee.save() - user2 = UserFactory.create() + user2 = user_factory.create() report = report_factory.create(user=user2, task=task_assignee.task) if verified: - report.verified_by = UserFactory.create() + report.verified_by = user_factory.create() report.save() - if is_external: - EmploymentFactory.create(user=user, is_external=True) - else: - EmploymentFactory.create(user=user, is_external=False) + employment_factory.create(user=user, is_external=is_external) url = reverse("report-detail", args=[report.id]) response = auth_client.delete(url) @@ -1123,8 +1133,8 @@ def test_report_round_duration(report_factory): assert duration_string(report.duration) == "02:00:00" -def test_report_list_no_result(admin_client): - EmploymentFactory.create(user=admin_client.user) +def test_report_list_no_result(admin_client, employment_factory): + employment_factory.create(user=admin_client.user) url = reverse("report-list") res = admin_client.get(url) @@ -1133,9 +1143,9 @@ def test_report_list_no_result(admin_client): assert json["meta"]["total-time"] == "00:00:00" -def test_report_delete_superuser(superadmin_client, report_factory): +def test_report_delete_superuser(superadmin_client, report_factory, employment_factory): """Test that superuser may not delete reports of other users.""" - EmploymentFactory.create(user=superadmin_client.user) + employment_factory.create(user=superadmin_client.user) report = report_factory.create() url = reverse("report-detail", args=[report.id]) @@ -1273,9 +1283,10 @@ def test_report_update_bulk_verify_reviewer_multiple_notify( report_factory, user_factory, mailoutbox, + project_assignee_factory, ): reviewer = internal_employee_client.user - ProjectAssigneeFactory.create(user=reviewer, project=project, is_reviewer=True) + project_assignee_factory.create(user=reviewer, project=project, is_reviewer=True) user1, user2, user3 = user_factory.create_batch(3) report1_1 = report_factory(user=user1, task=task) @@ -1338,6 +1349,8 @@ def test_report_update_reviewer_notify( different_attributes, verified, expected, + project_assignee_factory, + task_assignee_factory, ): reviewer = internal_employee_client.user user = user_factory() @@ -1346,16 +1359,16 @@ def test_report_update_reviewer_notify( report = report_factory(user=reviewer, review=True) else: report = report_factory(user=user, review=True) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=reviewer, project=report.task.project, is_reviewer=True ) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) new_task = task_factory(project=report.task.project) task = report.task - TaskAssigneeFactory.create(user=user, is_resource=True, task=task) - TaskAssigneeFactory.create(user=reviewer, is_reviewer=True, task=task) + task_assignee_factory.create(user=user, is_resource=True, task=task) + task_assignee_factory.create(user=reviewer, is_reviewer=True, task=task) data = { "data": { @@ -1399,10 +1412,11 @@ def test_report_notify_rendering( task_factory, mailoutbox, snapshot, + project_assignee_factory, ): reviewer = internal_employee_client.user user = user_factory() - ProjectAssigneeFactory.create(user=reviewer, project=project, is_reviewer=True) + project_assignee_factory.create(user=reviewer, project=project, is_reviewer=True) task1, task2, task3 = task_factory.create_batch(3, project=project) report1 = report_factory( @@ -1445,9 +1459,9 @@ def test_report_notify_rendering( ("report__review", "needs_review"), [(True, False), (False, True), (True, True)] ) def test_report_update_bulk_review_and_verified( - superadmin_client, report, needs_review + superadmin_client, report, needs_review, employment_factory ): - EmploymentFactory.create(user=superadmin_client.user) + employment_factory.create(user=superadmin_client.user) data = { "data": {"type": "report-bulks", "id": None, "attributes": {"verified": True}} } @@ -1477,12 +1491,11 @@ def test_report_update_bulk_bill_non_reviewer( def test_report_update_bulk_bill_reviewer( - internal_employee_client, - report_factory, + internal_employee_client, report_factory, project_assignee_factory ): user = internal_employee_client.user report = report_factory.create(user=user) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) @@ -1621,10 +1634,12 @@ def test_report_update_billed(internal_employee_client, report_factory, task): assert not report.billed -def test_report_update_bulk_billed(internal_employee_client, report_factory, task): +def test_report_update_bulk_billed( + internal_employee_client, report_factory, task, project_assignee_factory +): user = internal_employee_client.user report = report_factory.create(user=user) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, project=report.task.project, is_reviewer=True ) task.project.billed = True @@ -1652,10 +1667,12 @@ def test_report_update_bulk_billed(internal_employee_client, report_factory, tas assert report.billed -def test_report_list_external_employee(external_employee_client, report_factory): +def test_report_list_external_employee( + external_employee_client, report_factory, task_assignee_factory +): user = external_employee_client.user report = report_factory.create(user=user, duration=timedelta(hours=1)) - TaskAssigneeFactory.create(user=user, task=report.task, is_resource=True) + task_assignee_factory.create(user=user, task=report.task, is_resource=True) report_factory.create_batch(4) url = reverse("report-list") @@ -1684,12 +1701,17 @@ def test_report_list_external_employee(external_employee_client, report_factory) [(True, 1, status.HTTP_200_OK), (False, 0, status.HTTP_403_FORBIDDEN)], ) def test_report_list_no_employment( - auth_client, report_factory, is_assigned, expected, status_code + auth_client, + report_factory, + is_assigned, + expected, + status_code, + customer_assignee_factory, ): user = auth_client.user report = report_factory.create(user=user, duration=timedelta(hours=1)) if is_assigned: - CustomerAssigneeFactory.create( + customer_assignee_factory.create( user=user, is_customer=True, customer=report.task.project.customer ) report_factory.create_batch(4) @@ -1724,12 +1746,14 @@ def test_report_reject( status_code, mail_count, mailoutbox, + user_factory, + project_assignee_factory, ): user = internal_employee_client.user - user2 = UserFactory.create() + user2 = user_factory.create() report = report_factory.create(user=user2 if not report_owner else user) if reviewer: - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=user, is_reviewer=True, project=report.task.project ) @@ -1785,9 +1809,10 @@ def test_report_reject_multiple_notify( report_factory, user_factory, mailoutbox, + project_assignee_factory, ): reviewer = internal_employee_client.user - ProjectAssigneeFactory.create(user=reviewer, project=project, is_reviewer=True) + project_assignee_factory.create(user=reviewer, project=project, is_reviewer=True) user1, user2, user3 = user_factory.create_batch(3) report1_1 = report_factory(user=user1, task=task) @@ -1848,14 +1873,18 @@ def test_report_automatic_unreject(internal_employee_client, report_factory, tas def test_report_bulk_automatic_unreject( - internal_employee_client, user_factory, report_factory, task + internal_employee_client, + user_factory, + report_factory, + task, + project_assignee_factory, ): reviewer = internal_employee_client.user user = user_factory.create() report = report_factory.create(user=user, rejected=True) - ProjectAssigneeFactory.create( + project_assignee_factory.create( user=reviewer, project=report.task.project, is_reviewer=True ) @@ -1897,9 +1926,10 @@ def test_report_set_remaining_effort( expected, is_superuser, report_factory, + employment_factory, ): user = auth_client.user - EmploymentFactory.create(user=user, is_external=is_external) + employment_factory.create(user=user, is_external=is_external) report = report_factory.create(user=user) if remaining_effort_active: @@ -1979,12 +2009,11 @@ def test_report_create_remaining_effort( def test_report_remaining_effort_total( - internal_employee_client, - report_factory, + internal_employee_client, report_factory, task_factory ): user = internal_employee_client.user report = report_factory.create(user=user) - task_2 = TaskFactory.create(project=report.task.project) + task_2 = task_factory.create(project=report.task.project) report_2 = report_factory.create(user=user, task=task_2) report.task.project.remaining_effort_tracking = True report.task.project.save()