def test_worktime_balance_with_employments(auth_client, django_assert_num_queries): # Calculate over one week start_date = date(2017, 3, 19) end_date = date(2017, 3, 26) employment = EmploymentFactory.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(user=auth_client.user, start_date=date(2017, 3, 24), worktime_per_day=timedelta(hours=8), end_date=None) # Overtime credit of 10 hours OvertimeCreditFactory.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) # One public holiday on weekend PublicHolidayFactory.create(date=start_date + timedelta(days=1), location=employment.location) # 2x 10 hour reported worktime ReportFactory.create(user=auth_client.user, date=start_date + timedelta(days=3), duration=timedelta(hours=10)) ReportFactory.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)) url = reverse('worktime-balance-detail', args=[ '{0}_{1}'.format(auth_client.user.id, end_date.strftime('%Y-%m-%d')) ]) with django_assert_num_queries(12): result = auth_client.get(url) assert result.status_code == status.HTTP_200_OK # 4 workdays 8.5 hours, 1 workday 8 hours, minus one holiday 8.5 # minutes 10.5 hours overtime credit expected_worktime = timedelta(hours=23) # 2 x 10 reports hours + 1 absence of 8 hours expected_reported = timedelta(hours=28) json = result.json() assert json['data']['attributes']['balance'] == ( duration_string(expected_reported - expected_worktime))
def test_absence_public_holiday(auth_client): """Should not be able to create an absence on a public holiday.""" date = datetime.date(2017, 5, 16) user = auth_client.user type = AbsenceTypeFactory.create() employment = EmploymentFactory.create( user=user, start_date=date, worktime_per_day=datetime.timedelta(hours=8) ) PublicHolidayFactory.create(location=employment.location, date=date) data = { "data": { "type": "absences", "id": None, "attributes": {"date": date.strftime("%Y-%m-%d")}, "relationships": { "type": {"data": {"type": "absence-types", "id": type.id}} }, } } url = reverse("absence-list") response = auth_client.post(url, data) assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_absence_public_holiday(self): """Should not be able to create an absence on a public holiday.""" date = datetime.date(2017, 5, 16) type = AbsenceTypeFactory.create() PublicHolidayFactory.create(location=Employment.objects.for_user( self.user, date).location, date=date) data = { 'data': { 'type': 'absences', 'id': None, 'attributes': { 'date': date.strftime('%Y-%m-%d') }, 'relationships': { 'type': { 'data': { 'type': 'absence-types', 'id': type.id } } } } } url = reverse('absence-list') res = self.client.post(url, data) assert res.status_code == HTTP_400_BAD_REQUEST
def test_user_worktime_balance(self): """Should calculate correct worktime balances.""" user = self.user employment = user.employments.get(end_date__isnull=True) # Calculate over one week start_date = date(2017, 3, 19) end_date = date(2017, 3, 26) employment.start_date = start_date employment.worktime_per_day = timedelta(hours=8) employment.save() # Overtime credit of 10 hours OvertimeCreditFactory.create(user=user, date=start_date, duration=timedelta(hours=10, minutes=30)) # One public holiday during workdays PublicHolidayFactory.create(date=start_date, location=employment.location) # One public holiday on weekend PublicHolidayFactory.create(date=start_date + timedelta(days=1), location=employment.location) url = reverse('user-detail', args=[user.id]) res = self.client.get('{0}?until={1}'.format( url, end_date.strftime('%Y-%m-%d'))) result = self.result(res) # 5 workdays minus one holiday minus 10 hours overtime credit expected_worktime = (4 * employment.worktime_per_day - timedelta(hours=10, minutes=30)) assert (result['data']['attributes']['worktime-balance'] == duration_string(timedelta() - expected_worktime)) # 2x 10 hour reported worktime ReportFactory.create(user=user, date=start_date + timedelta(days=3), duration=timedelta(hours=10)) ReportFactory.create(user=user, date=start_date + timedelta(days=4), duration=timedelta(hours=10)) AbsenceFactory.create(user=user, date=start_date + timedelta(days=5)) res2 = self.client.get('{0}?until={1}'.format( url, end_date.strftime('%Y-%m-%d'))) result2 = self.result(res2) assert (result2['data']['attributes']['worktime-balance'] == duration_string(timedelta(hours=28) - expected_worktime))
def test_public_holiday_list(auth_client): PublicHolidayFactory.create() url = reverse('public-holiday-list') response = auth_client.get(url) assert response.status_code == status.HTTP_200_OK json = response.json() assert len(json['data']) == 1
def test_public_holiday_year_filter(auth_client): PublicHolidayFactory.create(date=date(2017, 1, 1)) public_holiday = PublicHolidayFactory.create(date=date(2018, 1, 1)) url = reverse('public-holiday-list') response = auth_client.get(url, data={'year': 2018}) assert response.status_code == status.HTTP_200_OK json = response.json() assert len(json['data']) == 1 assert json['data'][0]['id'] == str(public_holiday.id)
def test_public_holiday_delete(auth_client): public_holiday = PublicHolidayFactory.create() url = reverse("public-holiday-detail", args=[public_holiday.id]) response = auth_client.delete(url) assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
def test_public_holiday_detail(auth_client): public_holiday = PublicHolidayFactory.create() url = reverse("public-holiday-detail", args=[public_holiday.id]) response = auth_client.get(url) assert response.status_code == status.HTTP_200_OK
def test_absence_list_authenticated(auth_client): absence = AbsenceFactory.create(user=auth_client.user) # overlapping absence with public holidays need to be hidden overlap_absence = AbsenceFactory.create( user=auth_client.user, date=datetime.date(2018, 1, 1) ) employment = EmploymentFactory.create( user=overlap_absence.user, start_date=datetime.date(2017, 12, 31) ) PublicHolidayFactory.create(date=overlap_absence.date, location=employment.location) url = reverse("absence-list") response = auth_client.get(url) assert response.status_code == status.HTTP_200_OK json = response.json() assert len(json["data"]) == 1 assert json["data"][0]["id"] == str(absence.id)
def setUp(self): """Set the environment for the tests up.""" super().setUp() self.public_holidays = PublicHolidayFactory.create_batch(10)