]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/commit
Refactor: Consolidate versioned tests for docs examples
authorgoogle-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Thu, 19 Jun 2025 21:19:49 +0000 (21:19 +0000)
committergoogle-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Thu, 19 Jun 2025 21:19:49 +0000 (21:19 +0000)
commit35469583e41338b7302028c3490c88b677a71ce2
tree4c19b398792929aa5798c20f982f428b1d5af448
parent131559c0a751198c97f8784a97b0c73bd38fab3f
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.
61 files changed:
tests/test_advanced/test_decimal/test_tutorial001.py
tests/test_advanced/test_decimal/test_tutorial001_py310.py [deleted file]
tests/test_advanced/test_uuid/test_tutorial001.py
tests/test_advanced/test_uuid/test_tutorial001_py310.py [deleted file]
tests/test_advanced/test_uuid/test_tutorial002.py
tests/test_advanced/test_uuid/test_tutorial002_py310.py [deleted file]
tests/test_tutorial/test_code_structure/test_tutorial002.py
tests/test_tutorial/test_code_structure/test_tutorial002_py310.py [deleted file]
tests/test_tutorial/test_code_structure/test_tutorial002_py39.py [deleted file]
tests/test_tutorial/test_connect/test_create_connected_tables/test_tutorial001.py
tests/test_tutorial/test_connect/test_create_connected_tables/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_connect/test_delete/test_tutorial001.py
tests/test_tutorial/test_connect/test_delete/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_connect/test_insert/test_tutorial001.py
tests/test_tutorial/test_connect/test_insert/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_connect/test_select/test_tutorial003.py
tests/test_tutorial/test_connect/test_select/test_tutorial003_py310.py [deleted file]
tests/test_tutorial/test_connect/test_select/test_tutorial004.py
tests/test_tutorial/test_connect/test_select/test_tutorial004_py310.py [deleted file]
tests/test_tutorial/test_connect/test_select/test_tutorial005.py
tests/test_tutorial/test_connect/test_select/test_tutorial005_py310.py [deleted file]
tests/test_tutorial/test_connect/test_update/test_tutorial001.py
tests/test_tutorial/test_connect/test_update/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_create_db_and_table/test_tutorial001.py
tests/test_tutorial/test_create_db_and_table/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_create_db_and_table/test_tutorial002.py
tests/test_tutorial/test_create_db_and_table/test_tutorial002_py310.py [deleted file]
tests/test_tutorial/test_create_db_and_table/test_tutorial003.py
tests/test_tutorial/test_create_db_and_table/test_tutorial003_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_app_testing/test_tutorial001_py310_tests_main.py [deleted file]
tests/test_tutorial/test_fastapi/test_app_testing/test_tutorial001_py39_tests_main.py [deleted file]
tests/test_tutorial/test_fastapi/test_app_testing/test_tutorial001_tests_main.py
tests/test_tutorial/test_fastapi/test_delete/test_tutorial001.py
tests/test_tutorial/test_fastapi/test_delete/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_delete/test_tutorial001_py39.py [deleted file]
tests/test_tutorial/test_fastapi/test_limit_and_offset/test_tutorial001.py
tests/test_tutorial/test_fastapi/test_limit_and_offset/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_limit_and_offset/test_tutorial001_py39.py [deleted file]
tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial001.py
tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial001_py39.py [deleted file]
tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial002.py
tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial002_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial002_py39.py [deleted file]
tests/test_tutorial/test_fastapi/test_read_one/test_tutorial001.py
tests/test_tutorial/test_fastapi/test_read_one/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_read_one/test_tutorial001_py39.py [deleted file]
tests/test_tutorial/test_fastapi/test_relationships/test_tutorial001.py
tests/test_tutorial/test_fastapi/test_relationships/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_relationships/test_tutorial001_py39.py [deleted file]
tests/test_tutorial/test_fastapi/test_response_model/test_tutorial001.py
tests/test_tutorial/test_fastapi/test_response_model/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_response_model/test_tutorial001_py39.py [deleted file]
tests/test_tutorial/test_fastapi/test_session_with_dependency/test_tutorial001.py
tests/test_tutorial/test_fastapi/test_session_with_dependency/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_session_with_dependency/test_tutorial001_py39.py [deleted file]
tests/test_tutorial/test_fastapi/test_simple_hero_api/test_tutorial001.py
tests/test_tutorial/test_fastapi/test_simple_hero_api/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_teams/test_tutorial001.py
tests/test_tutorial/test_fastapi/test_teams/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_fastapi/test_teams/test_tutorial001_py39.py [deleted file]