File size: 12,197 Bytes
dee9fba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | import unittest
from collections import OrderedDict
from unittest.mock import PropertyMock, patch
from faker import Faker
from faker.config import DEFAULT_LOCALE
from faker.generator import Generator
class TestFakerProxyClass(unittest.TestCase):
def setUp(self):
self.fake = None
def test_unspecified_locale(self):
self.fake = Faker()
assert len(self.fake.locales) == 1
assert len(self.fake.factories) == 1
assert self.fake.locales[0] == DEFAULT_LOCALE
def test_locale_as_string(self):
locale = 'en_US'
self.fake = Faker()
assert len(self.fake.locales) == 1
assert len(self.fake.factories) == 1
assert self.fake.locales[0] == locale
def test_locale_as_list(self):
locale = ['en-US', 'en_PH', 'ja_JP', 'de-DE']
expected = ['en_US', 'en_PH', 'ja_JP', 'de_DE']
for _ in range(10):
self.fake = Faker(locale)
assert self.fake.locales == expected
assert len(self.fake.factories) == len(expected)
locale = ['en-US', 'en_PH', 'ja_JP', 'de-DE', 'ja-JP', 'de_DE', 'en-US'] * 3
expected = ['en_US', 'en_PH', 'ja_JP', 'de_DE']
for _ in range(10):
self.fake = Faker(locale)
assert self.fake.locales == expected
assert len(self.fake.factories) == len(expected)
def test_locale_as_ordereddict(self):
locale = OrderedDict([
('de_DE', 3),
('en-US', 2),
('en-PH', 1),
('ja_JP', 5),
])
self.fake = Faker(locale)
assert len(self.fake.locales) == 4
assert self.fake.locales == ['de_DE', 'en_US', 'en_PH', 'ja_JP']
assert self.fake.weights == [3, 2, 1, 5]
locale = OrderedDict([
('de_DE', 3),
('en-US', 2),
('en-PH', 1),
('ja_JP', 5),
('de-DE', 4),
('ja-JP', 2),
('en-US', 1),
])
self.fake = Faker(locale)
assert self.fake.locales == ['de_DE', 'en_US', 'en_PH', 'ja_JP']
assert self.fake.weights == [4, 1, 1, 2]
def test_items(self):
locale = ['de_DE', 'en-US', 'en-PH', 'ja_JP', 'de-DE', 'ja-JP', 'en-US']
processed_locale = list({l.replace('-', '_') for l in locale})
self.fake = Faker(locale)
for locale_name, factory in self.fake.items():
assert locale_name in processed_locale
assert isinstance(factory, Generator)
def test_dunder_getitem(self):
locale = ['de_DE', 'en-US', 'en-PH', 'ja_JP']
self.fake = Faker(locale)
for l in locale:
assert isinstance(self.fake[l], Generator)
with self.assertRaises(KeyError):
self.fake['en_GB']
def test_seed_classmethod(self):
self.fake = Faker()
# Verify `seed()` is not callable from a class instance
with self.assertRaises(TypeError):
self.fake.seed(0)
# Verify calls to `seed()` from a class object are proxied properly
with patch('faker.generator.Generator.seed') as mock_seed:
mock_seed.assert_not_called()
Faker.seed(0)
mock_seed.assert_called_once_with(0)
def test_seed_instance(self):
locale = ['de_DE', 'en-US', 'en-PH', 'ja_JP']
self.fake = Faker(locale)
with patch('faker.generator.Generator.seed_instance') as mock_seed_instance:
mock_seed_instance.assert_not_called()
self.fake.seed_instance(0)
# Verify `seed_instance(0)` was called 4 times (one for each locale)
calls = mock_seed_instance.call_args_list
assert len(calls) == 4
for call in calls:
args, kwargs = call
assert args == (0, )
assert kwargs == {}
def test_seed_locale(self):
from faker.generator import random as shared_random_instance
locale = ['de_DE', 'en-US', 'en-PH', 'ja_JP']
self.fake = Faker(locale)
# Get current state of each factory's random instance
states = {}
for locale, factory in self.fake.items():
states[locale] = factory.random.getstate()
# Create a new random instance for en_US factory with seed value
self.fake.seed_locale('en_US', 0)
for locale, factory in self.fake.items():
# en_US factory should have changed
if locale == 'en_US':
assert factory.random != shared_random_instance
assert factory.random.getstate() != states[locale]
# There should be no changes for the rest
else:
assert factory.random == shared_random_instance
assert factory.random.getstate() == states[locale]
def test_single_locale_proxy_behavior(self):
self.fake = Faker()
internal_factory = self.fake.factories[0]
# Test if `Generator` attributes are proxied properly
for attr in self.fake.generator_attrs:
assert getattr(self.fake, attr) == getattr(internal_factory, attr)
# Test if `random` getter and setter are proxied properly
tmp_random = self.fake.random
assert internal_factory.random != 1
self.fake.random = 1
assert internal_factory.random == 1
self.fake.random = tmp_random
# Test if a valid provider method is proxied properly
# Factory selection logic should not be triggered
with patch('faker.proxy.Faker._select_factory') as mock_select_factory:
mock_select_factory.assert_not_called()
assert self.fake.name == internal_factory.name
self.fake.name()
mock_select_factory.assert_not_called()
def test_multiple_locale_proxy_behavior(self):
self.fake = Faker(['de-DE', 'en-US', 'en-PH', 'ja-JP'])
# `Generator` attributes are not implemented
for attr in self.fake.generator_attrs:
with self.assertRaises(NotImplementedError):
getattr(self.fake, attr)
# The `random` getter is not implemented
with self.assertRaises(NotImplementedError):
random = self.fake.random
random.seed(0)
# The `random` setter is not implemented
with self.assertRaises(NotImplementedError):
self.fake.random = 1
def test_multiple_locale_caching_behavior(self):
self.fake = Faker(['de_DE', 'en-US', 'en-PH', 'ja_JP'])
with patch('faker.proxy.Faker._map_provider_method',
wraps=self.fake._map_provider_method) as mock_map_method:
mock_map_method.assert_not_called()
assert not hasattr(self.fake, '_cached_name_mapping')
# Test cache creation
self.fake.name()
assert hasattr(self.fake, '_cached_name_mapping')
mock_map_method.assert_called_once_with('name')
# Test subsequent cache access
with patch.object(Faker, '_cached_name_mapping', create=True,
new_callable=PropertyMock) as mock_cached_map:
# Keep test fast by patching the cached mapping to return something simpler
mock_cached_map.return_value = [self.fake['en_US']], [1]
for _ in range(100):
self.fake.name()
# Python's hasattr() internally calls getattr()
# So each call to name() accesses the cached mapping twice
assert mock_cached_map.call_count == 200
@patch('faker.proxy.random.choice')
@patch('faker.proxy.choices_distribution')
def test_multiple_locale_factory_selection_no_weights(self, mock_choices_fn, mock_random_choice):
self.fake = Faker(['de_DE', 'en-US', 'en-PH', 'ja_JP'])
# There are no distribution weights, so factory selection logic will use `random.choice`
# if multiple factories have the specified provider method
with patch('faker.proxy.Faker._select_factory',
wraps=self.fake._select_factory) as mock_select_factory:
mock_select_factory.assert_not_called()
mock_choices_fn.assert_not_called()
mock_random_choice.assert_not_called()
# All factories for the listed locales have the `name` provider method
self.fake.name()
mock_select_factory.assert_called_once_with('name')
mock_choices_fn.assert_not_called()
mock_random_choice.assert_called_once_with(self.fake.factories)
mock_select_factory.reset_mock()
mock_choices_fn.reset_mock()
mock_random_choice.reset_mock()
# Only `en_PH` factory has provider method `luzon_province`, so there is no
# need for `random.choice` factory selection logic to run
self.fake.luzon_province()
mock_select_factory.assert_called_with('luzon_province')
mock_choices_fn.assert_not_called()
mock_random_choice.assert_not_called()
mock_select_factory.reset_mock()
mock_choices_fn.reset_mock()
mock_random_choice.reset_mock()
# Both `en_US` and `ja_JP` factories have provider method `zipcode`
self.fake.zipcode()
mock_select_factory.assert_called_once_with('zipcode')
mock_choices_fn.assert_not_called()
mock_random_choice.assert_called_once_with(
[self.fake['en_US'], self.fake['ja_JP']],
)
mock_select_factory.reset_mock()
mock_choices_fn.reset_mock()
mock_random_choice.reset_mock()
@patch('faker.proxy.random.choice')
@patch('faker.proxy.choices_distribution')
def test_multiple_locale_factory_selection_with_weights(self, mock_choices_fn, mock_random_choice):
locale = OrderedDict([
('de_DE', 3),
('en-US', 2),
('en-PH', 1),
('ja_JP', 5),
])
self.fake = Faker(locale)
mock_choices_fn.assert_not_called()
mock_random_choice.assert_not_called()
# Distribution weights have been specified, so factory selection logic will use
# `choices_distribution` if multiple factories have the specified provider method
with patch('faker.proxy.Faker._select_factory',
wraps=self.fake._select_factory) as mock_select_factory:
# All factories for the listed locales have the `name` provider method
self.fake.name()
mock_select_factory.assert_called_once_with('name')
mock_choices_fn.assert_called_once_with(self.fake.factories, self.fake.weights, length=1)
mock_random_choice.assert_not_called()
mock_select_factory.reset_mock()
mock_choices_fn.reset_mock()
mock_random_choice.reset_mock()
# Only `en_PH` factory has provider method `luzon_province`, so there is no
# need for `choices_distribution` factory selection logic to run
self.fake.luzon_province()
mock_select_factory.assert_called_once_with('luzon_province')
mock_choices_fn.assert_not_called()
mock_random_choice.assert_not_called()
mock_select_factory.reset_mock()
mock_choices_fn.reset_mock()
mock_random_choice.reset_mock()
# Both `en_US` and `ja_JP` factories have provider method `zipcode`
self.fake.zipcode()
mock_select_factory.assert_called_once_with('zipcode')
mock_choices_fn.assert_called_once_with(
[self.fake['en_US'], self.fake['ja_JP']], [2, 5], length=1,
)
mock_random_choice.assert_not_called()
mock_select_factory.reset_mock()
mock_choices_fn.reset_mock()
mock_random_choice.reset_mock()
def test_multiple_locale_factory_selection_unsupported_method(self):
self.fake = Faker(['en_US', 'en_PH'])
with self.assertRaises(AttributeError):
self.fake.obviously_invalid_provider_method_a23f()
|