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

Assessment import for multiple languages and identical slug #408

Merged
merged 2 commits into from
Feb 5, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Answer responses for Forms
- Language_code field on imports and exports
- Fix for assessment import for multiple languages
### Removed
- Locale field on exports
-->
Expand Down
14 changes: 7 additions & 7 deletions home/import_assessments.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,23 @@ def add_field_values_to_assessment(self, assessment: Assessment) -> None:
assessment.high_result_page_id = (
None
if self.high_result_page == ""
else get_content_page_id_from_slug(self.high_result_page)
else get_content_page_id_from_slug(self.high_result_page, self.locale)
)
assessment.medium_inflection = self.medium_inflection
assessment.medium_result_page_id = (
None
if self.medium_result_page == ""
else get_content_page_id_from_slug(self.medium_result_page)
else get_content_page_id_from_slug(self.medium_result_page, self.locale)
)
assessment.low_result_page_id = (
None
if self.low_result_page == ""
else get_content_page_id_from_slug(self.low_result_page)
else get_content_page_id_from_slug(self.low_result_page, self.locale)
)
assessment.skip_high_result_page_id = (
None
if self.skip_high_result_page == ""
else get_content_page_id_from_slug(self.skip_high_result_page)
else get_content_page_id_from_slug(self.skip_high_result_page, self.locale)
)
assessment.skip_threshold = self.skip_threshold
assessment.generic_error = self.generic_error
Expand Down Expand Up @@ -398,13 +398,13 @@ def from_flat(cls, row: dict[str, str], row_num: int) -> "AssessmentRow":
)


def get_content_page_id_from_slug(slug: str) -> int:
def get_content_page_id_from_slug(slug: str, locale: Locale) -> int:
try:
page = ContentPage.objects.get(slug=slug)
page = ContentPage.objects.get(slug=slug, locale=locale)
except ObjectDoesNotExist:
raise ImportAssessmentException(
f"You are trying to add an assessment, where one of the result pages "
f"references the content page with slug {slug} which does not exist. "
f"references the content page with slug {slug} and locale {locale} which does not exist. "
"Please create the content page first."
)
return page.id
Expand Down
3 changes: 3 additions & 0 deletions home/tests/import-export-data/multiple_language.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
Test min max range,integer_question,test-min-max-range,test-min-max-range,v1.0,en,,5.0,,3.0,,0.0,,This is a generic error,Lowest temeprature you're experienced,We need to know some things,This is an error message,0,30,,,,lowest-temperature,
Weather Trivia,integer_question,weather-trivia,weather-trivia,v1.0,en,high-inflection,5.0,medium-score,1.0,low-score,0.0,,"Sorry, we didn't quite get that.",What's the coldest weather you're experienced?,We need to know some things,Your reply should be between {min} and {max},50,70,,,,coldest-weather,
59 changes: 57 additions & 2 deletions home/tests/test_assessment_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ def normalise_nested_ids(assessment: DbDict) -> DbDict:
]


def create_content_page(title: str, slug: str, locale_code: str = "en") -> ContentPage:
locale = Locale.objects.get(language_code=locale_code)
home_page = HomePage.objects.get(slug="home")
page = ContentPage(
title=title,
slug=slug,
locale=locale,
)
home_page.add_child(instance=page)
page.save_revision().publish()
return page


def create_locale_if_not_exists(locale_code: str) -> None:
if not Locale.objects.filter(language_code=locale_code).exists():
Locale.objects.create(language_code=locale_code)


@dataclass
class ImportExport:
admin_client: Any
Expand Down Expand Up @@ -431,11 +449,11 @@ def test_missing_related_pages(self, csv_impexp: ImportExport) -> None:
assert (
e.value.message
== "You are trying to add an assessment, where one of the result pages "
"references the content page with slug fake-page which does not exist. "
"references the content page with slug fake-page and locale English which does not exist. "
"Please create the content page first."
)

def test_import_error(elf, csv_impexp: ImportExport) -> None:
def test_import_error(self, csv_impexp: ImportExport) -> None:
"""
Importing an invalid CSV file leaves the db as-is.

Expand Down Expand Up @@ -573,3 +591,40 @@ def test_mismatched_length_answers(self, csv_impexp: ImportExport) -> None:
"answer responses (5) do not match."
)
assert e.value.row_num == 2


@pytest.mark.usefixtures("result_content_pages")
@pytest.mark.django_db()
class TestImportMultipleLanguages:

def test_create_content_page(self, csv_impexp: ImportExport) -> None:
"""
Importing a csv with a results page that has the same slug
in more than one locale should pass.

This test uses high-inflection as slug and high-result page in English and French locale
The English high-inflection is already created by the result_content_pages fixture
The French high_inflection is created below
"""

high_inflection_page_en = ContentPage.objects.get(
slug="high-inflection", locale__language_code="en"
)

create_locale_if_not_exists("fr")
high_inflection_page_fr = create_content_page(
"High Inflection", "high-inflection", locale_code="fr"
)

assert high_inflection_page_en.title == "High Inflection"
assert high_inflection_page_en.locale.language_code == "en"
assert high_inflection_page_en.live is True

assert high_inflection_page_fr.title == "High Inflection"
assert high_inflection_page_fr.locale.language_code == "fr"
assert high_inflection_page_fr.live is True

csv_bytes = csv_impexp.import_file("multiple_language.csv")
content = csv_impexp.export_assessment()
src, dst = csv_impexp.csvs2dicts(csv_bytes, content)
assert dst == src