File: test_example_8.py

package info (click to toggle)
python-polyfactory 2.22.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,892 kB
  • sloc: python: 11,338; makefile: 103; sh: 37
file content (27 lines) | stat: -rw-r--r-- 593 bytes parent folder | download
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
from dataclasses import dataclass
from enum import Enum

from polyfactory.factories import DataclassFactory


class Species(str, Enum):
    CAT = "Cat"
    DOG = "Dog"
    RABBIT = "Rabbit"
    MOUSE = "Mouse"


@dataclass
class Pet:
    name: str
    species: Species
    sound: str


def test_imperative_sub_factory_creation() -> None:
    pet_factory = DataclassFactory.create_factory(model=Pet)
    cat_factory = pet_factory.create_factory(species=Species.CAT)
    cat_instance = cat_factory.build()

    assert isinstance(cat_instance, Pet)
    assert cat_instance.species == Species.CAT