forked from litestar-org/polyfactory
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: random seed configuration (litestar-org#321)
Co-authored-by: Jacob Coffee <jacob@z7x.org>
- Loading branch information
1 parent
5543e66
commit 61f1e2e
Showing
6 changed files
with
109 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from dataclasses import dataclass | ||
from random import Random | ||
from typing import cast | ||
|
||
import pytest | ||
|
||
from polyfactory.factories.dataclass_factory import DataclassFactory | ||
|
||
RANDINT_MAP = {i: Random(i).randint(0, 10) for i in range(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 cast(int, 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] |