-
Notifications
You must be signed in to change notification settings - Fork 30
feat: show simple shift statistics #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8e365e0
f06da84
d6a1bd2
0db1c0f
152ae4d
81562bd
8aa4292
087dcfc
cfc2939
70e4daa
a0d341d
ef7ac3d
df4a345
372e17b
a2a5357
20ce421
40f225e
b706d46
0eb3b08
7f824d0
c435d6c
f962f3f
dc4f0d9
fd44868
db15129
36e3674
fe14c97
44dd717
dcb891d
c8fa274
2d8050c
a81191e
9b51401
daa101d
28ea082
5d670be
1a7211e
c2cfaa1
7c684df
c19bab2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import datetime | ||
| import random | ||
|
|
||
| import tapir.shifts.config | ||
| from django import template | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import datetime | ||
|
|
||
| from django.utils import timezone | ||
|
|
||
| from tapir.accounts.tests.factories.factories import TapirUserFactory | ||
| from tapir.shifts.models import ( | ||
| ShiftTemplate, | ||
| ShiftAttendance, | ||
| Shift, | ||
| ) | ||
| from tapir.shifts.tests.factories import ShiftFactory, ShiftTemplateFactory | ||
| from tapir.shifts.views import ShiftDetailView | ||
| from tapir.utils.tests_utils import TapirFactoryTestBase | ||
|
|
||
|
|
||
| class ShiftGetPastShiftsStatisticsTests(TapirFactoryTestBase): | ||
|
|
||
| def test_getPastShiftsData_differentStatesOfAttendance_onlyCountDoneState(self): | ||
| user_done = TapirUserFactory.create() | ||
| user_excused = TapirUserFactory.create() | ||
|
|
||
| shift_template: ShiftTemplate = ShiftTemplateFactory.create(nb_slots=2) | ||
| shift: Shift = ShiftFactory.create( | ||
| start_time=timezone.now() - datetime.timedelta(days=1), | ||
| nb_slots=2, | ||
| shift_template=shift_template, | ||
| ) | ||
| slots = list(shift.slots.all()) | ||
| ShiftAttendance.objects.create( | ||
| state=ShiftAttendance.State.DONE, user=user_done, slot=slots[0] | ||
| ) | ||
| ShiftAttendance.objects.create( | ||
| state=ShiftAttendance.State.MISSED_EXCUSED, | ||
| user=user_excused, | ||
| slot=slots[1], | ||
| ) | ||
|
crosspolar marked this conversation as resolved.
|
||
|
|
||
| context = ShiftDetailView.get_past_shifts_data(shift.shift_template) | ||
| self.assertEqual(context["no_of_past_shifts"], 1) | ||
| self.assertEqual(context["total_valid_attendances"], 1) | ||
| self.assertEqual( | ||
| context["total_hours"], | ||
| (shift.end_time - shift.start_time).total_seconds() / 3600, | ||
| ) | ||
|
|
||
| def test_getPastShiftsData_multipleAttendandances_correctValues(self): | ||
| users = TapirUserFactory.create_batch(5) | ||
| shift_template = ShiftTemplateFactory.create(nb_slots=len(users)) | ||
| shifts: list[Shift] = [ | ||
| ShiftFactory.create( | ||
| start_time=timezone.now() - datetime.timedelta(days=i + 1), | ||
| nb_slots=len(users), | ||
| shift_template=shift_template, | ||
| ) | ||
| for i in range(5) | ||
| ] | ||
|
|
||
| for shift in shifts: | ||
| slots = list(shift.slots.all()) | ||
| for i, user in enumerate(users): | ||
| ShiftAttendance.objects.create( | ||
| state=ShiftAttendance.State.DONE, user=user, slot=slots[i] | ||
| ) | ||
| context = ShiftDetailView.get_past_shifts_data(shift_template) | ||
|
|
||
| self.assertEqual(context["no_of_past_shifts"], len(shifts)) | ||
| self.assertEqual(context["total_valid_attendances"], len(users) * len(shifts)) | ||
| self.assertEqual( | ||
| context["total_hours"], | ||
| 70.0, | ||
| "The value should have been calculated by " | ||
| "len(users)* sum((shift.end_time - shift.start_time).total_seconds() / 3600 for shift in shifts)", | ||
| ) | ||
|
|
||
| def test_getPastShiftsData_changedShiftTemplateDuration_correctSum(self): | ||
| shift_template: ShiftTemplate = ShiftTemplateFactory.create( | ||
| start_time=datetime.time(hour=10, tzinfo=datetime.timezone.utc), | ||
| end_time=datetime.time(hour=12, tzinfo=datetime.timezone.utc), | ||
| ) | ||
| shift_2_hours = shift_template.create_shift_if_necessary( | ||
| timezone.now() - datetime.timedelta(days=7) | ||
| ) | ||
| ShiftAttendance.objects.create( | ||
| user=TapirUserFactory.create(), | ||
| slot=shift_2_hours.slots.first(), | ||
| state=ShiftAttendance.State.DONE, | ||
| ) | ||
|
|
||
| shift_template.end_time = datetime.time(hour=15, tzinfo=datetime.timezone.utc) | ||
| shift_template.save() | ||
|
|
||
| shift_5_hours = shift_template.create_shift_if_necessary( | ||
| timezone.now() - datetime.timedelta(days=14) | ||
| ) | ||
| ShiftAttendance.objects.create( | ||
| user=TapirUserFactory.create(), | ||
| slot=shift_5_hours.slots.first(), | ||
| state=ShiftAttendance.State.DONE, | ||
| ) | ||
|
|
||
| context = ShiftDetailView.get_past_shifts_data(shift_template) | ||
| self.assertAlmostEqual(context["total_hours"], 7.0) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can just be a normal
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Floating-point numbers, you know. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://stackoverflow.com/a/33199669 Some "evidence"
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But have you actually had that problem here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, but I would say it's good practice preventing problems |
||
Uh oh!
There was an error while loading. Please reload this page.