From 7e0ba9e21485a02395410c580c202d8b52c5580f Mon Sep 17 00:00:00 2001 From: John de Rooij Date: Mon, 11 Oct 2021 10:50:11 +0200 Subject: [PATCH] Extend utils.is_valid_locale to do more extensive check (#76) * Extend utils.is_valid_locale to do more extensive check * Removed test for invalid locale which is valid --- mytoyota/utils.py | 9 ++++++++- tests/test_utils.py | 13 ++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mytoyota/utils.py b/mytoyota/utils.py index 3adcbf71..1ea866b2 100644 --- a/mytoyota/utils.py +++ b/mytoyota/utils.py @@ -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 @@ -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: diff --git a/tests/test_utils.py b/tests/test_utils.py index dd6e7189..5e49929a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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)