Skip to content

Commit

Permalink
Extend utils.is_valid_locale to do more extensive check (#76)
Browse files Browse the repository at this point in the history
* Extend utils.is_valid_locale to do more extensive check

* Removed test for invalid locale which is valid
  • Loading branch information
joro75 authored Oct 11, 2021
1 parent 27b5d19 commit 7e0ba9e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 8 additions & 1 deletion mytoyota/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

from langcodes import Language
from langcodes.tag_parser import LanguageTagError

from .const import TOKEN_LENGTH
from .exceptions import ToyotaInvalidToken
Expand All @@ -11,7 +12,13 @@

def is_valid_locale(locale: str) -> bool:
"""Is locale string valid."""
return Language.get(locale).is_valid()
valid = False
if locale:
try:
valid = Language.get(locale).is_valid()
except LanguageTagError:
pass
return valid


def is_valid_token(token: str) -> bool:
Expand Down
13 changes: 6 additions & 7 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ def test_is_valid_locale(self, locale: str):
@pytest.mark.parametrize(
"invalid_locale",
[
# "",
# joro75: Should be fixed, waiting for other PR to be accepted
# None,
# 'something',
# 'en-u',
# 'en-us-nl-nl',
"",
None,
"something_invalid",
"en-u",
"en-us-nl-nl",
],
)
def disabled_test_not_is_valid_locale(self, invalid_locale: str):
def test_not_is_valid_locale(self, invalid_locale: str):
"""Test invalid cases for is_valid_locale"""
assert not is_valid_locale(invalid_locale)

Expand Down

0 comments on commit 7e0ba9e

Please sign in to comment.