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

Fix language code validation (PP-422) #1362

Merged
merged 2 commits into from
Sep 8, 2023
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
7 changes: 6 additions & 1 deletion core/configuration/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,17 @@ def validate_language_codes(
) -> Optional[List[str]]:
"""Verify that collection languages are valid."""
if value is not None:
languages = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: You could switch to a set here to avoid checking before adding a language at l. 652.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using a list because I want to avoid re-ordering the languages, and sets don't provide order.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about using one of the suggestions from stack overflow for an ordered set, but in the end with this little data, using dict keys to approximate an ordered set seemed to negatively impact readability with very little gain.

for language in value:
if not LanguageCodes.string_to_alpha_3(language):
validated_language = LanguageCodes.string_to_alpha_3(language)
if validated_language is None:
field_label = cls.get_form_field_label(field.name)
raise SettingsValidationError(
problem_detail=UNKNOWN_LANGUAGE.detailed(
f'"{field_label}": "{language}" is not a valid language code.'
)
)
if validated_language not in languages:
languages.append(validated_language)
return languages
return value
55 changes: 55 additions & 0 deletions tests/core/configuration/test_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from functools import partial
from typing import Callable, List, Optional

import pytest

from core.configuration.library import LibrarySettings
from core.util.problem_detail import ProblemError

LibrarySettingsFixture = Callable[..., LibrarySettings]


@pytest.fixture
def library_settings() -> LibrarySettingsFixture:
# Provide a default library settings object for tests, it just gives
# default values for required fields, so we can construct the settings
# without worrying about the defaults.
return partial(
LibrarySettings,
website="http://library.com",
help_web="http://library.com/help",
)


@pytest.mark.parametrize(
"languages,expected",
[
(None, None),
([], []),
(["English"], ["eng"]),
(["English", "eng", "fr", "fre", "french"], ["eng", "fre"]),
],
)
def test_validate_language_codes(
languages: Optional[List[str]],
expected: Optional[List[str]],
library_settings: LibrarySettingsFixture,
) -> None:
settings = library_settings(large_collection_languages=languages)
assert settings.large_collection_languages == expected

settings = library_settings(small_collection_languages=languages)
assert settings.small_collection_languages == expected

settings = library_settings(tiny_collection_languages=languages)
assert settings.tiny_collection_languages == expected


def test_validate_language_codes_error(
library_settings: LibrarySettingsFixture,
) -> None:
with pytest.raises(ProblemError) as excinfo:
library_settings(large_collection_languages=["eng", "xyz"])

assert excinfo.value.problem_detail.detail is not None
assert '"xyz" is not a valid language code' in excinfo.value.problem_detail.detail