mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-16 03:06:29 +08:00
![google-labs-jules[bot]](/assets/img/avatar_default.png)
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.