Skip to content
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

[216755] Include information about the record failing phone no validation for xlsx individual update #4293

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ def _update_single_individual(self, row: Row, individual: Individual) -> Individ
field.required = False

if not form.is_valid():
if "phone_no" in form.errors:
form.errors["phone_no"] = [f"Invalid phone number for individual {individual.unicef_id}."]
raise ValidationError(form.errors)
# TODO: add 'program_id' arg or None? individual.program_id
log_create(Individual.ACTIVITY_LOG_MAPPING, "business_area", None, None, old_individual, individual)
Expand Down
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/unit/apps/household/test_individual_xlsx_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.files import File

from hct_mis_api.apps.core.base_test_case import APITestCase
Expand Down Expand Up @@ -39,6 +40,13 @@ def invalid_file() -> File:
return File(BytesIO(content), name="invalid_updated_test_file.xlsx")


def invalid_phone_no_file() -> File:
content = Path(
f"{settings.TESTS_ROOT}/apps/household/test_file/invalid_updated_test_file_wrong_phone_no.xlsx"
).read_bytes()
return File(BytesIO(content), name="invalid_updated_phone_no_test_file.xlsx")


class TestIndividualXlsxUpdate(APITestCase):
databases = "__all__"

Expand Down Expand Up @@ -67,6 +75,12 @@ def setUpTestData(cls) -> None:
xlsx_match_columns=["individual__full_name"],
)

cls.xlsx_update_invalid_phone_no_file = XlsxUpdateFile.objects.create(
file=invalid_phone_no_file(),
business_area=cls.business_area,
xlsx_match_columns=["individual__given_name"],
)

household_data = {
"registration_data_import": registration_data_import,
"business_area": cls.business_area,
Expand Down Expand Up @@ -192,3 +206,12 @@ def test_complex_update_individual(self) -> None:
self.assertEqual(self.individuals[1].birth_date, datetime.date(1965, 8, 6))
self.assertEqual(self.individuals[2].birth_date, datetime.date(1965, 8, 7))
self.assertEqual(self.individuals[3].birth_date, datetime.date(1985, 8, 12))

def test_raise_error_when_invalid_phone_number(self) -> None:
with self.assertRaises(ValidationError) as context:
IndividualXlsxUpdate(self.xlsx_update_invalid_phone_no_file).update_individuals()

self.assertEqual(
str({"phone_no": [f"Invalid phone number for individual {self.individuals[0]}."]}),
str(context.exception),
)
Loading