diff --git a/timed/conftest.py b/timed/conftest.py index 725c8223..31404a52 100644 --- a/timed/conftest.py +++ b/timed/conftest.py @@ -62,7 +62,7 @@ def superadmin_user(db): @pytest.fixture -def external_employee(db): +def external_employee(db, employment_factory): user = get_user_model().objects.create_user( username="user", password="123qweasd", @@ -71,12 +71,12 @@ def external_employee(db): is_superuser=False, is_staff=False, ) - employment_factories.EmploymentFactory.create(user=user, is_external=True) + employment_factory(user=user, is_external=True) return user @pytest.fixture -def internal_employee(db): +def internal_employee(db, employment_factory): user = get_user_model().objects.create_user( username="user", password="123qweasd", @@ -86,7 +86,7 @@ def internal_employee(db): is_superuser=False, is_staff=False, ) - employment_factories.EmploymentFactory.create(user=user, is_external=False) + employment_factory(user=user, is_external=False) return user @@ -146,7 +146,11 @@ def _autoclear_cache(): def setup_customer_and_employment_status( - user, is_assignee, is_customer, is_employed, is_external + user, + is_assignee, + is_customer, + is_employed, + is_external, ): """ Set up customer and employment status. @@ -157,11 +161,11 @@ def setup_customer_and_employment_status( assignee = None employment = None if is_assignee: - assignee = projects_factories.CustomerAssigneeFactory.create( + assignee = projects_factories.CustomerAssigneeFactory( user=user, is_customer=is_customer ) if is_employed: - employment = employment_factories.EmploymentFactory.create( + employment = employment_factories.EmploymentFactory( user=user, is_external=is_external ) return assignee, employment diff --git a/timed/employment/tests/test_absence_balance.py b/timed/employment/tests/test_absence_balance.py index df776043..03998fb7 100644 --- a/timed/employment/tests/test_absence_balance.py +++ b/timed/employment/tests/test_absence_balance.py @@ -3,30 +3,31 @@ from django.urls import reverse from rest_framework import status -from timed.employment.factories import ( - AbsenceCreditFactory, - AbsenceTypeFactory, - EmploymentFactory, - UserFactory, -) -from timed.tracking.factories import AbsenceFactory, ReportFactory - -def test_absence_balance_full_day(auth_client, django_assert_num_queries): +def test_absence_balance_full_day( + auth_client, + django_assert_num_queries, + absence_credit_factory, + absence_factory, + absence_type_factory, + employment_factory, +): day = date(2017, 2, 28) user = auth_client.user - EmploymentFactory.create(user=user, start_date=day) - absence_type = AbsenceTypeFactory.create() + employment_factory.create(user=user, start_date=day) + absence_type = absence_type_factory.create() - AbsenceCreditFactory.create(date=day, user=user, days=5, absence_type=absence_type) + absence_credit_factory.create( + date=day, user=user, days=5, absence_type=absence_type + ) # credit on different user, may not show up - AbsenceCreditFactory.create(date=date.today(), absence_type=absence_type) + absence_credit_factory.create(date=date.today(), absence_type=absence_type) - AbsenceFactory.create(date=day, user=user, absence_type=absence_type) + absence_factory.create(date=day, user=user, absence_type=absence_type) - AbsenceFactory.create( + absence_factory.create( date=day - timedelta(days=1), user=user, absence_type=absence_type ) @@ -56,25 +57,33 @@ def test_absence_balance_full_day(auth_client, django_assert_num_queries): assert len(json["included"]) == 2 -def test_absence_balance_fill_worktime(auth_client, django_assert_num_queries): +def test_absence_balance_fill_worktime( + auth_client, + django_assert_num_queries, + absence_factory, + absence_type_factory, + employment_factory, + report_factory, + user_factory, +): day = date(2017, 2, 28) - user = UserFactory.create() + user = user_factory.create() user.supervisors.add(auth_client.user) - EmploymentFactory.create( + employment_factory.create( user=user, start_date=day, worktime_per_day=timedelta(hours=5) ) - absence_type = AbsenceTypeFactory.create(fill_worktime=True) + absence_type = absence_type_factory.create(fill_worktime=True) - ReportFactory.create( + report_factory.create( user=user, date=day + timedelta(days=1), duration=timedelta(hours=4) ) - AbsenceFactory.create( + absence_factory.create( date=day + timedelta(days=1), user=user, absence_type=absence_type ) - AbsenceFactory.create(date=day, user=user, absence_type=absence_type) + absence_factory.create(date=day, user=user, absence_type=absence_type) url = reverse("absence-balance-list") with django_assert_num_queries(11): @@ -100,9 +109,9 @@ def test_absence_balance_fill_worktime(auth_client, django_assert_num_queries): assert entry["attributes"]["used-duration"] == "06:00:00" -def test_absence_balance_detail(auth_client): +def test_absence_balance_detail(auth_client, absence_type_factory): user = auth_client.user - absence_type = AbsenceTypeFactory.create() + absence_type = absence_type_factory.create() url = reverse( "absence-balance-detail", args=["{0}_{1}_2017-03-01".format(user.id, absence_type.id)], @@ -120,10 +129,12 @@ def test_absence_balance_detail(auth_client): assert entry["attributes"]["used-duration"] is None -def test_absence_balance_list_none_supervisee(auth_client): +def test_absence_balance_list_none_supervisee( + auth_client, absence_type_factory, user_factory +): url = reverse("absence-balance-list") - AbsenceTypeFactory.create() - unrelated_user = UserFactory.create() + absence_type_factory.create() + unrelated_user = user_factory.create() result = auth_client.get( url, data={"user": unrelated_user.id, "date": "2017-01-03"} @@ -132,10 +143,12 @@ def test_absence_balance_list_none_supervisee(auth_client): assert len(result.json()["data"]) == 0 -def test_absence_balance_detail_none_supervisee(auth_client): +def test_absence_balance_detail_none_supervisee( + auth_client, absence_type_factory, user_factory +): url = reverse("absence-balance-list") - absence_type = AbsenceTypeFactory.create() - unrelated_user = UserFactory.create() + absence_type = absence_type_factory.create() + unrelated_user = user_factory.create() url = reverse( "absence-balance-detail", diff --git a/timed/employment/tests/test_absence_credit.py b/timed/employment/tests/test_absence_credit.py index b494f7c2..f0621dba 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,8 +9,8 @@ def test_absence_credit_create_authenticated(auth_client): assert result.status_code == status.HTTP_403_FORBIDDEN -def test_absence_credit_create_superuser(superadmin_client): - absence_type = AbsenceTypeFactory.create() +def test_absence_credit_create_superuser(superadmin_client, absence_type_factory): + absence_type = absence_type_factory.create() url = reverse("absence-credit-list") @@ -38,9 +32,9 @@ def test_absence_credit_create_superuser(superadmin_client): assert result.status_code == status.HTTP_201_CREATED -def test_absence_credit_get_authenticated(auth_client): - AbsenceCreditFactory.create_batch(2) - absence_credit = AbsenceCreditFactory.create(user=auth_client.user) +def test_absence_credit_get_authenticated(auth_client, absence_credit_factory): + absence_credit_factory.create_batch(2) + absence_credit = absence_credit_factory.create(user=auth_client.user) url = reverse("absence-credit-list") result = auth_client.get(url) @@ -50,9 +44,9 @@ def test_absence_credit_get_authenticated(auth_client): assert json["data"][0]["id"] == str(absence_credit.id) -def test_absence_credit_get_superuser(superadmin_client): - AbsenceCreditFactory.create_batch(2) - AbsenceCreditFactory.create(user=superadmin_client.user) +def test_absence_credit_get_superuser(superadmin_client, absence_credit_factory): + absence_credit_factory.create_batch(2) + absence_credit_factory.create(user=superadmin_client.user) url = reverse("absence-credit-list") result = superadmin_client.get(url) @@ -61,13 +55,15 @@ def test_absence_credit_get_superuser(superadmin_client): assert len(json["data"]) == 3 -def test_absence_credit_get_supervisor(auth_client): - user = UserFactory.create() +def test_absence_credit_get_supervisor( + auth_client, absence_credit_factory, user_factory +): + user = user_factory.create() auth_client.user.supervisees.add(user) - AbsenceCreditFactory.create_batch(1) - AbsenceCreditFactory.create(user=auth_client.user) - AbsenceCreditFactory.create(user=user) + absence_credit_factory.create_batch(1) + absence_credit_factory.create(user=auth_client.user) + absence_credit_factory.create(user=user) url = reverse("absence-credit-list") result = auth_client.get(url) diff --git a/timed/employment/tests/test_absence_type.py b/timed/employment/tests/test_absence_type.py index ad681122..51604c6e 100644 --- a/timed/employment/tests/test_absence_type.py +++ b/timed/employment/tests/test_absence_type.py @@ -3,7 +3,6 @@ from rest_framework import status from timed.conftest import setup_customer_and_employment_status -from timed.employment.factories import AbsenceTypeFactory, EmploymentFactory @pytest.mark.parametrize( @@ -17,7 +16,12 @@ ], ) def test_absence_type_list( - auth_client, is_employed, is_customer_assignee, is_customer, expected + auth_client, + is_employed, + is_customer_assignee, + is_customer, + expected, + absence_type_factory, ): setup_customer_and_employment_status( user=auth_client.user, @@ -26,7 +30,7 @@ def test_absence_type_list( is_employed=is_employed, is_external=False, ) - AbsenceTypeFactory.create_batch(2) + absence_type_factory.create_batch(2) url = reverse("absence-type-list") response = auth_client.get(url) @@ -36,9 +40,11 @@ def test_absence_type_list( assert len(json["data"]) == expected -def test_absence_type_list_filter_fill_worktime(internal_employee_client): - absence_type = AbsenceTypeFactory.create(fill_worktime=True) - AbsenceTypeFactory.create() +def test_absence_type_list_filter_fill_worktime( + internal_employee_client, absence_type_factory +): + absence_type = absence_type_factory.create(fill_worktime=True) + absence_type_factory.create() url = reverse("absence-type-list") @@ -57,10 +63,12 @@ def test_absence_type_list_filter_fill_worktime(internal_employee_client): (False, status.HTTP_404_NOT_FOUND), ], ) -def test_absence_type_detail(auth_client, is_employed, expected): - absence_type = AbsenceTypeFactory.create() +def test_absence_type_detail( + auth_client, is_employed, expected, absence_type_factory, employment_factory +): + absence_type = absence_type_factory.create() if is_employed: - EmploymentFactory.create(user=auth_client.user) + employment_factory.create(user=auth_client.user) url = reverse("absence-type-detail", args=[absence_type.id]) @@ -76,8 +84,8 @@ def test_absence_type_create(auth_client): assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED -def test_absence_type_update(auth_client): - absence_type = AbsenceTypeFactory.create() +def test_absence_type_update(auth_client, absence_type_factory): + absence_type = absence_type_factory.create() url = reverse("absence-type-detail", args=[absence_type.id]) @@ -85,8 +93,8 @@ def test_absence_type_update(auth_client): assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED -def test_absence_type_delete(auth_client): - absence_type = AbsenceTypeFactory.create() +def test_absence_type_delete(auth_client, absence_type_factory): + absence_type = absence_type_factory.create() url = reverse("absence-type-detail", args=[absence_type.id]) diff --git a/timed/employment/tests/test_employment.py b/timed/employment/tests/test_employment.py index 97ea8f73..24cd5578 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,9 @@ 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_factory): url = reverse("employment-list") - location = LocationFactory.create() + location = location_factory.create() data = { "data": { @@ -44,8 +41,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 +57,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 +75,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 +88,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 +100,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 +117,13 @@ 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, employment_factory, user_factory): + user = user_factory.create() 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") @@ -136,19 +133,19 @@ def test_employment_list_supervisor(auth_client): assert len(json["data"]) == 2 -def test_employment_unique_active(db): +def test_employment_unique_active(db, employment_factory, user_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) + user = user_factory.create() + 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): form.save() -def test_employment_start_before_end(db): - employment = EmploymentFactory.create() +def test_employment_start_before_end(db, employment_factory): + employment = employment_factory.create() form = EmploymentForm( {"start_date": date(2009, 1, 1), "end_date": date(2016, 1, 1)}, instance=employment, @@ -158,10 +155,10 @@ def test_employment_start_before_end(db): form.save() -def test_employment_get_at(db): +def test_employment_get_at(db, employment_factory, user_factory): """Should return the right employment on a date.""" - user = UserFactory.create() - employment = EmploymentFactory.create(user=user) + user = user_factory.create() + employment = employment_factory.create(user=user) assert Employment.objects.get_at(user, employment.start_date) == employment @@ -173,14 +170,20 @@ def test_employment_get_at(db): Employment.objects.get_at(user, employment.start_date + timedelta(days=21)) -def test_worktime_balance_partial(db): +def test_worktime_balance_partial( + db, + employment_factory, + overtime_credit_factory, + public_holiday_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.create( start_date=date(2010, 1, 1), end_date=None, worktime_per_day=timedelta(hours=8) ) user = employment.user @@ -190,14 +193,14 @@ def test_worktime_balance_partial(db): end = date(2017, 3, 26) # Overtime credit of 10.5 hours - factories.OvertimeCreditFactory.create( + overtime_credit_factory.create( 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.create(date=start, location=employment.location) # One public holiday on weekend - factories.PublicHolidayFactory.create( + public_holiday_factory.create( date=start + timedelta(days=1), location=employment.location ) # 5 workdays minus one holiday (32 hours) @@ -205,7 +208,7 @@ def test_worktime_balance_partial(db): # reported 2 days each 10 hours for day in range(3, 5): - ReportFactory.create( + report_factory.create( user=user, date=start + timedelta(days=day), duration=timedelta(hours=10) ) # 10 hours reported time + 10.5 overtime credit @@ -219,9 +222,15 @@ def test_worktime_balance_partial(db): assert balance == expected_balance -def test_worktime_balance_longer(db): +def test_worktime_balance_longer( + db, + 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.create( start_date=date(2017, 3, 21), end_date=date(2017, 3, 27), worktime_per_day=timedelta(hours=8), @@ -233,34 +242,34 @@ def test_worktime_balance_longer(db): end = date(2017, 12, 31) # Overtime credit of 10.5 hours before employment - factories.OvertimeCreditFactory.create( + overtime_credit_factory.create( user=user, date=start, duration=timedelta(hours=10, minutes=30) ) # Overtime credit of during employment - factories.OvertimeCreditFactory.create( + overtime_credit_factory.create( user=user, date=employment.start_date, duration=timedelta(hours=10, minutes=30) ) # One public holiday during employment - factories.PublicHolidayFactory.create( + public_holiday_factory.create( 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.create(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.create( 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.create( + 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 @@ -272,24 +281,22 @@ def test_worktime_balance_longer(db): assert balance == expected_balance -def test_employment_for_user(db): - user = factories.UserFactory.create() +def test_employment_for_user(db, user_factory, employment_factory): + user = user_factory.create() # 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 5685afda..efc7780d 100644 --- a/timed/employment/tests/test_location.py +++ b/timed/employment/tests/test_location.py @@ -3,7 +3,6 @@ from rest_framework import status from timed.conftest import setup_customer_and_employment_status -from timed.employment.factories import EmploymentFactory, LocationFactory @pytest.mark.parametrize( @@ -44,10 +43,12 @@ 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, employment_factory, location_factory +): + location = location_factory.create() if is_employed: - EmploymentFactory.create(user=auth_client.user) + employment_factory.create(user=auth_client.user) url = reverse("location-detail", args=[location.id]) @@ -62,18 +63,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..b764af6d 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.create(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.create(user=superadmin_client.user) url = reverse("overtime-credit-list") result = superadmin_client.get(url) @@ -54,13 +52,15 @@ 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, overtime_credit_factory, user_factory +): + user = user_factory.create() auth_client.user.supervisees.add(user) - OvertimeCreditFactory.create_batch(1) - OvertimeCreditFactory.create(user=auth_client.user) - OvertimeCreditFactory.create(user=user) + overtime_credit_factory.create_batch(1) + overtime_credit_factory.create(user=auth_client.user) + overtime_credit_factory.create(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 03325287..dbbb569d 100644 --- a/timed/employment/tests/test_public_holiday.py +++ b/timed/employment/tests/test_public_holiday.py @@ -5,7 +5,6 @@ from rest_framework import status from timed.conftest import setup_customer_and_employment_status -from timed.employment.factories import EmploymentFactory, PublicHolidayFactory @pytest.mark.parametrize( @@ -19,7 +18,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, + public_holiday_factory, ): setup_customer_and_employment_status( user=auth_client.user, @@ -28,7 +32,7 @@ def test_public_holiday_list( is_employed=is_employed, is_external=False, ) - PublicHolidayFactory.create() + public_holiday_factory.create() url = reverse("public-holiday-list") response = auth_client.get(url) @@ -45,10 +49,12 @@ 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, employment_factory, public_holiday_factory +): + public_holiday = public_holiday_factory.create() 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,8 +69,8 @@ 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_factory): + public_holiday = public_holiday_factory.create() url = reverse("public-holiday-detail", args=[public_holiday.id]) @@ -72,8 +78,8 @@ def test_public_holiday_update(auth_client): 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_factory): + public_holiday = public_holiday_factory.create() url = reverse("public-holiday-detail", args=[public_holiday.id]) @@ -81,9 +87,9 @@ def test_public_holiday_delete(auth_client): 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 136e62a2..2569e8f2 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") @@ -23,15 +11,16 @@ def test_user_list_unauthenticated(client): assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_user_update_unauthenticated(client, db): - user = UserFactory.create() +def test_user_update_unauthenticated(client, db, user): url = reverse("user-detail", args=[user.id]) response = client.patch(url) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_user_list(db, internal_employee_client, django_assert_num_queries): - UserFactory.create_batch(2) +def test_user_list( + db, internal_employee_client, django_assert_num_queries, user_factory +): + user_factory.create_batch(2) url = reverse("user-list") @@ -44,8 +33,8 @@ def test_user_list(db, 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") @@ -114,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) @@ -133,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, employment_factory, user): """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]) @@ -144,11 +131,12 @@ def test_user_delete_superuser(superadmin_client): assert response.status_code == status.HTTP_204_NO_CONTENT -def test_user_delete_with_reports_superuser(superadmin_client, db): +def test_user_delete_with_reports_superuser( + superadmin_client, db, employment_factory, report_factory, user +): """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]) @@ -156,11 +144,11 @@ def test_user_delete_with_reports_superuser(superadmin_client, db): 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() @@ -173,14 +161,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, absence_factory, absence_type_factory, employment_factory, user +): + 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) @@ -206,13 +197,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} @@ -221,22 +213,31 @@ 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, + project_assignee_factory, + project_factory, + user, + user_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) + project = project_factory.create() + 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) @@ -244,16 +245,17 @@ 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, project_assignee_factory, user +): """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.create(user=user, project=project, is_reviewer=True) res = internal_employee_client.get(url) assert res.json()["data"]["attributes"]["is-reviewer"] @@ -289,11 +291,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, + customer_assignee_factory, + user_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 4db21dde..6732d300 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.create( 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.create( 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.create( 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.create( 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.create( user=auth_client.user, date=date(2017, 2, 2), duration=timedelta(hours=10) ) - ReportFactory.create( + report_factory.create( user=auth_client.user, date=date(2017, 2, 3), duration=timedelta(hours=10) ) diff --git a/timed/notifications/factories.py b/timed/notifications/factories.py index 6cade3d2..6da82a42 100644 --- a/timed/notifications/factories.py +++ b/timed/notifications/factories.py @@ -1,9 +1,11 @@ from factory import Faker, SubFactory from factory.django import DjangoModelFactory +from pytest_factoryboy import register from timed.notifications.models import Notification +@register class NotificationFactory(DjangoModelFactory): project = SubFactory("timed.projects.factories.ProjectFactory") notification_type = Faker("word", ext_word_list=Notification.NOTIFICATION_TYPES) diff --git a/timed/notifications/tests/test_budget_check.py b/timed/notifications/tests/test_budget_check.py index 0b4b4569..f9b731a8 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 @@ -15,7 +14,13 @@ [(1, 0, 0), (3, 0, 0), (4, 30, 1), (8, 70, 2), (0, 0, 0)], ) def test_budget_check_1( - db, mocker, report_factory, duration, percentage_exceeded, notification_count + db, + mocker, + report_factory, + duration, + percentage_exceeded, + notification_count, + notification_factory, ): """Test budget check.""" redmine_instance = mocker.MagicMock() @@ -35,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 ) @@ -55,7 +60,9 @@ def test_budget_check_1( assert Notification.objects.all().count() == notification_count -def test_budget_check_skip_notification(db, capsys, mocker, report_factory): +def test_budget_check_skip_notification( + db, capsys, mocker, report_factory, notification_factory +): redmine_instance = mocker.MagicMock() issue = mocker.MagicMock() redmine_instance.issue.get.return_value = issue @@ -69,7 +76,7 @@ def test_budget_check_skip_notification(db, 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 d901bcd1..a5793101 100644 --- a/timed/notifications/tests/test_notify_changed_employments.py +++ b/timed/notifications/tests/test_notify_changed_employments.py @@ -2,21 +2,20 @@ from django.core.management import call_command -from timed.employment.factories import EmploymentFactory from timed.notifications.models import Notification -def test_notify_changed_employments(db, mailoutbox, freezer): +def test_notify_changed_employments(db, 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.create(end_date=date(2017, 10, 10), percentage=80) + new = employment_factory.create(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 b008997d..b9db7b09 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.freeze_time("2017-8-4") @@ -24,25 +16,35 @@ ("", "This is a test"), ], ) -def test_notify_reviewers_with_cc_and_message(db, mailoutbox, cc, message): +def test_notify_reviewers_with_cc_and_message( + db, + mailoutbox, + cc, + message, + project_factory, + user_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) # 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 ) @@ -66,16 +68,24 @@ def test_notify_reviewers_with_cc_and_message(db, mailoutbox, cc, message): @pytest.mark.freeze_time("2017-8-4") -def test_notify_reviewers(db, mailoutbox): +def test_notify_reviewers( + db, + mailoutbox, + project_factory, + project_assignee_factory, + task_factory, + report_factory, + user_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") @@ -92,23 +102,32 @@ def test_notify_reviewers(db, mailoutbox): @pytest.mark.freeze_time("2017-8-4") -def test_notify_reviewers_reviewer_hierarchy(db, mailoutbox): +def test_notify_reviewers_reviewer_hierarchy( + db, + mailoutbox, + project_factory, + task_factory, + project_assignee_factory, + task_assignee_factory, + report_factory, + user_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 e6c00174..3ee85199 100644 --- a/timed/notifications/tests/test_notify_supervisors_shorttime.py +++ b/timed/notifications/tests/test_notify_supervisors_shorttime.py @@ -4,22 +4,21 @@ 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.freeze_time("2017-7-27") -def test_notify_supervisors(db, mailoutbox): +def test_notify_supervisors( + db, 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, @@ -27,9 +26,9 @@ def test_notify_supervisors(db, 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) ) @@ -48,10 +47,10 @@ def test_notify_supervisors(db, mailoutbox): assert Notification.objects.count() == 1 -def test_notify_supervisors_no_employment(db, mailoutbox): +def test_notify_supervisors_no_employment(db, 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 f4e9fcb2..062ca76a 100644 --- a/timed/projects/tests/test_billing_type.py +++ b/timed/projects/tests/test_billing_type.py @@ -3,7 +3,7 @@ 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( @@ -31,6 +31,8 @@ def test_billing_type_list( customer_visible, expected, status_code, + customer_factory, + project_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -43,8 +45,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 08d6422e..a7e2c0d9 100644 --- a/timed/projects/tests/test_cost_center.py +++ b/timed/projects/tests/test_cost_center.py @@ -3,7 +3,6 @@ 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( @@ -17,10 +16,14 @@ ], ) 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, ): 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 6a901a80..84274778 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,13 @@ 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 73c7232d..905cff13 100644 --- a/timed/projects/tests/test_customer_assignee.py +++ b/timed/projects/tests/test_customer_assignee.py @@ -3,7 +3,6 @@ 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( @@ -20,9 +19,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, + customer_assignee_factory, ): - customer_assignee = CustomerAssigneeFactory.create() + customer_assignee = customer_assignee_factory.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 7ef69b31..2c4b4864 100644 --- a/timed/projects/tests/test_project.py +++ b/timed/projects/tests/test_project.py @@ -6,20 +6,12 @@ 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_factory): + project = project_factory.create(archived=False) + project_factory.create(archived=True) url = reverse("project-list") @@ -32,10 +24,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") @@ -118,10 +113,10 @@ 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) @@ -134,11 +129,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") @@ -155,8 +152,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") @@ -196,10 +193,17 @@ def test_project_update_billed_flag(internal_employee_client, 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 ) @@ -222,19 +226,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 6fc5eec2..8aba010e 100644 --- a/timed/projects/tests/test_project_assignee.py +++ b/timed/projects/tests/test_project_assignee.py @@ -3,7 +3,6 @@ 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( @@ -20,7 +19,13 @@ ], ) 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, + project_assignee, ): user = auth_client.user setup_customer_and_employment_status( @@ -30,7 +35,6 @@ def test_project_assignee_list( is_employed=is_employed, is_external=is_external, ) - project_assignee = ProjectAssigneeFactory.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 217d4bf9..dc3bbfa4 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) @@ -115,15 +108,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() @@ -150,12 +150,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]) @@ -188,9 +188,11 @@ 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) @@ -212,11 +214,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: @@ -232,8 +241,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 522350e2..b54e7a83 100644 --- a/timed/projects/tests/test_task_assignee.py +++ b/timed/projects/tests/test_task_assignee.py @@ -3,7 +3,6 @@ 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( @@ -20,7 +19,13 @@ ], ) 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, + task_assignee_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -30,7 +35,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 e3972cf7..8af24436 100644 --- a/timed/reports/tests/test_customer_statistic.py +++ b/timed/reports/tests/test_customer_statistic.py @@ -5,9 +5,6 @@ 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( @@ -28,6 +25,7 @@ def test_customer_statistic_list( expected, status_code, django_assert_num_queries, + report_factory, ): user = auth_client.user @@ -44,9 +42,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): @@ -92,7 +90,16 @@ 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): +def test_customer_statistic_filtered( + auth_client, + filter, + expected_result, + cost_center, + report_factory, + task_factory, + task_assignee_factory, + user, +): user = auth_client.user setup_customer_and_employment_status( user=user, @@ -102,14 +109,17 @@ def test_customer_statistic_filtered(auth_client, filter, expected_result): 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) + task_z = task_factory.create(name="Z", cost_center=cost_center) + task_test = task_factory.create(name="Test") + reviewer = task_assignee_factory(user=user, 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 @@ -139,11 +149,17 @@ def test_customer_statistic_filtered(auth_client, filter, expected_result): ], ) 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 2ae27471..b10f3864 100644 --- a/timed/reports/tests/test_month_statistic.py +++ b/timed/reports/tests/test_month_statistic.py @@ -5,7 +5,6 @@ from rest_framework import status from timed.conftest import setup_customer_and_employment_status -from timed.tracking.factories import ReportFactory @pytest.mark.parametrize( @@ -19,7 +18,12 @@ ], ) 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, + report_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -30,9 +34,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 4345cece..ee4e4ebd 100644 --- a/timed/reports/tests/test_project_statistic.py +++ b/timed/reports/tests/test_project_statistic.py @@ -5,9 +5,6 @@ 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( @@ -28,6 +25,8 @@ def test_project_statistic_list( expected, status_code, django_assert_num_queries, + report_factory, + task_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -37,13 +36,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 +107,16 @@ 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): +def test_project_statistic_filtered( + auth_client, + filter, + expected_result, + cost_center, + task_factory, + report_factory, + user, + task_assignee_factory, +): user = auth_client.user setup_customer_and_employment_status( user=user, @@ -118,14 +126,17 @@ def test_project_statistic_filtered(auth_client, filter, expected_result): 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) + task_z = task_factory.create(name="Z", cost_center=cost_center) + task_test = task_factory.create(name="Test") + reviewer = task_assignee_factory(user=user, 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 94918e20..c729f3bd 100644 --- a/timed/reports/tests/test_task_statistic.py +++ b/timed/reports/tests/test_task_statistic.py @@ -5,9 +5,6 @@ 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( @@ -28,6 +25,8 @@ def test_task_statistic_list( expected, status_code, django_assert_num_queries, + task_factory, + report_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -37,11 +36,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 +99,11 @@ def test_task_statistic_filtered( auth_client, filter, expected_result, + cost_center, + report_factory, + task_factory, + task_assignee_factory, + user, ): user = auth_client.user setup_customer_and_employment_status( @@ -110,14 +114,17 @@ 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) + task_z = task_factory.create(name="Z", cost_center=cost_center) + task_test = task_factory.create(name="Test") + reviewer = task_assignee_factory(user=user, 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 50ebcc85..05ebc65d 100644 --- a/timed/reports/tests/test_user_statistic.py +++ b/timed/reports/tests/test_user_statistic.py @@ -5,7 +5,6 @@ from rest_framework import status from timed.conftest import setup_customer_and_employment_status -from timed.tracking.factories import ReportFactory @pytest.mark.parametrize( @@ -19,7 +18,12 @@ ], ) 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, + report_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -29,9 +33,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.create(duration=timedelta(hours=1), user=user) + report_factory.create(duration=timedelta(hours=2), user=user) + report = report_factory.create(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 9830a65c..28bd12c0 100644 --- a/timed/reports/tests/test_work_report.py +++ b/timed/reports/tests/test_work_report.py @@ -8,10 +8,7 @@ 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 +30,10 @@ def test_work_report_single_project( expected, status_code, django_assert_num_queries, + 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, + project_factory, + task_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="Project{0}".format(i)) - task = TaskFactory.create(project=project) - ReportFactory.create_batch(10, user=user, task=task, date=report_date) + project = project_factory.create(customer=customer, name="Project{0}".format(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(db, customer_name, project_name, expected): +def test_generate_work_report_name( + db, 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, test_date, project) assert name == expected @@ -169,17 +181,25 @@ def test_generate_work_report_name(db, 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 132b3386..e5665045 100644 --- a/timed/reports/tests/test_year_statistic.py +++ b/timed/reports/tests/test_year_statistic.py @@ -5,8 +5,6 @@ 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( @@ -20,7 +18,12 @@ ], ) 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, + report_factory, ): user = auth_client.user setup_customer_and_employment_status( @@ -31,9 +34,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.create(duration=timedelta(hours=1), date=date(2017, 1, 1)) + 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-list") result = auth_client.get(url, data={"ordering": "year"}) @@ -64,11 +67,13 @@ 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 95b5c5f7..4e648e57 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( @@ -19,13 +17,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: @@ -61,10 +65,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() @@ -72,7 +82,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: @@ -98,10 +108,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: @@ -111,7 +126,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 ) @@ -157,16 +172,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: @@ -220,11 +237,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 ) @@ -270,17 +293,24 @@ 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_factory, + customer_assignee_factory, ): user = auth_client.user - order = factories.OrderFactory.create() + order = order_factory.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..72edb79d 100644 --- a/timed/subscription/tests/test_package.py +++ b/timed/subscription/tests/test_package.py @@ -1,12 +1,9 @@ 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 - -def test_subscription_package_list(auth_client): - PackageFactory.create() +def test_subscription_package_list(auth_client, package_factory): + package_factory.create() url = reverse("subscription-package-list") @@ -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 39336eb6..1b80f488 100644 --- a/timed/subscription/tests/test_subscription_project.py +++ b/timed/subscription/tests/test_subscription_project.py @@ -4,49 +4,52 @@ 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, + billing_type, + project_factory, + report_factory, + task_factory, + package_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( + 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 +76,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 +113,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 d18c8ca9..644efe3b 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.parametrize("is_id_token", [True, False]) @pytest.mark.parametrize( @@ -90,9 +88,8 @@ def test_authentication_new_user( assert user_model.objects.count() == expected_count -def test_authentication_update_user_data(db, rf, requests_mock, settings): +def test_authentication_update_user_data(db, 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 e5dc07c1..e1c9d1ef 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,14 @@ 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 +232,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 +241,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 +251,18 @@ 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 +286,9 @@ 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 +296,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 +327,11 @@ 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 +354,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 +384,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 +406,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 b94471df..17151a9c 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]) @@ -38,10 +35,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 @@ -74,9 +73,11 @@ 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() @@ -110,12 +111,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 @@ -151,13 +159,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 @@ -169,10 +184,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") @@ -183,11 +198,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")}) @@ -221,9 +236,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": { @@ -246,9 +261,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 ) @@ -271,9 +286,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 ) @@ -290,9 +305,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 ) @@ -302,8 +317,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 fc67e2b7..e0936ffd 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 7c5731fa..8361398e 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") @@ -490,7 +493,13 @@ def test_report_detail( ], ) def test_report_create( - auth_client, report_factory, task_factory, task_assignee, is_external, expected + auth_client, + report_factory, + task_factory, + task_assignee, + is_external, + expected, + employment_factory, ): """Should create a new report and automatically set the user.""" user = auth_client.user @@ -500,9 +509,9 @@ def test_report_create( task_assignee.save() if is_external: - EmploymentFactory.create(user=user, is_external=True) + employment_factory.create(user=user, is_external=True) else: - EmploymentFactory.create(user=user, is_external=False) + employment_factory.create(user=user, is_external=False) data = { "data": { @@ -610,9 +619,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") @@ -629,12 +640,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 ) @@ -658,9 +668,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 +772,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 +795,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 +837,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 +885,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 +933,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() @@ -948,14 +957,13 @@ def test_report_verify_other_user(superadmin_client, report_factory, user_factor def test_report_reset_verified_by_reviewer( - internal_employee_client, - report_factory, + 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 +985,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 @@ -1022,7 +1029,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 @@ -1030,13 +1044,13 @@ 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) + employment_factory.create(user=user, is_external=True) else: - EmploymentFactory.create(user=user, is_external=False) + employment_factory.create(user=user, is_external=False) url = reverse("report-detail", args=[report.id]) response = auth_client.delete(url) @@ -1061,23 +1075,29 @@ 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) + employment_factory.create(user=user, is_external=True) else: - EmploymentFactory.create(user=user, is_external=False) + employment_factory.create(user=user, is_external=False) url = reverse("report-detail", args=[report.id]) response = auth_client.delete(url) @@ -1109,8 +1129,8 @@ def test_report_round_duration(db, 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) @@ -1119,9 +1139,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]) @@ -1260,9 +1280,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) @@ -1325,6 +1346,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() @@ -1333,16 +1356,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": { @@ -1386,10 +1409,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( @@ -1430,9 +1454,15 @@ 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, project, task, report, user_factory, needs_review + superadmin_client, + project, + task, + report, + user_factory, + 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}} } @@ -1462,12 +1492,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 ) @@ -1606,10 +1635,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 @@ -1637,10 +1668,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") @@ -1669,12 +1702,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) @@ -1709,12 +1747,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 ) @@ -1771,9 +1811,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) @@ -1834,14 +1875,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 ) @@ -1883,9 +1928,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: @@ -1966,12 +2012,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()