Skip to content

Commit

Permalink
Merge branch 'main' into feature/notificative-notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
RishiDiwanTT authored Sep 11, 2023
2 parents 9cee4eb + daabfad commit 8892ac5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
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 = []
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

0 comments on commit 8892ac5

Please sign in to comment.