Skip to content

Commit

Permalink
updated score field validation and changed Assessments naming to CMS …
Browse files Browse the repository at this point in the history
…forms
  • Loading branch information
Mudiwa Matanda authored and Mudiwa Matanda committed Feb 24, 2025
1 parent ad6ca7b commit a638838
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 6 deletions.
14 changes: 14 additions & 0 deletions home/import_assessments.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,10 @@ def from_flat(cls, row: dict[str, str], row_num: int) -> "AssessmentRow":

high_inflection = row.get("high_inflection")
medium_inflection = row.get("medium_inflection")
score = row.get("scores")
check_punctuation(high_inflection, medium_inflection, row_num)
check_score_type(high_inflection, medium_inflection, row_num)
check_score_field(score, row_num)

row = {
key: value for key, value in row.items() if value and key in cls.fields()
Expand Down Expand Up @@ -457,6 +459,18 @@ def check_score_type(
)


def check_score_field(score: Any, row_num: int) -> None:
if score:
try:
[float(s) for s in score.split(",")]
except ValueError:
raise ImportAssessmentException(
"Invalid number format for score field. "
"The score value allows only numbers",
row_num,
)


def deserialise_list(value: str) -> list[str]:
"""
Takes a comma separated value serialised by the CSV library, and returns it as a
Expand Down
20 changes: 20 additions & 0 deletions home/migrations/0091_alter_assessment_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.17 on 2025-02-24 06:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("home", "0090_alter_orderedcontentset_pages"),
]

operations = [
migrations.AlterField(
model_name="assessment",
name="slug",
field=models.SlugField(
help_text="A unique identifier for this CMS Form", max_length=255
),
),
]
2 changes: 1 addition & 1 deletion home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ class Meta:

title = models.CharField(max_length=255)
slug = models.SlugField(
max_length=255, help_text="A unique identifier for this assessment"
max_length=255, help_text="A unique identifier for this CMS Form"
)
version = models.CharField(
max_length=200,
Expand Down
3 changes: 3 additions & 0 deletions home/tests/import-export-data/bad_high_score.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title,question_type,tags,slug,version,locale,high_result_page,high_inflection,medium_result_page,medium_inflection,low_result_page,skip_threshold,skip_high_result_page,generic_error,question,explainer,error,min,max,answers,scores,answer_semantic_ids,question_semantic_id,answer_responses
Titles,age_question,,titles,,en,,Text,,,,0.0,,Generic,What is your age,,,,,,,,test,
title,age_question,,title,,en,,,,,,0.0,,yes,asg,,,,,,,,id,
326 changes: 326 additions & 0 deletions home/tests/import-export-data/bad_score_field.csv

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion home/tests/test_assessment_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def test_invalid_high_score(self, csv_impexp: ImportExport) -> None:
return an intuitive error message
"""
with pytest.raises(ImportAssessmentException) as e:
csv_impexp.import_file("bad_form_score.csv")
csv_impexp.import_file("bad_high_score.csv")
assert (
e.value.message == "Invalid number format for high inflection. "
"The score value allows only numbers"
Expand All @@ -618,6 +618,19 @@ def test_invalid_medium_score(self, csv_impexp: ImportExport) -> None:
)
assert e.value.row_num == 2

def test_invalid_score_field(self, csv_impexp: ImportExport) -> None:
"""
Importing a CSV with invalid data in the score field value should
return an intuitive error message
"""
with pytest.raises(ImportAssessmentException) as e:
csv_impexp.import_file("bad_score_field.csv")
assert (
e.value.message == "Invalid number format for score field. "
"The score value allows only numbers"
)
assert e.value.row_num == 3


@pytest.mark.usefixtures("result_content_pages")
@pytest.mark.django_db()
Expand Down
8 changes: 4 additions & 4 deletions home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,22 @@ def run(self):
self.result_queue.put(
(
messages.ERROR,
f"Assessment import failed on row {e.row_num}: {e.message}",
f"CMS Forms import failed on row {e.row_num}: {e.message}",
)
)
except ImportException as e:
self.result_queue.put(
(
messages.ERROR,
[
f"Assessment import failed on row {e.row_num}: {msg}"
f"CMS Forms import failed on row {e.row_num}: {msg}"
for msg in e.message
],
)
)
except Exception:
self.result_queue.put((messages.ERROR, "Assessment import failed"))
logger.exception("Assessment import failed")
self.result_queue.put((messages.ERROR, "CMS Forms import failed"))
logger.exception("CMS Forms import failed")
else:
self.result_queue.put((messages.SUCCESS, "Assessment import successful"))
# Wait until the user has fetched the result message to close the thread
Expand Down

0 comments on commit a638838

Please sign in to comment.