|
1 | | -from unittest.mock import patch |
| 1 | +import importlib |
| 2 | +from types import ModuleType |
2 | 3 |
|
| 4 | +import pytest |
3 | 5 | from dirty_equals import IsUUID |
4 | 6 | from sqlmodel import create_engine |
5 | 7 |
|
6 | | -from ...conftest import get_testing_print_function |
| 8 | +from ...conftest import PrintMock, needs_py310 |
7 | 9 |
|
8 | 10 |
|
9 | | -def test_tutorial() -> None: |
10 | | - from docs_src.advanced.uuid import tutorial002 as mod |
11 | | - |
| 11 | +@pytest.fixture( |
| 12 | + name="mod", |
| 13 | + params=[ |
| 14 | + "tutorial002", |
| 15 | + pytest.param("tutorial002_py310", marks=needs_py310), |
| 16 | + ], |
| 17 | +) |
| 18 | +def get_module(request: pytest.FixtureRequest) -> ModuleType: |
| 19 | + mod = importlib.import_module(f"docs_src.advanced.uuid.{request.param}") |
12 | 20 | mod.sqlite_url = "sqlite://" |
13 | 21 | mod.engine = create_engine(mod.sqlite_url) |
14 | | - calls = [] |
| 22 | + return mod |
15 | 23 |
|
16 | | - new_print = get_testing_print_function(calls) |
17 | 24 |
|
18 | | - with patch("builtins.print", new=new_print): |
19 | | - mod.main() |
20 | | - first_uuid = calls[1][0]["id"] |
| 25 | +def test_tutorial(print_mock: PrintMock, mod: ModuleType) -> None: |
| 26 | + mod.main() |
| 27 | + first_uuid = print_mock.calls[1][0]["id"] |
21 | 28 | assert first_uuid == IsUUID(4) |
22 | 29 |
|
23 | | - second_uuid = calls[7][0]["id"] |
| 30 | + second_uuid = print_mock.calls[7][0]["id"] |
24 | 31 | assert second_uuid == IsUUID(4) |
25 | 32 |
|
26 | 33 | assert first_uuid != second_uuid |
27 | 34 |
|
28 | | - assert calls == [ |
| 35 | + assert print_mock.calls == [ |
29 | 36 | ["The hero before saving in the DB"], |
30 | 37 | [ |
31 | 38 | { |
|
0 commit comments