5 from unittest.mock import patch
8 from sqlmodel import create_engine, SQLModel
10 # Assuming conftest.py is at tests/conftest.py, the path should be ....conftest
11 from ....conftest import get_testing_print_function, needs_py39, needs_py310, PrintMock
14 expected_calls_tutorial001 = [
20 "secret_name": "Dive Wilson",
30 "secret_name": "Tommy Sharp",
40 "secret_name": "Pedro Parqueador",
50 "secret_name": "Pedro Parqueador",
57 {"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
60 "Preventers new hero:",
64 "secret_name": "Natalia Roman-on",
70 "Preventers new hero:",
74 "secret_name": "Steve Weird",
80 "Preventers new hero:",
84 "secret_name": "Esteban Rogelios",
86 "name": "Captain North America",
96 pytest.param("tutorial001_py39", marks=needs_py39),
97 pytest.param("tutorial001_py310", marks=needs_py310),
100 def module_fixture(request: pytest.FixtureRequest, clear_sqlmodel: Any):
101 module_name = request.param
102 full_module_name = f"docs_src.tutorial.relationship_attributes.create_and_update_relationships.{module_name}"
104 if full_module_name in sys.modules:
105 mod = importlib.reload(sys.modules[full_module_name])
107 mod = importlib.import_module(full_module_name)
109 mod.sqlite_url = "sqlite://"
110 mod.engine = create_engine(mod.sqlite_url)
112 if hasattr(mod, "create_db_and_tables") and callable(mod.create_db_and_tables):
113 # Assuming main() or create_db_and_tables() handles table creation
115 elif hasattr(mod, "SQLModel") and hasattr(mod.SQLModel, "metadata"):
116 mod.SQLModel.metadata.create_all(mod.engine)
121 def test_tutorial(module: types.ModuleType, print_mock: PrintMock, clear_sqlmodel: Any):
122 with patch("builtins.print", new=get_testing_print_function(print_mock.calls)):
125 assert print_mock.calls == expected_calls_tutorial001