Skip to content

Commit 861c5bf

Browse files
committed
Add excluded_types attribute to faker unique proxy
1 parent e1319d9 commit 861c5bf

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

rdt/transformers/pii/anonymizer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ def reset_randomization(self):
196196

197197
def _function(self):
198198
"""Return the result of calling the ``faker`` function."""
199+
if not hasattr(self.faker.unique, '_excluded_types'):
200+
setattr(self.faker.unique, '_excluded_types', ()) # The default is an empty tuple
201+
199202
try:
200203
if self.cardinality_rule in {'unique', 'match', 'scale'}:
201204
faker_attr = self.faker.unique

tests/unit/transformers/pii/test_anonymizer.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test__function_cardinality_rule_none(self):
101101
- The returned function, when called, has to call the `faker.<function_name>` function
102102
with the provided kwargs.
103103
"""
104-
# setup
104+
# Setup
105105
instance = Mock()
106106
function = Mock()
107107
unique_function = Mock()
@@ -147,7 +147,7 @@ def test__function_cardinality_rule_unique(self):
147147
- The returned function, when called, has to call the ``faker.unique.<function_name>``
148148
function with the provided kwargs.
149149
"""
150-
# setup
150+
# Setup
151151
instance = Mock()
152152
function = Mock()
153153
unique_function = Mock()
@@ -169,7 +169,7 @@ def test__function_cardinality_rule_unique(self):
169169

170170
def test__function_cardinality_rule_match(self):
171171
"""Test it when 'cardinality_rule' is 'match'."""
172-
# setup
172+
# Setup
173173
instance = Mock()
174174
function = Mock()
175175
unique_function = Mock()
@@ -191,7 +191,7 @@ def test__function_cardinality_rule_match(self):
191191

192192
def test__function_cardinality_rule_missing_attribute(self):
193193
"""Test it when ``cardinality_rule`` attribute is missing."""
194-
# setup
194+
# Setup
195195
instance = Mock()
196196
function = Mock()
197197
unique_function = Mock()
@@ -214,11 +214,36 @@ def test__function_cardinality_rule_missing_attribute(self):
214214

215215
def test__function_with_iterables_return(self):
216216
"""Test that ``_function`` returns the values of the iterable."""
217-
# setup
217+
# Setup
218+
instance = Mock()
219+
instance.cardinality_rule = None
220+
function = Mock()
221+
function.return_value = ('value_1', 'value_2')
222+
223+
instance.faker.number = function
224+
instance.function_name = 'number'
225+
instance.function_kwargs = {'type': 'int'}
226+
227+
# Run
228+
result = AnonymizedFaker._function(instance)
229+
230+
# Assert
231+
function.assert_called_once_with(type='int')
232+
assert result == 'value_1, value_2'
233+
234+
def test__function_sets__excluded_types(self):
235+
"""Test that ``_function`` will set the `_excluded_types` attribute to `faker`."""
236+
237+
# Setup
238+
class Dummy:
239+
pass
240+
218241
instance = Mock()
219242
instance.cardinality_rule = None
220243
function = Mock()
221244
function.return_value = ('value_1', 'value_2')
245+
instance.faker.unique = Dummy()
246+
assert hasattr(instance.faker.unique, '_excluded_types') is False
222247

223248
instance.faker.number = function
224249
instance.function_name = 'number'
@@ -230,6 +255,7 @@ def test__function_with_iterables_return(self):
230255
# Assert
231256
function.assert_called_once_with(type='int')
232257
assert result == 'value_1, value_2'
258+
assert hasattr(instance.faker.unique, '_excluded_types')
233259

234260
@patch('rdt.transformers.pii.anonymizer.importlib')
235261
@patch('rdt.transformers.pii.anonymizer.warnings')

0 commit comments

Comments
 (0)