Skip to content

Commit

Permalink
Resolve locales warning for providers that end with the locale name (#…
Browse files Browse the repository at this point in the history
…702)

* Resolve warning conflict
  • Loading branch information
pvk-developer authored Sep 7, 2023
1 parent 5d7f8b7 commit 4f75aae
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rdt/transformers/pii/anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ def _check_locales(self):
locales = self.locales if isinstance(self.locales, list) else [self.locales]
missed_locales = []
for locale in locales:
spec = importlib.util.find_spec(f'faker.providers.{self.provider_name}.{locale}')
provider_name = self.provider_name
if self.provider_name.endswith(f'.{locale}'):
provider_name = self.provider_name.replace(f'.{locale}', '')

spec = importlib.util.find_spec(f'faker.providers.{provider_name}.{locale}')
if spec is None:
missed_locales.append(locale)

Expand Down
53 changes: 53 additions & 0 deletions tests/unit/transformers/pii/test_anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,59 @@ def test__check_locales(self, mock_warnings, mock_importlib):
)
mock_warnings.warn.assert_called_once_with(expected_message)

@patch('rdt.transformers.pii.anonymizer.importlib')
@patch('rdt.transformers.pii.anonymizer.warnings')
def test__check_locales_provider_ending_with_locale(self, mock_warnings, mock_importlib):
"""Test that check locales does not warn the user if the provider ends with the locale.
Mock:
- Mock importlib with side effects to return `None`.
- Mock the warnings.
"""
# Setup
instance = Mock()
instance.provider_name = 'address.en_US'
instance.function_name = 'postcode'
instance.locales = ['en_US']
mock_importlib.util.find_spec.side_effect = ['en_US']

# Run
AnonymizedFaker._check_locales(instance)

# Assert
mock_warnings.warn.assert_not_called()

@patch('rdt.transformers.pii.anonymizer.importlib')
@patch('rdt.transformers.pii.anonymizer.warnings')
def test__check_locales_provider_ending_with_wrong_locale(self, mock_warnings, mock_importlib):
"""Test that check locales warns the user.
If the provider ends with the given locale but is not separated by a dot this will warn
that the default 'en_US' will be used instead'.
Mock:
- Mock importlib with side effects to return `None`.
- Mock the warnings.
"""
# Setup
instance = Mock()
instance.provider_name = 'addressit'
instance.function_name = 'postcode'
instance.locales = ['it']
mock_importlib.util.find_spec.side_effect = [None]

# Run
AnonymizedFaker._check_locales(instance)

# Assert
expected_message = (
"Locales ['it'] do not support provider 'addressit' and function 'postcode'.\n"
"In place of these locales, 'en_US' will be used instead. "
'Please refer to the localized provider docs for more information: '
'https://faker.readthedocs.io/en/master/locales.html'
)
mock_warnings.warn.assert_called_once_with(expected_message)

@patch('rdt.transformers.pii.anonymizer.faker')
@patch('rdt.transformers.pii.anonymizer.AnonymizedFaker.check_provider_function')
def test___init__default(self, mock_check_provider_function, mock_faker):
Expand Down

0 comments on commit 4f75aae

Please sign in to comment.