-
models class Match(BaseModel):
rule: Rule
selector: str
class InterestingElement(BaseModel):
id: str
html: Html
match: Match
selectors: ElementSelector
text: Text
nearbyLabels: list["InterestingElement"] factories joba = random.Random(10)
@register_fixture
class InterestingElementFactory(ModelFactory[InterestingElement]):
__set_as_default_factory_for_type__ = True
__randomize_collection_length__ = True
__min_collection_length__ = 0
__max_collection_length__ = 5
__model__ = InterestingElement
# __random_seed__ = 10
__random__ = joba
class RuleFactory(ModelFactory[Rule]):
__set_as_default_factory_for_type__ = True
__randomize_collection_length__ = True
__min_collection_length__ = 0
__max_collection_length__ = 5
__model__ = Rule
__random__ = joba
# __random_seed__ = 10
name = Use(ModelFactory.__random__.choice, ["rule", "group_rule", "misc_rule"])
color = Use(ModelFactory.__random__.choice, ["green", "red", "purple"])
type = Use(ModelFactory.__random__.choice, ["input", "checkbox", "list", "link"]) The output is always different if I use |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I am assuming this is because of polyfactory/polyfactory/factories/base.py Line 504 in d75b1d2 It is directly using the faker instance and the For a workaround can you try this. I have roughly modeled my own from random import Random
from faker import Faker, Generator
from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel
class Rule(BaseModel):
name: str
color: str
type: str
something: str
something_else: str
# Create your own faker instance with the random instance you want
joba = Random(10)
generator = Generator()
generator.random = joba
faker = Faker(generator=generator)
class RuleFactory(ModelFactory[Rule]):
__set_as_default_factory_for_type__ = True
__randomize_collection_length__ = True
__min_collection_length__ = 0
__max_collection_length__ = 5
# assign the faker and random instances created above
__random__ = joba
__faker__ = faker
# use the `__random__` instance of `RuleFactory` not `ModelFactory`
name = lambda: RuleFactory.__random__.choice(["rule", "group_rule", "misc_rule"])
color = lambda: RuleFactory.__random__.choice(["green", "red", "purple"])
type = lambda: RuleFactory.__random__.choice(["input", "checkbox", "list", "link"])
print(RuleFactory.build()) @guacs can probably add more to this |
Beta Was this translation helpful? Give feedback.
-
@JobaDiniz what @Alc-Alc said will work. The reason you were getting non-deterministic values is because 1) faker has it's own seeding and 2) you only set the Point 2 is also the reason from polyfactory.factories.pydantic_factory import ModelFactory
ModelFactory.seed_random(10) # You can run this in an `autouse` fixture in `pytest` as well to get determinstic values across tests |
Beta Was this translation helpful? Give feedback.
@JobaDiniz what @Alc-Alc said will work. The reason you were getting non-deterministic values is because 1) faker has it's own seeding and 2) you only set the
__random__
on the factories you defined which meansModelFactory.__random__
is the default one set by polyfactory without any specific seed value.Point 2 is also the reason
__random_seed__
didn't work for theUse
fields. The easiest way to ensure you don't face any of these issues is to: