mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-18 04:10:40 +08:00
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
This commit is contained in:
@ -5,17 +5,20 @@ 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
|
||||
from sqlalchemy.exc import MultipleResultsFound # Keep this import
|
||||
from sqlmodel import ( # Ensure Session and delete are imported
|
||||
Session,
|
||||
create_engine,
|
||||
delete,
|
||||
)
|
||||
|
||||
from ...conftest import PrintMock, get_testing_print_function, needs_py310
|
||||
|
||||
expected_calls_tutorial004 = [
|
||||
[
|
||||
"Hero:",
|
||||
{
|
||||
"id": 1, # Assuming ID will be 1 after clearing and adding one hero
|
||||
"id": 1, # Assuming ID will be 1 after clearing and adding one hero
|
||||
"name": "Test Hero",
|
||||
"secret_name": "Secret Test Hero",
|
||||
"age": 24,
|
||||
@ -50,7 +53,7 @@ def module_fixture(request: pytest.FixtureRequest, clear_sqlmodel: Any):
|
||||
# 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)
|
||||
mod.SQLModel.metadata.create_all(mod.engine)
|
||||
|
||||
return mod
|
||||
|
||||
@ -63,17 +66,19 @@ def test_tutorial(module: types.ModuleType, print_mock: PrintMock, clear_sqlmode
|
||||
# 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
|
||||
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.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
|
||||
module.select_heroes() # This function is defined in the tutorial module
|
||||
|
||||
assert print_mock.calls == expected_calls_tutorial004
|
||||
|
Reference in New Issue
Block a user