Skip to content

Commit

Permalink
employee_record: migrate sanitize_employee_records to logging
Browse files Browse the repository at this point in the history
  • Loading branch information
xavfernandez committed Jan 28, 2025
1 parent ec917e6 commit 10050dd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,13 @@ def _check_approvals(self, dry_run):
)
count_no_approval = no_approval.count()

self.stdout.write("* Checking missing employee records approval:")

if count_no_approval == 0:
self.stdout.write(" - no missing approval (great!)")
else:
self.stdout.write(f" - found {count_no_approval} missing approval(s)")

self.logger.info("found %d employee records with missing approval", count_no_approval)
if count_no_approval:
if dry_run:
return

self.stdout.write(" - fixing missing approvals: DELETING employee records")

no_approval.delete()

self.stdout.write(" - done!")
delete_nb, _delete_info = no_approval.delete()
self.logger.info("deleted %d/%d employee records with missing approval", delete_nb, count_no_approval)

@transaction.atomic()
def _check_missed_notifications(self, dry_run):
Expand Down Expand Up @@ -80,7 +72,9 @@ def _check_missed_notifications(self, dry_run):
)
)

self.stdout.write(f" - found {len(employee_record_with_missing_notification)} missing notification(s)")
self.logger.info(
"found %d missed employee records notifications", len(employee_record_with_missing_notification)
)

total_created = 0
if not dry_run:
Expand All @@ -94,16 +88,17 @@ def _check_missed_notifications(self, dry_run):
# Unarchive the employee record so next time we don't miss the notification
if employee_record.status == Status.ARCHIVED:
employee_record.unarchive()
self.stdout.write(f" - {total_created} notification(s) created")
self.stdout.write(" - done!")
self.logger.info(
"%d/%d notifications created", total_created, len(employee_record_with_missing_notification)
)

def handle(self, *, dry_run, **options):
self.stdout.write("+ Checking employee records coherence before transferring to ASP")
self.logger.info("Checking employee records coherence before transferring to ASP")

if dry_run:
self.stdout.write(" - DRY-RUN mode: not fixing, just reporting")
self.logger.info("DRY-RUN mode: not fixing, just reporting")

self._check_approvals(dry_run)
self._check_missed_notifications(dry_run)

self.stdout.write("+ Employee records sanitizing done. Have a great day!")
self.logger.info("Employee records sanitizing done. Have a great day!")
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# serializer version: 1
# name: test_missed_notifications_limit
'''
* Checking missing employee records notifications:
- found 3 missing notification(s)
- 2 notification(s) created
- done!

'''
list([
'found 3 missed employee records notifications',
'2/3 notifications created',
])
# ---
37 changes: 15 additions & 22 deletions tests/employee_record/test_sanitize_employee_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ def command_fixture():
return sanitize_employee_records.Command(stdout=io.StringIO(), stderr=io.StringIO())


def test_handle_dry_run_option(mocker, command):
def test_handle_dry_run_option(mocker, command, caplog):
mocker.patch.object(command, "_check_approvals")
mocker.patch.object(command, "_check_missed_notifications")

command.handle(dry_run=True)
assert command.stdout.getvalue().split("\n") == [
"+ Checking employee records coherence before transferring to ASP",
" - DRY-RUN mode: not fixing, just reporting",
"+ Employee records sanitizing done. Have a great day!",
"",
assert caplog.messages == [
"Checking employee records coherence before transferring to ASP",
"DRY-RUN mode: not fixing, just reporting",
"Employee records sanitizing done. Have a great day!",
]


def test_missing_approvals(command):
def test_missing_approvals(command, caplog):
# Check for employee record without approval (through job application)

employee_record = factories.EmployeeRecordFactory()
Expand All @@ -37,16 +36,13 @@ def test_missing_approvals(command):
command._check_approvals(dry_run=False)

assert models.EmployeeRecord.objects.count() == 0
assert command.stdout.getvalue().split("\n") == [
"* Checking missing employee records approval:",
" - found 1 missing approval(s)",
" - fixing missing approvals: DELETING employee records",
" - done!",
"",
assert caplog.messages == [
"found 1 employee records with missing approval",
"deleted 1/1 employee records with missing approval",
]


def test_missed_notifications(command, faker):
def test_missed_notifications(command, faker, caplog):
# Approval() updated after the last employee record snapshot are what we want
employee_record_before_approval = factories.EmployeeRecordFactory(
status=models.Status.ARCHIVED,
Expand Down Expand Up @@ -84,16 +80,13 @@ def test_missed_notifications(command, faker):
assert employee_record_before_approval.update_notifications.count() == 1
employee_record_before_approval.refresh_from_db()
assert employee_record_before_approval.status != Status.ARCHIVED
assert command.stdout.getvalue().split("\n") == [
"* Checking missing employee records notifications:",
" - found 1 missing notification(s)",
" - 1 notification(s) created",
" - done!",
"",
assert caplog.messages == [
"found 1 missed employee records notifications",
"1/1 notifications created",
]


def test_missed_notifications_limit(faker, mocker, snapshot, command):
def test_missed_notifications_limit(faker, mocker, snapshot, command, caplog):
mocker.patch.object(command, "MAX_MISSED_NOTIFICATIONS_CREATED", 2)
factories.EmployeeRecordFactory.create_batch(
3,
Expand All @@ -107,4 +100,4 @@ def test_missed_notifications_limit(faker, mocker, snapshot, command):
command._check_missed_notifications(dry_run=False)

assert models.EmployeeRecordUpdateNotification.objects.count() == 2
assert command.stdout.getvalue() == snapshot
assert caplog.messages == snapshot

0 comments on commit 10050dd

Please sign in to comment.