Commit 3546958
committed
Refactor: Consolidate versioned tests for docs examples
This commit consolidates multiple version-specific test files (for Python 3.8, 3.9, 3.10) into single test files for a significant portion of the documentation examples.
**Summary of Changes:**
The primary goal was to have one test file per example, using pytest parametrization to handle different Python versions of the source documentation files. I achieved this by:
1. **Identifying Groups:** Scanned `tests/test_advanced` and `tests/test_tutorial` to find sets of test files like `test_example.py`, `test_example_py39.py`, `test_example_py310.py`.
2. **Consolidation Strategy:**
* Chose the base file (e.g., `test_example.py`) as the consolidated file.
* Introduced a `pytest` fixture (usually named `module` or `modules`) within the consolidated file.
* This fixture is parametrized with the base name of the example and its versioned counterparts (e.g., "tutorial001", "tutorial001_py39", "tutorial001_py310").
* Used `importlib.import_module()` within the fixture to load the correct example code from `docs_src/` based on the pytest parameter.
* Applied `needs_py39` and `needs_py310` marks (from `tests.conftest`) to the relevant parameters to ensure tests are skipped on incompatible Python versions.
* Modified test functions to accept this new fixture.
* For tests involving FastAPI, adapted existing `session` and `client` fixtures (or created new ones) to correctly use the parametrized `module` for setting up the test environment (in-memory SQLite engine, creating tables, and configuring the `TestClient` with the correct app instance). This often involved reloading the module and ensuring `SQLModel.metadata` was cleared between parametrized runs using the `clear_sqlmodel` fixture.
* For tests that check printed output, the `print_mock` fixture was used. For others, assertions were based on database state (via `sqlalchemy.inspect`) or API responses.
* Deleted the now-redundant version-specific test files.
**Examples Consolidated So Far:**
* **Advanced:**
* `decimal/tutorial001`
* `uuid/tutorial001`
* `uuid/tutorial002`
* **Tutorial - Code Structure:**
* `code_structure/tutorial002`
* **Tutorial - Connect:**
* `connect/create_connected_tables/tutorial001`
* `connect/delete/tutorial001`
* `connect/insert/tutorial001`
* `connect/select/tutorial003`, `tutorial004`, `tutorial005`
* `connect/update/tutorial001`
* **Tutorial - Create DB and Table:**
* `create_db_and_table/tutorial001`, `tutorial002`, `tutorial003`
* **Tutorial - FastAPI:**
* `fastapi/app_testing/tutorial001_tests_main` (refactored from subprocess)
* `fastapi/delete/tutorial001`
* `fastapi/limit_and_offset/tutorial001`
* `fastapi/multiple_models/tutorial001`, `tutorial002`
* `fastapi/read_one/tutorial001`
* `fastapi/relationships/tutorial001`
* `fastapi/response_model/tutorial001`
* `fastapi/session_with_dependency/tutorial001`
* `fastapi/simple_hero_api/tutorial001`
* `fastapi/teams/tutorial001`
This work is part of an effort to simplify the test suite structure.
The next steps would involve continuing this consolidation for the remaining examples. I also received feedback to remove extra comments and consistently use `from types import ModuleType` for type hinting, which I will apply in future work.1 parent 131559c commit 3546958
File tree
61 files changed
+1216
-8550
lines changed- tests
- test_advanced
- test_decimal
- test_uuid
- test_tutorial
- test_code_structure
- test_connect
- test_create_connected_tables
- test_delete
- test_insert
- test_select
- test_update
- test_create_db_and_table
- test_fastapi
- test_app_testing
- test_delete
- test_limit_and_offset
- test_multiple_models
- test_read_one
- test_relationships
- test_response_model
- test_session_with_dependency
- test_simple_hero_api
- test_teams
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
61 files changed
+1216
-8550
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | | - | |
| 4 | + | |
3 | 5 | | |
| 6 | + | |
4 | 7 | | |
5 | 8 | | |
6 | | - | |
| 9 | + | |
7 | 10 | | |
8 | 11 | | |
9 | 12 | | |
| |||
30 | 33 | | |
31 | 34 | | |
32 | 35 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
39 | 46 | | |
40 | | - | |
41 | 47 | | |
42 | | - | |
43 | | - | |
44 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
Lines changed: 0 additions & 45 deletions
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | | - | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | | - | |
10 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
11 | 20 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | 21 | | |
16 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
17 | 25 | | |
18 | | - | |
19 | | - | |
20 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
21 | 30 | | |
22 | 31 | | |
23 | | - | |
| 32 | + | |
24 | 33 | | |
25 | 34 | | |
26 | 35 | | |
27 | 36 | | |
28 | | - | |
| 37 | + | |
| 38 | + | |
29 | 39 | | |
30 | 40 | | |
31 | 41 | | |
| |||
69 | 79 | | |
70 | 80 | | |
71 | 81 | | |
| 82 | + | |
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | | - | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | | - | |
10 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
11 | 20 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | 21 | | |
16 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
17 | 25 | | |
18 | | - | |
19 | | - | |
20 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
21 | 30 | | |
22 | 31 | | |
23 | | - | |
| 32 | + | |
24 | 33 | | |
25 | 34 | | |
26 | 35 | | |
27 | 36 | | |
28 | | - | |
| 37 | + | |
| 38 | + | |
29 | 39 | | |
30 | 40 | | |
31 | 41 | | |
| |||
69 | 79 | | |
70 | 80 | | |
71 | 81 | | |
| 82 | + | |
This file was deleted.
0 commit comments