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
|
import pytest
from faker import Faker
from faker.exceptions import UniquenessException
class TestUniquenessClass:
def test_uniqueness(self):
fake = Faker("en_US")
names = set()
# There are (at time of writing 690) first names in the
# US identity provider. Birthday paradox puts the chances of
# no duplicates in 250 selections as low enough to be impossible
for i in range(250):
first_name = fake.unique.first_name()
assert first_name not in names
names.add(first_name)
def test_sanity_escape(self):
fake = Faker()
# Those of you who are especially astute may realise
# there are only 2 booleans, so the third boolean cannot
# be unique.
with pytest.raises(UniquenessException, match=r"Got duplicated values after [\d,]+ iterations."):
for i in range(3):
_ = fake.unique.boolean()
def test_uniqueness_clear(self):
fake = Faker()
for i in range(2):
fake.unique.boolean()
fake.unique.clear()
# Because we cleared the generated values, this will not
# throw an exception
fake.unique.boolean()
def test_exclusive_arguments(self):
"""Calls through the "unique" portal will only affect
calls with that specific function signature.
"""
fake = Faker()
for i in range(10):
fake.unique.random_int(min=1, max=10)
# Different signature, so new pool. If they shared a pool
# this would throw a sanity exception
fake.unique.random_int(min=2, max=10)
def test_functions_only(self):
"""Accessing non-functions through the `.unique` attribute
will throw a TypeError."""
fake = Faker()
with pytest.raises(TypeError, match="Accessing non-functions through .unique is not supported."):
fake.unique.locales
def test_complex_return_types_is_supported(self):
"""The unique decorator supports complex return types
like the ones used in the profile provider."""
fake = Faker()
for i in range(10):
fake.unique.pydict()
for i in range(10):
fake.unique.pylist()
for i in range(10):
fake.unique.pyset()
def test_unique_locale_access(self):
"""Accessing locales through UniqueProxy with subscript notation
maintains global uniqueness across all locales."""
fake = Faker(["en_US", "fr_FR", "ja_JP"])
generated = set()
for i in range(5):
value = fake.unique["en_US"].random_int(min=1, max=10)
assert value not in generated
generated.add(value)
for i in range(5):
value = fake.unique["fr_FR"].random_int(min=1, max=10)
assert value not in generated
generated.add(value)
with pytest.raises(UniquenessException, match=r"Got duplicated values after [\d,]+ iterations."):
fake.unique["ja_JP"].random_int(min=1, max=10)
|