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
|
from dataclasses import dataclass
from random import Random
from typing import List, Union
import pytest
from faker import Faker
from faker.config import AVAILABLE_LOCALES
from polyfactory.factories.dataclass_factory import DataclassFactory
FakerLocaleType = Union[str, List[str]]
RANDINT_MAP = {i: Random(i).randint(0, 10) for i in range(3)}
FAKER_LOCALES: List[FakerLocaleType] = [
AVAILABLE_LOCALES[0],
AVAILABLE_LOCALES[1],
AVAILABLE_LOCALES[2],
AVAILABLE_LOCALES[:3],
]
@pytest.mark.parametrize("seed", RANDINT_MAP.keys())
def test_setting_random(seed: int) -> None:
@dataclass
class Foo:
foo: int
class FooFactory(DataclassFactory[Foo]):
__model__ = Foo
__random__ = Random(seed)
@classmethod
def foo(cls) -> int:
return cls.__random__.randint(0, 10)
assert FooFactory.build().foo == RANDINT_MAP[seed]
@pytest.mark.parametrize("seed", RANDINT_MAP.keys())
def test_setting_random_seed_on_random(seed: int) -> None:
@dataclass
class Foo:
foo: int
class FooFactory(DataclassFactory[Foo]):
__model__ = Foo
__random_seed__ = seed
@classmethod
def foo(cls) -> int:
return cls.__random__.randint(0, 10)
assert FooFactory.build().foo == RANDINT_MAP[seed]
@pytest.mark.parametrize("seed", RANDINT_MAP.keys())
def test_setting_random_seed_on_faker(seed: int) -> None:
@dataclass
class Foo:
foo: int
class FooFactory(DataclassFactory[Foo]):
__model__ = Foo
__random_seed__ = seed
@classmethod
def foo(cls) -> int:
return cls.__faker__.random_digit()
assert FooFactory.build().foo == RANDINT_MAP[seed]
def test_setting_random_seed_on_multiple_factories() -> None:
foo_seed = 0
bar_seed = 1
@dataclass
class Foo:
foo: int
@dataclass
class Bar:
bar: int
class FooFactory(DataclassFactory[Foo]):
__model__ = Foo
__random_seed__ = foo_seed
@classmethod
def foo(cls) -> int:
return cls.__random__.randint(0, 10)
class BarFactory(DataclassFactory[Bar]):
__model__ = Bar
__random_seed__ = bar_seed
@classmethod
def bar(cls) -> int:
return cls.__random__.randint(0, 10)
assert FooFactory.build().foo == RANDINT_MAP[foo_seed]
assert BarFactory.build().bar == RANDINT_MAP[bar_seed]
def test_no_override_of_faker() -> None:
faker = Faker()
@dataclass
class Foo:
foo: int
class FooFactory(DataclassFactory[Foo]):
__model__ = Foo
__faker__ = faker
__random_seed__ = 10
assert FooFactory.__faker__ is faker
@pytest.mark.parametrize("locale", FAKER_LOCALES)
def test_faker_locale_preserved(locale: FakerLocaleType) -> None:
@dataclass
class Foo:
foo: int
class FooFactory(DataclassFactory[Foo]):
__model__ = Foo
__faker__ = Faker(locale=locale)
__random_seed__ = 10
expected_locales = [locale] if isinstance(locale, str) else locale
assert FooFactory.__faker__.locales == expected_locales
|