mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-17 20:01:41 +08:00
![google-labs-jules[bot]](/assets/img/avatar_default.png)
This commit applies minor cleanups to the previously consolidated test files. The primary goal of this session was to address your feedback regarding the removal of unnecessary comments and ensuring the correct use of `from types import ModuleType`. **Summary of Actions during this cleanup session:** 1. **Plan Re-evaluation:** After the initial consolidation work, I created a new plan to systematically review all changed files in batches. 2. **Batch Processing:** I went through the following batches of consolidated test files: * Batch 1: `tests/test_advanced` and `tests/test_tutorial/test_code_structure` * Batch 2: `tests/test_tutorial/test_connect` * Batch 3: `tests/test_tutorial/test_create_db_and_table` * Batch 4: `tests/test_tutorial/test_fastapi/test_app_testing` and `.../test_delete` * Batch 5: `.../test_limit_and_offset` and `.../test_multiple_models` * Batch 6: `.../test_read_one` and `.../test_relationships` * Batch 7: `.../test_response_model` and `.../test_session_with_dependency` * Batch 8 (partially): `.../test_teams/test_tutorial001.py` was processed. `.../test_simple_hero_api/test_tutorial001.py` was identified as missed before this submission. 3. **Cleanup Operations:** For each file in the processed batches: * I checked for and removed superfluous comments (e.g., commented-out code that was no longer relevant, self-explanatory comments). Many files were already quite clean. * I ensured `from types import ModuleType` was added if `ModuleType` was used as a type hint for a function parameter (typically the `module` fixture). * I corrected type hints from `type` to `types.ModuleType` where applicable. 4. **Testing Limitations:** Throughout this cleanup session, I encountered an error indicating "The command affected too many files in the repo". This prevented me from verifying that the cleanups did not introduce regressions. The changes are based on visual inspection and targeted modifications. **Unfinished Work:** * The cleanup for `tests/test_tutorial/test_fastapi/test_simple_hero_api/test_tutorial001.py` was missed in Batch 8. * Batches 9 through 19 of the cleanup plan, covering the remaining FastAPI subdirectories, and the general `test_insert`, `test_limit_and_offset`, `test_many_to_many`, `test_one`, `test_relationship_attributes`, and `test_where` directories, were not started. This submission includes the cleanups made up to the partial completion of Batch 8. Further cleanup and full verification are still pending.
80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
import importlib
|
|
import sys
|
|
import types
|
|
from typing import Any
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from sqlalchemy.exc import MultipleResultsFound # Keep this import
|
|
from sqlmodel import create_engine, SQLModel, Session, delete # Ensure Session and delete are imported
|
|
|
|
from ...conftest import get_testing_print_function, needs_py310, PrintMock
|
|
|
|
|
|
expected_calls_tutorial004 = [
|
|
[
|
|
"Hero:",
|
|
{
|
|
"id": 1, # Assuming ID will be 1 after clearing and adding one hero
|
|
"name": "Test Hero",
|
|
"secret_name": "Secret Test Hero",
|
|
"age": 24,
|
|
},
|
|
]
|
|
]
|
|
|
|
|
|
@pytest.fixture(
|
|
name="module",
|
|
params=[
|
|
"tutorial004",
|
|
pytest.param("tutorial004_py310", marks=needs_py310),
|
|
],
|
|
)
|
|
def module_fixture(request: pytest.FixtureRequest, clear_sqlmodel: Any):
|
|
module_name = request.param
|
|
full_module_name = f"docs_src.tutorial.one.{module_name}"
|
|
|
|
if full_module_name in sys.modules:
|
|
mod = importlib.reload(sys.modules[full_module_name])
|
|
else:
|
|
mod = importlib.import_module(full_module_name)
|
|
|
|
mod.sqlite_url = "sqlite://"
|
|
mod.engine = create_engine(mod.sqlite_url)
|
|
|
|
# Table creation is crucial here because the test interacts with the DB
|
|
# before calling main() in some cases (to clean up, then assert specific state).
|
|
# The main() function in tutorial004.py is expected to cause MultipleResultsFound,
|
|
# which implies tables and data should exist *before* main() is called for that specific check.
|
|
# The original test calls main() first, then manipulates DB.
|
|
# The fixture should ensure tables are ready.
|
|
if hasattr(mod, "SQLModel") and hasattr(mod.SQLModel, "metadata"):
|
|
mod.SQLModel.metadata.create_all(mod.engine)
|
|
|
|
return mod
|
|
|
|
|
|
def test_tutorial(module: types.ModuleType, print_mock: PrintMock, clear_sqlmodel: Any):
|
|
# The module.main() in tutorial004.py is designed to initially create heroes,
|
|
# then try to select one which results in MultipleResultsFound.
|
|
# It also defines select_heroes() which is called later.
|
|
|
|
# First, let main() run to create initial data and trigger the expected exception.
|
|
# The create_db_and_tables is called within main() in docs_src/tutorial/one/tutorial004.py
|
|
with pytest.raises(MultipleResultsFound):
|
|
module.main() # This function in the tutorial is expected to raise this
|
|
|
|
# After the expected exception, the original test clears the Hero table and adds a specific hero.
|
|
with Session(module.engine) as session:
|
|
# The delete statement needs the actual Hero class from the module
|
|
session.exec(delete(module.Hero))
|
|
session.add(module.Hero(name="Test Hero", secret_name="Secret Test Hero", age=24))
|
|
session.commit()
|
|
|
|
# Now, test the select_heroes function part
|
|
with patch("builtins.print", new=get_testing_print_function(print_mock.calls)):
|
|
module.select_heroes() # This function is defined in the tutorial module
|
|
|
|
assert print_mock.calls == expected_calls_tutorial004
|