Skip to content

Commit f295410

Browse files
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
1 parent 3546958 commit f295410

File tree

20 files changed

+234
-164
lines changed

20 files changed

+234
-164
lines changed

tests/test_advanced/test_decimal/test_tutorial001.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import importlib
2-
import types # Add import for types
2+
import types # Add import for types
33
from decimal import Decimal
4-
from unittest.mock import MagicMock # Keep MagicMock for type hint, though not strictly necessary for runtime
54

65
import pytest
76
from sqlmodel import create_engine
87

9-
from ...conftest import needs_py310, PrintMock # Import PrintMock for type hint
8+
from ...conftest import PrintMock, needs_py310 # Import PrintMock for type hint
109

1110
expected_calls = [
1211
[
@@ -45,8 +44,10 @@ def get_module(request: pytest.FixtureRequest):
4544
return importlib.import_module(f"docs_src.advanced.decimal.{module_name}")
4645

4746

48-
def test_tutorial(print_mock: PrintMock, module: types.ModuleType): # Use PrintMock for type hint and types.ModuleType
47+
def test_tutorial(
48+
print_mock: PrintMock, module: types.ModuleType
49+
): # Use PrintMock for type hint and types.ModuleType
4950
module.sqlite_url = "sqlite://"
5051
module.engine = create_engine(module.sqlite_url)
5152
module.main()
52-
assert print_mock.calls == expected_calls # Use .calls instead of .mock_calls
53+
assert print_mock.calls == expected_calls # Use .calls instead of .mock_calls

tests/test_tutorial/test_connect/test_delete/test_tutorial001.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@
6969
)
7070
def get_module(request: pytest.FixtureRequest) -> ModuleType:
7171
module_name = request.param
72-
mod = importlib.import_module(
73-
f"docs_src.tutorial.connect.delete.{module_name}"
74-
)
72+
mod = importlib.import_module(f"docs_src.tutorial.connect.delete.{module_name}")
7573
mod.sqlite_url = "sqlite://"
7674
mod.engine = create_engine(mod.sqlite_url)
7775
return mod

tests/test_tutorial/test_connect/test_insert/test_tutorial001.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@
4949
)
5050
def get_module(request: pytest.FixtureRequest) -> ModuleType:
5151
module_name = request.param
52-
mod = importlib.import_module(
53-
f"docs_src.tutorial.connect.insert.{module_name}"
54-
)
52+
mod = importlib.import_module(f"docs_src.tutorial.connect.insert.{module_name}")
5553
mod.sqlite_url = "sqlite://"
5654
mod.engine = create_engine(mod.sqlite_url)
5755
return mod

tests/test_tutorial/test_connect/test_select/test_tutorial003.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@
8585
)
8686
def get_module(request: pytest.FixtureRequest) -> ModuleType:
8787
module_name = request.param
88-
mod = importlib.import_module(
89-
f"docs_src.tutorial.connect.select.{module_name}"
90-
)
88+
mod = importlib.import_module(f"docs_src.tutorial.connect.select.{module_name}")
9189
mod.sqlite_url = "sqlite://"
9290
mod.engine = create_engine(mod.sqlite_url)
9391
return mod

tests/test_tutorial/test_connect/test_select/test_tutorial004.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@
5959
)
6060
def get_module(request: pytest.FixtureRequest) -> ModuleType:
6161
module_name = request.param
62-
mod = importlib.import_module(
63-
f"docs_src.tutorial.connect.select.{module_name}"
64-
)
62+
mod = importlib.import_module(f"docs_src.tutorial.connect.select.{module_name}")
6563
mod.sqlite_url = "sqlite://"
6664
mod.engine = create_engine(mod.sqlite_url)
6765
return mod

tests/test_tutorial/test_connect/test_select/test_tutorial005.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@
6161
)
6262
def get_module(request: pytest.FixtureRequest) -> ModuleType:
6363
module_name = request.param
64-
mod = importlib.import_module(
65-
f"docs_src.tutorial.connect.select.{module_name}"
66-
)
64+
mod = importlib.import_module(f"docs_src.tutorial.connect.select.{module_name}")
6765
mod.sqlite_url = "sqlite://"
6866
mod.engine = create_engine(mod.sqlite_url)
6967
return mod

tests/test_tutorial/test_connect/test_update/test_tutorial001.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import importlib
22
from types import ModuleType
3-
from typing import Any # For clear_sqlmodel type hint
3+
from typing import Any # For clear_sqlmodel type hint
44

55
import pytest
66
from sqlmodel import create_engine
@@ -60,14 +60,14 @@
6060
)
6161
def get_module(request: pytest.FixtureRequest) -> ModuleType:
6262
module_name = request.param
63-
mod = importlib.import_module(
64-
f"docs_src.tutorial.connect.update.{module_name}"
65-
)
63+
mod = importlib.import_module(f"docs_src.tutorial.connect.update.{module_name}")
6664
mod.sqlite_url = "sqlite://"
6765
mod.engine = create_engine(mod.sqlite_url)
6866
return mod
6967

7068

71-
def test_tutorial(clear_sqlmodel: Any, print_mock: PrintMock, module: ModuleType) -> None:
69+
def test_tutorial(
70+
clear_sqlmodel: Any, print_mock: PrintMock, module: ModuleType
71+
) -> None:
7272
module.main()
7373
assert print_mock.calls == expected_calls

tests/test_tutorial/test_create_db_and_table/test_tutorial002.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import importlib
22
from types import ModuleType
3-
from typing import Any # For clear_sqlmodel type hint
3+
from typing import Any # For clear_sqlmodel type hint
44

55
import pytest
66
from sqlalchemy import inspect

tests/test_tutorial/test_create_db_and_table/test_tutorial003.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import importlib
22
from types import ModuleType
3-
from typing import Any # For clear_sqlmodel type hint
3+
from typing import Any # For clear_sqlmodel type hint
44

55
import pytest
66
from sqlalchemy import inspect

tests/test_tutorial/test_fastapi/test_app_testing/test_tutorial001_tests_main.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import importlib
2-
import sys # Add sys import
2+
import sys # Add sys import
33
from types import ModuleType
44
from typing import Any, Generator
55

66
import pytest
77
from fastapi.testclient import TestClient
8-
from sqlmodel import Session, SQLModel, create_engine # Keep this for session_fixture
9-
from sqlmodel.pool import StaticPool # Keep this for session_fixture
8+
from sqlmodel import Session, SQLModel, create_engine # Keep this for session_fixture
9+
from sqlmodel.pool import StaticPool # Keep this for session_fixture
1010

1111
from ....conftest import needs_py39, needs_py310
1212

13+
1314
# This will be our parametrized fixture providing the versioned 'main' module
1415
@pytest.fixture(
1516
name="module",
@@ -20,7 +21,9 @@
2021
pytest.param("tutorial001_py310", marks=needs_py310),
2122
],
2223
)
23-
def get_module(request: pytest.FixtureRequest, clear_sqlmodel: Any) -> ModuleType: # clear_sqlmodel is autouse
24+
def get_module(
25+
request: pytest.FixtureRequest, clear_sqlmodel: Any
26+
) -> ModuleType: # clear_sqlmodel is autouse
2427
module_name = f"docs_src.tutorial.fastapi.app_testing.{request.param}.main"
2528

2629
# Forcing reload to try to get a fresh state for models
@@ -30,6 +33,7 @@ def get_module(request: pytest.FixtureRequest, clear_sqlmodel: Any) -> ModuleTyp
3033
module = importlib.import_module(module_name)
3134
return module
3235

36+
3337
@pytest.fixture(name="session", scope="function")
3438
def session_fixture(module: ModuleType) -> Generator[Session, None, None]:
3539
# Store original engine-related attributes from the module
@@ -39,13 +43,13 @@ def session_fixture(module: ModuleType) -> Generator[Session, None, None]:
3943

4044
# Force module to use a fresh in-memory SQLite DB for this test run
4145
module.sqlite_url = "sqlite://"
42-
module.connect_args = {"check_same_thread": False} # Crucial for FastAPI + SQLite
46+
module.connect_args = {"check_same_thread": False} # Crucial for FastAPI + SQLite
4347

4448
# Re-create the engine in the module to use these new settings
4549
test_engine = create_engine(
4650
module.sqlite_url,
4751
connect_args=module.connect_args,
48-
poolclass=StaticPool # Recommended for tests
52+
poolclass=StaticPool, # Recommended for tests
4953
)
5054
module.engine = test_engine
5155

@@ -55,7 +59,9 @@ def session_fixture(module: ModuleType) -> Generator[Session, None, None]:
5559
# Fallback if the function isn't named create_db_and_tables
5660
SQLModel.metadata.create_all(module.engine)
5761

58-
with Session(module.engine) as session: # Use the module's (now test-configured) engine
62+
with Session(
63+
module.engine
64+
) as session: # Use the module's (now test-configured) engine
5965
yield session
6066

6167
# Teardown: drop tables from the module's engine
@@ -68,14 +74,16 @@ def session_fixture(module: ModuleType) -> Generator[Session, None, None]:
6874
module.connect_args = original_connect_args
6975
if original_engine is not None:
7076
module.engine = original_engine
71-
else: # If engine didn't exist, remove the one we created
77+
else: # If engine didn't exist, remove the one we created
7278
if hasattr(module, "engine"):
7379
del module.engine
7480

7581

7682
@pytest.fixture(name="client", scope="function")
77-
def client_fixture(session: Session, module: ModuleType) -> Generator[TestClient, None, None]:
78-
def get_session_override() -> Generator[Session, None, None]: # Must be a generator
83+
def client_fixture(
84+
session: Session, module: ModuleType
85+
) -> Generator[TestClient, None, None]:
86+
def get_session_override() -> Generator[Session, None, None]: # Must be a generator
7987
yield session
8088

8189
module.app.dependency_overrides[module.get_session] = get_session_override
@@ -140,7 +148,7 @@ def test_read_heroes(session: Session, client: TestClient, module: ModuleType):
140148

141149

142150
def test_read_hero(session: Session, client: TestClient, module: ModuleType):
143-
hero_1 = module.Hero(name="Deadpond", secret_name="Dive Wilson") # Use module.Hero
151+
hero_1 = module.Hero(name="Deadpond", secret_name="Dive Wilson") # Use module.Hero
144152
session.add(hero_1)
145153
session.commit()
146154

@@ -155,7 +163,7 @@ def test_read_hero(session: Session, client: TestClient, module: ModuleType):
155163

156164

157165
def test_update_hero(session: Session, client: TestClient, module: ModuleType):
158-
hero_1 = module.Hero(name="Deadpond", secret_name="Dive Wilson") # Use module.Hero
166+
hero_1 = module.Hero(name="Deadpond", secret_name="Dive Wilson") # Use module.Hero
159167
session.add(hero_1)
160168
session.commit()
161169

@@ -170,13 +178,13 @@ def test_update_hero(session: Session, client: TestClient, module: ModuleType):
170178

171179

172180
def test_delete_hero(session: Session, client: TestClient, module: ModuleType):
173-
hero_1 = module.Hero(name="Deadpond", secret_name="Dive Wilson") # Use module.Hero
181+
hero_1 = module.Hero(name="Deadpond", secret_name="Dive Wilson") # Use module.Hero
174182
session.add(hero_1)
175183
session.commit()
176184

177185
response = client.delete(f"/heroes/{hero_1.id}")
178186

179-
hero_in_db = session.get(module.Hero, hero_1.id) # Use module.Hero
187+
hero_in_db = session.get(module.Hero, hero_1.id) # Use module.Hero
180188

181189
assert response.status_code == 200
182190
assert hero_in_db is None

tests/test_tutorial/test_fastapi/test_delete/test_tutorial001.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import importlib
22
import sys
33
from types import ModuleType
4-
from typing import Any # For clear_sqlmodel type hint
4+
from typing import Any # For clear_sqlmodel type hint
55

66
import pytest
77
from dirty_equals import IsDict
88
from fastapi.testclient import TestClient
9-
from sqlmodel import SQLModel, create_engine # Import SQLModel for metadata operations
9+
from sqlmodel import SQLModel, create_engine # Import SQLModel for metadata operations
1010
from sqlmodel.pool import StaticPool
1111

1212
from ....conftest import needs_py39, needs_py310
@@ -22,7 +22,7 @@
2222
],
2323
)
2424
def get_module(request: pytest.FixtureRequest, clear_sqlmodel: Any) -> ModuleType:
25-
module_name = f"docs_src.tutorial.fastapi.delete.{request.param}" # No .main here
25+
module_name = f"docs_src.tutorial.fastapi.delete.{request.param}" # No .main here
2626
if module_name in sys.modules:
2727
module = importlib.reload(sys.modules[module_name])
2828
else:
@@ -34,19 +34,23 @@ def get_module(request: pytest.FixtureRequest, clear_sqlmodel: Any) -> ModuleTyp
3434
module.sqlite_url = "sqlite://"
3535
module.engine = create_engine(
3636
module.sqlite_url,
37-
connect_args={"check_same_thread": False}, # connect_args from original main.py
38-
poolclass=StaticPool
37+
connect_args={"check_same_thread": False}, # connect_args from original main.py
38+
poolclass=StaticPool,
3939
)
4040
# Assuming the module has a create_db_and_tables or similar, or uses SQLModel.metadata directly
4141
if hasattr(module, "create_db_and_tables"):
4242
module.create_db_and_tables()
4343
else:
44-
SQLModel.metadata.create_all(module.engine) # Fallback, ensure tables are created
44+
SQLModel.metadata.create_all(
45+
module.engine
46+
) # Fallback, ensure tables are created
4547

4648
return module
4749

4850

49-
def test_tutorial(clear_sqlmodel: Any, module: ModuleType): # clear_sqlmodel is autouse but explicit for safety
51+
def test_tutorial(
52+
clear_sqlmodel: Any, module: ModuleType
53+
): # clear_sqlmodel is autouse but explicit for safety
5054
# The engine and tables are now set up by the 'module' fixture
5155
# The app's dependency overrides for get_session will use module.engine
5256

@@ -56,7 +60,7 @@ def test_tutorial(clear_sqlmodel: Any, module: ModuleType): # clear_sqlmodel is
5660
hero2_data = {
5761
"name": "Spider-Boy",
5862
"secret_name": "Pedro Parqueador",
59-
"id": 9000, # Note: ID is part of creation data here
63+
"id": 9000, # Note: ID is part of creation data here
6064
}
6165
hero3_data = {
6266
"name": "Rusty-Man",
@@ -65,13 +69,15 @@ def test_tutorial(clear_sqlmodel: Any, module: ModuleType): # clear_sqlmodel is
6569
}
6670
response = client.post("/heroes/", json=hero1_data)
6771
assert response.status_code == 200, response.text
68-
hero1 = response.json() # Get actual ID of hero1
72+
hero1 = response.json() # Get actual ID of hero1
6973
hero1_id = hero1["id"]
7074

7175
response = client.post("/heroes/", json=hero2_data)
7276
assert response.status_code == 200, response.text
7377
hero2 = response.json()
74-
hero2_id = hero2["id"] # This will be the ID assigned by DB, not 9000 if 9000 is not allowed on POST
78+
hero2_id = hero2[
79+
"id"
80+
] # This will be the ID assigned by DB, not 9000 if 9000 is not allowed on POST
7581

7682
response = client.post("/heroes/", json=hero3_data)
7783
assert response.status_code == 200, response.text
@@ -86,8 +92,8 @@ def test_tutorial(clear_sqlmodel: Any, module: ModuleType): # clear_sqlmodel is
8692
# For robustness, let's check for a non-existent ID based on actual data.
8793
# If hero2_id is 1, check for 9000. If it's 9000, check for 1 (assuming hero1_id is 1).
8894
non_existent_id_check = 9000
89-
if hero2_id == non_existent_id_check: # if DB somehow used 9000
90-
non_existent_id_check = hero1_id + hero2_id + 100 # just some other ID
95+
if hero2_id == non_existent_id_check: # if DB somehow used 9000
96+
non_existent_id_check = hero1_id + hero2_id + 100 # just some other ID
9197

9298
response = client.get(f"/heroes/{non_existent_id_check}")
9399
assert response.status_code == 404, response.text
@@ -102,7 +108,9 @@ def test_tutorial(clear_sqlmodel: Any, module: ModuleType): # clear_sqlmodel is
102108
)
103109
assert response.status_code == 200, response.text
104110

105-
response = client.patch(f"/heroes/{non_existent_id_check}", json={"name": "Dragon Cube X"})
111+
response = client.patch(
112+
f"/heroes/{non_existent_id_check}", json={"name": "Dragon Cube X"}
113+
)
106114
assert response.status_code == 404, response.text
107115

108116
response = client.delete(f"/heroes/{hero2_id}")
@@ -111,7 +119,7 @@ def test_tutorial(clear_sqlmodel: Any, module: ModuleType): # clear_sqlmodel is
111119
response = client.get("/heroes/")
112120
assert response.status_code == 200, response.text
113121
data = response.json()
114-
assert len(data) == 2 # After deleting one hero
122+
assert len(data) == 2 # After deleting one hero
115123

116124
response = client.delete(f"/heroes/{non_existent_id_check}")
117125
assert response.status_code == 404, response.text

0 commit comments

Comments
 (0)