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

Don't raise error on invalid dates in CSV uploads #574

Merged
merged 2 commits into from
Feb 9, 2025
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
2 changes: 1 addition & 1 deletion project/npda/general_functions/csv/csv_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def csv_parse(csv_file, is_jersey=False):
for column in ALL_DATES:
if column in df.columns:
# Support DD/MM/YYYY and DD/MM/YY
df[column] = pd.to_datetime(df[column], format="mixed", dayfirst=True, errors="raise")
df[column] = pd.to_datetime(df[column], format="mixed", dayfirst=True, errors="coerce")

# Apply the dtype to non-date columns
for column, dtype in CSV_DATA_TYPES_MINUS_DATES.items():
Expand Down
59 changes: 55 additions & 4 deletions project/npda/tests/test_csv_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,11 +892,62 @@ def test_dates_with_short_year(one_patient_two_visits):
assert(df.equals(one_patient_two_visits))


@pytest.mark.parametrize(
"column",
[
pytest.param("Date of Birth"),
pytest.param("Date of Diabetes Diagnosis"),
],
)
@pytest.mark.django_db(transaction=True)
def test_bad_date_format_on_mandatory_column(
seed_groups_per_function_fixture,
seed_users_per_function_fixture,
one_patient_two_visits,
column
):
# As these tests need full transaction support we can't use our session fixtures
test_user = NPDAUser.objects.filter(
organisation_employers__pz_code=ALDER_HEY_PZ_CODE
).first()

# Delete all patients to ensure we're starting from a clean slate
Patient.objects.all().delete()

df = one_patient_two_visits

df[column] = df[column].astype(str)
df[column] = "beep"

csv = df.to_csv(index=False, date_format="%d/%m/%Y")

assert (
Patient.objects.count() == 0
), "There should be no patients in the database before the test"

df = read_csv_from_str(csv).df
errors = csv_upload_sync(test_user, df)

assert(len(errors) == 1)

assert (
Patient.objects.count() == 0
), "There should be no patients in the database after the test"


@pytest.mark.django_db
def test_bad_date_format(one_patient_two_visits):
csv = one_patient_two_visits.to_csv(index=False, date_format="%d/%m")
with pytest.raises(ValueError):
read_csv_from_str(csv)
def test_bad_date_format_on_optional_column(one_patient_two_visits):
df = one_patient_two_visits

column = "Date of Level 3 carbohydrate counting education received"

df[column] = df[column].astype(str)
df[column] = "beep"

csv = df.to_csv(index=False, date_format="%d/%m/%Y")

df = read_csv_from_str(csv).df
assert(len(df) == 2)


@pytest.mark.django_db
Expand Down