5 from unittest.mock import patch
8 from sqlmodel import create_engine
10 # Assuming conftest.py is at tests/conftest.py, the path should be ....conftest
11 from ....conftest import PrintMock, get_testing_print_function, needs_py39, needs_py310
13 expected_calls_tutorial001 = [
19 "secret_name": "Dive Wilson",
29 "secret_name": "Tommy Sharp",
39 "secret_name": "Pedro Parqueador",
49 "secret_name": "Pedro Parqueador",
56 {"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
59 "Preventers new hero:",
63 "secret_name": "Natalia Roman-on",
69 "Preventers new hero:",
73 "secret_name": "Steve Weird",
79 "Preventers new hero:",
83 "secret_name": "Esteban Rogelios",
85 "name": "Captain North America",
95 pytest.param("tutorial001_py39", marks=needs_py39),
96 pytest.param("tutorial001_py310", marks=needs_py310),
99 def module_fixture(request: pytest.FixtureRequest, clear_sqlmodel: Any):
100 module_name = request.param
101 full_module_name = f"docs_src.tutorial.relationship_attributes.create_and_update_relationships.{module_name}"
103 if full_module_name in sys.modules:
104 mod = importlib.reload(sys.modules[full_module_name])
106 mod = importlib.import_module(full_module_name)
108 mod.sqlite_url = "sqlite://"
109 mod.engine = create_engine(mod.sqlite_url)
111 if hasattr(mod, "create_db_and_tables") and callable(mod.create_db_and_tables):
112 # Assuming main() or create_db_and_tables() handles table creation
114 elif hasattr(mod, "SQLModel") and hasattr(mod.SQLModel, "metadata"):
115 mod.SQLModel.metadata.create_all(mod.engine)
120 def test_tutorial(module: types.ModuleType, print_mock: PrintMock, clear_sqlmodel: Any):
121 with patch("builtins.print", new=get_testing_print_function(print_mock.calls)):
124 assert print_mock.calls == expected_calls_tutorial001