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

Find nested Faker provider submodules #630

Merged
merged 1 commit into from
Apr 5, 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
3 changes: 2 additions & 1 deletion rdt/transformers/pii/anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import inspect
import warnings
from copy import deepcopy
from operator import attrgetter

import faker
import numpy as np
Expand Down Expand Up @@ -51,7 +52,7 @@ def check_provider_function(provider_name, function_name):
``AttributeError`` if the provider or the function is not found.
"""
try:
module = getattr(faker.providers, provider_name)
module = attrgetter(provider_name)(faker.providers)
if provider_name.lower() == 'baseprovider':
getattr(module, function_name)

Expand Down
23 changes: 14 additions & 9 deletions tests/unit/transformers/pii/test_anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,47 @@ class TestAnonymizedFaker:

@patch('rdt.transformers.pii.anonymizer.faker')
@patch('rdt.transformers.pii.anonymizer.getattr')
def test_check_provider_function_baseprovider(self, mock_getattr, mock_faker):
@patch('rdt.transformers.pii.anonymizer.attrgetter')
def test_check_provider_function_baseprovider(self, mock_attrgetter, mock_getattr, mock_faker):
"""Test that ``getattr`` is being called with ``BaseProvider`` and ``function_name``.

Mock:
- Mock the ``getattr`` from Python to ensure that is being called with the input.
- Mock faker and ensure that ``getattr`` is being called with ``faker.providers``.
"""
# Setup
mock_getattr.side_effect = ['module', 'provider', None]
mock_attrgetter.return_value = lambda x: 'module'
mock_getattr.side_effect = ['provider', None]

# Run
AnonymizedFaker.check_provider_function('BaseProvider', 'function_name')

# Assert
assert mock_getattr.call_args_list[0] == call(mock_faker.providers, 'BaseProvider')
assert mock_getattr.call_args_list[1] == call('module', 'function_name')
assert mock_attrgetter.call_args_list[0] == call('BaseProvider')
assert mock_getattr.call_args_list[0] == call('module', 'function_name')

@patch('rdt.transformers.pii.anonymizer.faker')
@patch('rdt.transformers.pii.anonymizer.getattr')
def test_check_provider_function_other_providers(self, mock_getattr, mock_faker):
@patch('rdt.transformers.pii.anonymizer.attrgetter')
def test_check_provider_function_other_providers(self, mock_attrgetter, mock_getattr,
mock_faker):
"""Test that ``getattr`` is being called with ``provider_name`` and ``function_name``.

Mock:
- Mock the ``getattr`` from Python to ensure that is being called with the input.
- Mock faker and ensure that ``getattr`` is being called with ``faker.providers``.
"""
# Setup
mock_getattr.side_effect = ['module', 'provider_class', None]
mock_attrgetter.return_value = lambda x: 'module'
mock_getattr.side_effect = ['provider_class', None]

# Run
AnonymizedFaker.check_provider_function('provider_name', 'function_name')

# Assert
assert mock_getattr.call_args_list[0] == call(mock_faker.providers, 'provider_name')
assert mock_getattr.call_args_list[1] == call('module', 'Provider')
assert mock_getattr.call_args_list[2] == call('provider_class', 'function_name')
assert mock_attrgetter.call_args_list[0] == call('provider_name')
assert mock_getattr.call_args_list[0] == call('module', 'Provider')
assert mock_getattr.call_args_list[1] == call('provider_class', 'function_name')

def test_check_provider_function_raise_attribute_error(self):
"""Test that ``check_provider_function`` raises an ``AttributeError``.
Expand Down