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

20 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("tutorial002"), 

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 [ 

26 "Event:", 

27 { 

28 "id": None, 

29 "starts_at": datetime(2026, 1, 1, 12, tzinfo=UTC), 

30 "local_time": datetime(2026, 1, 1, 12), 

31 }, 

32 ] 

33 ] 

34 

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

36 mod.Event.model_validate( 1nopqrstuvwxyz

37 { 

38 "starts_at": "2026-01-01T12:00:00", 

39 "local_time": "2026-01-01T12:00:00Z", 

40 } 

41 ) 

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

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

44 (("starts_at",), "timezone_aware"), 

45 (("local_time",), "timezone_naive"), 

46 ] 

47 

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

49 assert table.c.starts_at.type.timezone is True 1nopqrstuvwxyz

50 assert table.c.local_time.type.timezone is False 1nopqrstuvwxyz