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
|
from typing import TypedDict
import pytest
from polyfactory import Require
from polyfactory.exceptions import MissingBuildKwargException
from polyfactory.factories import TypedDictFactory
class Person(TypedDict):
id: int
name: str
class PersonFactory(TypedDictFactory[Person]):
id = Require()
def test_id_is_required() -> None:
# this will not raise an exception
person_instance = PersonFactory.build(id=1)
assert person_instance.get("name")
assert person_instance.get("id") == 1
# but when no kwarg is passed, an exception will be raised:
with pytest.raises(MissingBuildKwargException):
PersonFactory.build()
|