]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/blob
f2603dbd882112249143e4dae07b8a53056d1472
[thirdparty/fastapi/sqlmodel.git] /
1 import importlib
2 import sys
3 import types
4 from typing import Any
5 from unittest.mock import patch
6
7 import pytest
8 from sqlmodel import create_engine, SQLModel
9
10 from ....conftest import get_testing_print_function, needs_py39, needs_py310, PrintMock
11
12
13 expected_calls_tutorial001 = [
14 [
15 "Created hero:",
16 {
17 "name": "Deadpond",
18 "secret_name": "Dive Wilson",
19 "team_id": 1,
20 "id": 1,
21 "age": None,
22 },
23 ],
24 [
25 "Created hero:",
26 {
27 "name": "Rusty-Man",
28 "secret_name": "Tommy Sharp",
29 "team_id": 2,
30 "id": 2,
31 "age": 48,
32 },
33 ],
34 [
35 "Created hero:",
36 {
37 "name": "Spider-Boy",
38 "secret_name": "Pedro Parqueador",
39 "team_id": None,
40 "id": 3,
41 "age": None,
42 },
43 ],
44 [
45 "Updated hero:",
46 {
47 "name": "Spider-Boy",
48 "secret_name": "Pedro Parqueador",
49 "team_id": 2,
50 "id": 3,
51 "age": None,
52 },
53 ],
54 [
55 "Team Wakaland:",
56 {"name": "Wakaland", "id": 3, "headquarters": "Wakaland Capital City"},
57 ],
58 [
59 "Deleted team:",
60 {"name": "Wakaland", "id": 3, "headquarters": "Wakaland Capital City"},
61 ],
62 ["Black Lion not found:", None],
63 ["Princess Sure-E not found:", None],
64 ]
65
66
67 @pytest.fixture(
68 name="module",
69 params=[
70 "tutorial001",
71 pytest.param("tutorial001_py39", marks=needs_py39),
72 pytest.param("tutorial001_py310", marks=needs_py310),
73 ],
74 )
75 def module_fixture(request: pytest.FixtureRequest, clear_sqlmodel: Any):
76 module_name = request.param
77 # Using the corrected docs_src path
78 full_module_name = f"docs_src.tutorial.relationship_attributes.cascade_delete_relationships.{module_name}"
79
80 if full_module_name in sys.modules:
81 mod = importlib.reload(sys.modules[full_module_name])
82 else:
83 mod = importlib.import_module(full_module_name)
84
85 mod.sqlite_url = "sqlite://"
86 mod.engine = create_engine(mod.sqlite_url)
87
88 if hasattr(mod, "create_db_and_tables") and callable(mod.create_db_and_tables):
89 pass
90 elif hasattr(mod, "SQLModel") and hasattr(mod.SQLModel, "metadata"):
91 mod.SQLModel.metadata.create_all(mod.engine)
92
93 return mod
94
95
96 def test_tutorial(module: types.ModuleType, print_mock: PrintMock, clear_sqlmodel: Any):
97 with patch("builtins.print", new=get_testing_print_function(print_mock.calls)):
98 module.main()
99
100 assert print_mock.calls == expected_calls_tutorial001