Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Hlamallama committed Feb 25, 2025
2 parents f410628 + e209e43 commit 4a673f7
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pip-delete-this-directory.txt
htmlcov/
.tox/
.coverage
.coverage 2
.coverage.*
.cache
.pytest_cache/
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- CMS Forms Flexible imports and Better Error Handling
- Contentpage warnings for media_link field
- Test coverage report
- Changed Assessments to CMS Forms
- Validation for high_inflection, medium_inflection and score field on CMS Forms
### Removed
- Locale field on exports
- Menu app
Expand Down
23 changes: 19 additions & 4 deletions home/import_assessments.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ 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)
check_inflection_type(high_inflection, medium_inflection, row_num)
score = row.get("scores")
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 @@ -479,7 +481,7 @@ def check_punctuation(
)


def check_score_type(
def check_inflection_type(
high_inflection: Any | None, medium_inflection: Any | None, row_num: int
) -> None:
if high_inflection is not None and high_inflection != "":
Expand All @@ -488,7 +490,7 @@ def check_score_type(
except ValueError:
raise ImportAssessmentException(
"Invalid number format for high inflection. "
"The score value allows only numbers",
"The inflection value allows only numbers",
row_num,
)
if medium_inflection is not None and medium_inflection != "":
Expand All @@ -497,7 +499,20 @@ def check_score_type(
except ValueError:
raise ImportAssessmentException(
"Invalid number format for medium inflection. "
"The score value allows only numbers",
"The inflection value allows only numbers",
row_num,
)


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

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 07:38

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 @@ -1499,7 +1499,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
Original file line number Diff line number Diff line change
@@ -1,3 +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,
Titles,age_question,,titles,,en,,Text123,,,,0.0,,Generic,What is your age,,,,,,,,test,
title,age_question,,title,,en,,,,,,0.0,,yes,asg,,,,,,,,id,
5 changes: 5 additions & 0 deletions home/tests/import-export-data/bad_score_field.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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
Test assessment pt,categorical_question,"pt,test",test-assessment-pt,,pt,,,,,,0,,Generic error,Q1,,,,,A1,0,a1,q1,""""""
Breastfeeding Quiz,categorical_question,"mnch,breastfeeding_quiz",breastfeeding-quiz,1,en,breastfeeding-quiz-champion,75,breastfeeding-quiz-good,25,breastfeeding-quiz-learner,100,,"Sorry, I didn't understand that. Please click one of the buttons.","*Question 1*
",,,,,"Formula,Formula+breast milk,Breast milk only","test,0.0,1.0","formula,formula-and-breast milk,breast-milk-only",baby-food@,"
"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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
Test assessment pt,categorical_question,"pt,test",test-assessment-pt,,pt,,,,,,0,,Generic error,Q1,,,,,A1,"one, ""two, three'",a1,q1,""""""
Breastfeeding Quiz,categorical_question,"mnch,breastfeeding_quiz",breastfeeding-quiz,1,en,breastfeeding-quiz-champion,75,breastfeeding-quiz-good,25,breastfeeding-quiz-learner,100,,"Sorry, I didn't understand that. Please click one of the buttons.",*Question 1*,,,,,"Formula,Formula+breast milk,Breast milk only","one,two\nthree'","formula,formula-and-breast milk,breast-milk-only",baby-food@,"
"
38 changes: 32 additions & 6 deletions home/tests/test_assessment_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,29 +629,55 @@ 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:
def test_invalid_high_inflection(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")
csv_impexp.import_file("bad_high_inflection.csv")
assert (
e.value.message == "Invalid number format for high inflection. "
"The score value allows only numbers"
"The inflection value allows only numbers"
)
assert e.value.row_num == 2

def test_invalid_medium_score(self, csv_impexp: ImportExport) -> None:
def test_invalid_medium_inflection(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")
csv_impexp.import_file("bad_medium_inflection.csv")
assert (
e.value.message == "Invalid number format for medium inflection. "
"The score value allows only numbers"
"The inflection value allows only numbers"
)
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 a list of numbers separated by commas."
)
assert e.value.row_num == 3

def test_trigger_deserialise_list_error(self, csv_impexp: ImportExport) -> None:
"""
Importing a CSV with invalid data that fails the deserialise_list fucntion
should return a valid error message
"""
with pytest.raises(ImportAssessmentException) as e:
csv_impexp.import_file("trigger_deserialise_list_error.csv")
assert (
e.value.message == "Invalid number format for score field. "
"The score value allows only a list of numbers separated by commas."
)
assert e.value.row_num == 2

Expand Down
10 changes: 5 additions & 5 deletions home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,24 @@ 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"))
self.result_queue.put((messages.SUCCESS, "CMS Forms import successful"))
# Wait until the user has fetched the result message to close the thread
self.result_queue.join()

Expand Down

0 comments on commit 4a673f7

Please sign in to comment.