Skip to content

Commit

Permalink
Merge branch 'main' into list-items-error-descriptive-message
Browse files Browse the repository at this point in the history
  • Loading branch information
Hlamallama authored Feb 13, 2025
2 parents 1afde57 + 2c3475a commit d5078c6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix for assessment import for multiple languages
- Add locale filtering to assessments API
- Error message when list_items is broken
- Consistent labelling on import forms
### Removed
- Locale field on exports
-->
Expand Down
2 changes: 1 addition & 1 deletion home/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class UploadFileForm(forms.Form):
FILE_CHOICES = (("CSV", "CSV file"), ("XLSX", "Excel File"))
FILE_CHOICES = (("CSV", "CSV File"), ("XLSX", "Excel File"))
file = forms.FileField()
file_type = forms.ChoiceField(choices=FILE_CHOICES)

Expand Down
24 changes: 24 additions & 0 deletions home/import_assessments.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ def from_flat(cls, row: dict[str, str], row_num: int) -> "AssessmentRow":
high_inflection = row.get("high_inflection")
medium_inflection = row.get("medium_inflection")
check_punctuation(high_inflection, medium_inflection, row_num)
check_score_type(high_inflection, medium_inflection, row_num)

row = {
key: value for key, value in row.items() if value and key in cls.fields()
Expand Down Expand Up @@ -433,6 +434,29 @@ def check_punctuation(
)


def check_score_type(
high_inflection: Any | None, medium_inflection: Any | None, row_num: int
) -> None:
if high_inflection is not None and high_inflection != "":
try:
float(high_inflection)
except ValueError:
raise ImportAssessmentException(
"Invalid number format for high inflection. "
"The score value allows only numbers",
row_num,
)
if medium_inflection is not None and medium_inflection != "":
try:
float(medium_inflection)
except ValueError:
raise ImportAssessmentException(
"Invalid number format for medium inflection. "
"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
3 changes: 3 additions & 0 deletions home/tests/import-export-data/bad_form_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,
3 changes: 3 additions & 0 deletions home/tests/import-export-data/bad_medium_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,,,,Text123,,0.0,,Generic,What is your age,,,,,,,,test,
title,age_question,,title,,en,,,,,,0.0,,yes,asg,,,,,,,,id,
26 changes: 26 additions & 0 deletions home/tests/test_assessment_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,32 @@ def test_mismatched_length_answers(self, csv_impexp: ImportExport) -> None:
)
assert e.value.row_num == 2

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

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


@pytest.mark.usefixtures("result_content_pages")
@pytest.mark.django_db()
Expand Down
7 changes: 2 additions & 5 deletions home/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,7 @@ def test_form_has_all_expected_options(self, admin_client):
file_upload = form.find("input", type="file", id="id_file")
assert file_upload

assert find_options(form, "file_type") == ["CSV file", "Excel File"]
# TODO: consistency in the labeling, either both "file" or both "File"

assert find_options(form, "file_type") == ["CSV File", "Excel File"]
assert find_options(soup, "purge") == ["No", "Yes"]

all_locales = [locale.language_name for locale in Locale.objects.all()]
Expand Down Expand Up @@ -690,8 +688,7 @@ def test_ordered_form_has_all_expected_options(self, admin_client):
file_upload = form.find("input", type="file", id="id_file")
assert file_upload

assert find_options(form, "file_type") == ["CSV file", "Excel File"]

assert find_options(form, "file_type") == ["CSV File", "Excel File"]
assert find_options(soup, "purge") == ["No", "Yes"]


Expand Down

0 comments on commit d5078c6

Please sign in to comment.