]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/blob
df4797fa4344b704c523a127332ef1fd3c7e438c
[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_tutorial002 = [
14 [
15 "Created hero:",
16 {
17 "age": None,
18 "id": 1,
19 "name": "Deadpond",
20 "secret_name": "Dive Wilson",
21 "team_id": 1,
22 },
23 ],
24 [
25 "Created hero:",
26 {
27 "age": 48,
28 "id": 2,
29 "name": "Rusty-Man",
30 "secret_name": "Tommy Sharp",
31 "team_id": 2,
32 },
33 ],
34 [
35 "Created hero:",
36 {
37 "age": None,
38 "id": 3,
39 "name": "Spider-Boy",
40 "secret_name": "Pedro Parqueador",
41 "team_id": None,
42 },
43 ],
44 [
45 "Updated hero:",
46 {
47 "age": None,
48 "id": 3,
49 "name": "Spider-Boy",
50 "secret_name": "Pedro Parqueador",
51 "team_id": 2,
52 },
53 ],
54 [
55 "Team Wakaland:",
56 {"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
57 ],
58 [
59 "Deleted team:",
60 {"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
61 ],
62 [
63 "Black Lion has no team:",
64 {
65 "age": 35,
66 "id": 4,
67 "name": "Black Lion",
68 "secret_name": "Trevor Challa",
69 "team_id": None,
70 },
71 ],
72 [
73 "Princess Sure-E has no team:",
74 {
75 "age": None,
76 "id": 5,
77 "name": "Princess Sure-E",
78 "secret_name": "Sure-E",
79 "team_id": None,
80 },
81 ],
82 ]
83
84
85 @pytest.fixture(
86 name="module",
87 params=[
88 "tutorial002",
89 pytest.param("tutorial002_py39", marks=needs_py39),
90 pytest.param("tutorial002_py310", marks=needs_py310),
91 ],
92 )
93 def module_fixture(request: pytest.FixtureRequest, clear_sqlmodel: Any):
94 module_name = request.param
95 full_module_name = f"docs_src.tutorial.relationship_attributes.cascade_delete_relationships.{module_name}"
96
97 if full_module_name in sys.modules:
98 mod = importlib.reload(sys.modules[full_module_name])
99 else:
100 mod = importlib.import_module(full_module_name)
101
102 mod.sqlite_url = "sqlite://"
103 mod.engine = create_engine(mod.sqlite_url)
104
105 if hasattr(mod, "create_db_and_tables") and callable(mod.create_db_and_tables):
106 pass
107 elif hasattr(mod, "SQLModel") and hasattr(mod.SQLModel, "metadata"):
108 mod.SQLModel.metadata.create_all(mod.engine)
109
110 return mod
111
112
113 def test_tutorial(module: types.ModuleType, print_mock: PrintMock, clear_sqlmodel: Any):
114 with patch("builtins.print", new=get_testing_print_function(print_mock.calls)):
115 module.main()
116
117 assert print_mock.calls == expected_calls_tutorial002