|  | 
| 1 |  | -from unittest.mock import patch | 
|  | 1 | +import importlib | 
|  | 2 | +from dataclasses import dataclass | 
|  | 3 | +from types import ModuleType | 
| 2 | 4 | 
 | 
|  | 5 | +import pytest | 
| 3 | 6 | from sqlmodel import create_engine | 
| 4 | 7 | 
 | 
| 5 |  | -from ...conftest import get_testing_print_function | 
|  | 8 | +from tests.conftest import PrintMock, needs_py39, needs_py310 | 
| 6 | 9 | 
 | 
| 7 | 10 | expected_calls = [ | 
| 8 | 11 |     [ | 
|  | 
| 22 | 25 | ] | 
| 23 | 26 | 
 | 
| 24 | 27 | 
 | 
| 25 |  | -def test_tutorial(): | 
| 26 |  | -    from docs_src.tutorial.code_structure.tutorial001 import app, database | 
|  | 28 | +@dataclass | 
|  | 29 | +class Modules: | 
|  | 30 | +    app: ModuleType | 
|  | 31 | +    database: ModuleType | 
| 27 | 32 | 
 | 
| 28 |  | -    database.sqlite_url = "sqlite://" | 
| 29 |  | -    database.engine = create_engine(database.sqlite_url) | 
| 30 |  | -    app.engine = database.engine | 
| 31 |  | -    calls = [] | 
| 32 | 33 | 
 | 
| 33 |  | -    new_print = get_testing_print_function(calls) | 
| 34 |  | - | 
| 35 |  | -    with patch("builtins.print", new=new_print): | 
| 36 |  | -        app.main() | 
| 37 |  | -    assert calls == expected_calls | 
|  | 34 | +@pytest.fixture( | 
|  | 35 | +    name="modules", | 
|  | 36 | +    params=[ | 
|  | 37 | +        "tutorial001", | 
|  | 38 | +        pytest.param("tutorial001_py39", marks=needs_py39), | 
|  | 39 | +        pytest.param("tutorial001_py310", marks=needs_py310), | 
|  | 40 | +    ], | 
|  | 41 | +) | 
|  | 42 | +def get_modules(request: pytest.FixtureRequest) -> Modules: | 
|  | 43 | +    app_module = importlib.import_module( | 
|  | 44 | +        f"docs_src.tutorial.code_structure.{request.param}.app" | 
|  | 45 | +    ) | 
|  | 46 | +    database_module = importlib.import_module( | 
|  | 47 | +        f"docs_src.tutorial.code_structure.{request.param}.database" | 
|  | 48 | +    ) | 
|  | 49 | +    database_module.sqlite_url = "sqlite://" | 
|  | 50 | +    database_module.engine = create_engine(database_module.sqlite_url) | 
|  | 51 | +    app_module.engine = database_module.engine | 
|  | 52 | + | 
|  | 53 | +    return Modules(app=app_module, database=database_module) | 
|  | 54 | + | 
|  | 55 | + | 
|  | 56 | +def test_tutorial(print_mock: PrintMock, modules: Modules): | 
|  | 57 | +    modules.app.main() | 
|  | 58 | +    assert print_mock.calls == expected_calls | 
0 commit comments