Skip to content

Commit 4ea6a32

Browse files
✅ Simplify tests for code examples, one test file for multiple variants (#1664)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4c4d5b4 commit 4ea6a32

File tree

186 files changed

+1776
-14635
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+1776
-14635
lines changed

tests/test_advanced/test_decimal/test_tutorial001.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
import importlib
12
from decimal import Decimal
2-
from unittest.mock import patch
3+
from types import ModuleType
34

5+
import pytest
46
from sqlmodel import create_engine
57

6-
from ...conftest import get_testing_print_function
8+
from ...conftest import PrintMock, needs_py310
9+
10+
11+
@pytest.fixture(
12+
name="mod",
13+
params=[
14+
"tutorial001",
15+
pytest.param("tutorial001_py310", marks=needs_py310),
16+
],
17+
)
18+
def get_module(request: pytest.FixtureRequest) -> ModuleType:
19+
mod = importlib.import_module(f"docs_src.advanced.decimal.{request.param}")
20+
mod.sqlite_url = "sqlite://"
21+
mod.engine = create_engine(mod.sqlite_url)
22+
return mod
23+
724

825
expected_calls = [
926
[
@@ -30,15 +47,6 @@
3047
]
3148

3249

33-
def test_tutorial():
34-
from docs_src.advanced.decimal import tutorial001 as mod
35-
36-
mod.sqlite_url = "sqlite://"
37-
mod.engine = create_engine(mod.sqlite_url)
38-
calls = []
39-
40-
new_print = get_testing_print_function(calls)
41-
42-
with patch("builtins.print", new=new_print):
43-
mod.main()
44-
assert calls == expected_calls
50+
def test_tutorial(print_mock: PrintMock, mod: ModuleType):
51+
mod.main()
52+
assert print_mock.calls == expected_calls

tests/test_advanced/test_decimal/test_tutorial001_py310.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/test_advanced/test_uuid/test_tutorial001.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1-
from unittest.mock import patch
1+
import importlib
2+
from types import ModuleType
23

4+
import pytest
35
from dirty_equals import IsUUID
46
from sqlmodel import create_engine
57

6-
from ...conftest import get_testing_print_function
8+
from ...conftest import PrintMock, needs_py310
79

810

9-
def test_tutorial() -> None:
10-
from docs_src.advanced.uuid import tutorial001 as mod
11-
11+
@pytest.fixture(
12+
name="mod",
13+
params=[
14+
"tutorial001",
15+
pytest.param("tutorial001_py310", marks=needs_py310),
16+
],
17+
)
18+
def get_module(request: pytest.FixtureRequest) -> ModuleType:
19+
mod = importlib.import_module(f"docs_src.advanced.uuid.{request.param}")
1220
mod.sqlite_url = "sqlite://"
1321
mod.engine = create_engine(mod.sqlite_url)
14-
calls = []
22+
return mod
1523

16-
new_print = get_testing_print_function(calls)
1724

18-
with patch("builtins.print", new=new_print):
19-
mod.main()
20-
first_uuid = calls[1][0]["id"]
25+
def test_tutorial(print_mock: PrintMock, mod: ModuleType) -> None:
26+
mod.main()
27+
first_uuid = print_mock.calls[1][0]["id"]
2128
assert first_uuid == IsUUID(4)
2229

23-
second_uuid = calls[7][0]["id"]
30+
second_uuid = print_mock.calls[7][0]["id"]
2431
assert second_uuid == IsUUID(4)
2532

2633
assert first_uuid != second_uuid
2734

28-
assert calls == [
35+
assert print_mock.calls == [
2936
["The hero before saving in the DB"],
3037
[
3138
{

tests/test_advanced/test_uuid/test_tutorial001_py310.py

Lines changed: 0 additions & 72 deletions
This file was deleted.

tests/test_advanced/test_uuid/test_tutorial002.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1-
from unittest.mock import patch
1+
import importlib
2+
from types import ModuleType
23

4+
import pytest
35
from dirty_equals import IsUUID
46
from sqlmodel import create_engine
57

6-
from ...conftest import get_testing_print_function
8+
from ...conftest import PrintMock, needs_py310
79

810

9-
def test_tutorial() -> None:
10-
from docs_src.advanced.uuid import tutorial002 as mod
11-
11+
@pytest.fixture(
12+
name="mod",
13+
params=[
14+
"tutorial002",
15+
pytest.param("tutorial002_py310", marks=needs_py310),
16+
],
17+
)
18+
def get_module(request: pytest.FixtureRequest) -> ModuleType:
19+
mod = importlib.import_module(f"docs_src.advanced.uuid.{request.param}")
1220
mod.sqlite_url = "sqlite://"
1321
mod.engine = create_engine(mod.sqlite_url)
14-
calls = []
22+
return mod
1523

16-
new_print = get_testing_print_function(calls)
1724

18-
with patch("builtins.print", new=new_print):
19-
mod.main()
20-
first_uuid = calls[1][0]["id"]
25+
def test_tutorial(print_mock: PrintMock, mod: ModuleType) -> None:
26+
mod.main()
27+
first_uuid = print_mock.calls[1][0]["id"]
2128
assert first_uuid == IsUUID(4)
2229

23-
second_uuid = calls[7][0]["id"]
30+
second_uuid = print_mock.calls[7][0]["id"]
2431
assert second_uuid == IsUUID(4)
2532

2633
assert first_uuid != second_uuid
2734

28-
assert calls == [
35+
assert print_mock.calls == [
2936
["The hero before saving in the DB"],
3037
[
3138
{

tests/test_advanced/test_uuid/test_tutorial002_py310.py

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)