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

27 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, timedelta 1abcdefghijklm

3from types import ModuleType 1abcdefghijklm

4 

5import pytest 1abcdefghijklm

6from sqlalchemy import Engine 1abcdefghijklm

7from sqlmodel import SQLModel 1abcdefghijklm

8 

9from ...conftest import PrintMock 1abcdefghijklm

10 

11 

12@pytest.fixture(name="mod") 1abcdefghijklm

13def get_module(database_engine: Engine) -> ModuleType: 1abcdefghijklm

14 mod = importlib.import_module("docs_src.advanced.datetime.tutorial001") 1abcdefghijklm

15 mod.engine = database_engine 1abcdefghijklm

16 return mod 1abcdefghijklm

17 

18 

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

20 before = datetime.now(UTC) 1nopqrstuvwxyz

21 mod.main() 1nopqrstuvwxyz

22 after = datetime.now(UTC) 1nopqrstuvwxyz

23 

24 created_at = print_mock.calls[0][1]["created_at"] 1nopqrstuvwxyz

25 assert created_at.tzinfo is UTC 1nopqrstuvwxyz

26 assert before <= created_at <= after 1nopqrstuvwxyz

27 stored_created_at = print_mock.calls[1][1]["created_at"] 1nopqrstuvwxyz

28 assert stored_created_at.tzinfo is UTC 1nopqrstuvwxyz

29 if mod.engine.dialect.name in {"mysql", "mariadb"}: 1nopqrstuvwxyz

30 # The default DATETIME columns store whole seconds. 

31 assert abs(stored_created_at - created_at) < timedelta(seconds=1) 1nps

32 else: 

33 assert stored_created_at == created_at 1oqrtuvwxyz

34 assert print_mock.calls == [ 1nopqrstuvwxyz

35 ["Event:", {"id": None, "created_at": created_at}], 

36 ["Event from database:", {"id": 1, "created_at": stored_created_at}], 

37 ] 

38 

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

40 assert table.c.created_at.type.timezone is True 1nopqrstuvwxyz