I have a simple pydantic model with a default value field:
import pydantic
from uuid import UUID, uuid4
class User(pydantic.BaseModel):
id: UUID = pydantic.Field(default_factory=uuid4)
email: pydantic.EmailStr
password_hash: str
And I want to test it by pytest with mocked id generation:
from unittest.mock import patch, Mock
from uuid import UUID
from faker import Faker
@patch('uuid.uuid4')
def test_id_default_value_is_uuid4(uuid4_mock: Mock, faker: Faker):
excepted_id = UUID("550e8400-e29b-41d4-a716-446655440000")
uuid4_mock.return_value = excepted_id
user = User(email=faker.email(), password_hash=faker.sha256())
assert user.id == excepted_id
But the id is always generated randomly, as if the mock doesn't work.
I thought that pydantic somehow changes functions passed to default_factory parameter and tried to explicitly declare uuid4 call but it didn't work: id: UUID = pydantic.Field(default_factory=lambda: uuid4()). It continues to be generated randomly:
AssertionError: assert UUID('068642d2-af32-49c6-843c-a50d01b76a37') == UUID('550e8400-e29b-41d4-a716-446655440000')
`