]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/blob
f1f4824a76c530553dde6f24a6b4d602feef4008
[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
9
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
12
13 expected_calls_tutorial001 = [
14 [
15 "Created hero:",
16 {
17 "age": None,
18 "id": 1,
19 "secret_name": "Dive Wilson",
20 "team_id": 1,
21 "name": "Deadpond",
22 },
23 ],
24 [
25 "Created hero:",
26 {
27 "age": 48,
28 "id": 2,
29 "secret_name": "Tommy Sharp",
30 "team_id": 2,
31 "name": "Rusty-Man",
32 },
33 ],
34 [
35 "Created hero:",
36 {
37 "age": None,
38 "id": 3,
39 "secret_name": "Pedro Parqueador",
40 "team_id": None,
41 "name": "Spider-Boy",
42 },
43 ],
44 [
45 "Updated hero:",
46 {
47 "age": None,
48 "id": 3,
49 "secret_name": "Pedro Parqueador",
50 "team_id": 2,
51 "name": "Spider-Boy",
52 },
53 ],
54 [
55 "Team Wakaland:",
56 {"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
57 ],
58 [
59 "Preventers new hero:",
60 {
61 "age": 32,
62 "id": 6,
63 "secret_name": "Natalia Roman-on",
64 "team_id": 2,
65 "name": "Tarantula",
66 },
67 ],
68 [
69 "Preventers new hero:",
70 {
71 "age": 36,
72 "id": 7,
73 "secret_name": "Steve Weird",
74 "team_id": 2,
75 "name": "Dr. Weird",
76 },
77 ],
78 [
79 "Preventers new hero:",
80 {
81 "age": 93,
82 "id": 8,
83 "secret_name": "Esteban Rogelios",
84 "team_id": 2,
85 "name": "Captain North America",
86 },
87 ],
88 ]
89
90
91 @pytest.fixture(
92 name="module",
93 params=[
94 "tutorial001",
95 pytest.param("tutorial001_py39", marks=needs_py39),
96 pytest.param("tutorial001_py310", marks=needs_py310),
97 ],
98 )
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}"
102
103 if full_module_name in sys.modules:
104 mod = importlib.reload(sys.modules[full_module_name])
105 else:
106 mod = importlib.import_module(full_module_name)
107
108 mod.sqlite_url = "sqlite://"
109 mod.engine = create_engine(mod.sqlite_url)
110
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
113 pass
114 elif hasattr(mod, "SQLModel") and hasattr(mod.SQLModel, "metadata"):
115 mod.SQLModel.metadata.create_all(mod.engine)
116
117 return mod
118
119
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)):
122 module.main()
123
124 assert print_mock.calls == expected_calls_tutorial001