Coverage for tests/test_advanced/test_datetime/test_tutorial005.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-16 00:02 +0000

1import importlib 1abcdefghijklm

2from datetime import UTC, datetime 1abcdefghijklm

3from types import ModuleType 1abcdefghijklm

4 

5import pytest 1abcdefghijklm

6from pydantic import ValidationError 1abcdefghijklm

7from sqlmodel import SQLModel 1abcdefghijklm

8 

9from ...conftest import PrintMock 1abcdefghijklm

10 

11 

12@pytest.fixture( 1abcdefghijklm

13 name="mod", 

14 params=[ 

15 pytest.param("tutorial005"), 

16 ], 

17) 

18def get_module(request: pytest.FixtureRequest) -> ModuleType: 1abcdefghijklm

19 return importlib.import_module(f"docs_src.advanced.datetime.{request.param}") 1abcdefghijklm

20 

21 

22def test_tutorial(print_mock: PrintMock, mod: ModuleType) -> None: 1abcdefghijklm

23 mod.main() 1nopqrstuvwxyz

24 assert print_mock.calls == [ 1nopqrstuvwxyz

25 ["Event:", {"id": None, "created_at": datetime(2026, 1, 1, 12)}] 

26 ] 

27 

28 with pytest.raises(ValidationError) as exc_info: 1nopqrstuvwxyz

29 mod.Event.model_validate({"created_at": datetime(2026, 1, 1, 12, tzinfo=UTC)}) 1nopqrstuvwxyz

30 errors = exc_info.value.errors() 1nopqrstuvwxyz

31 assert [(error["loc"], error["type"]) for error in errors] == [ 1nopqrstuvwxyz

32 (("created_at",), "timezone_naive"), 

33 ] 

34 

35 table = SQLModel.metadata.tables["event"] 1nopqrstuvwxyz

36 assert table.c.created_at.type.timezone is False 1nopqrstuvwxyz