Skip to content

Commit 307d00b

Browse files
committed
test: Add unit tests for engine property accessor
Adds three test cases to verify the engine property behavior: 1. test_engine_property_from_url: Verifies that the engine property returns the underlying AsyncEngine when created via from_url() and can be used for advanced operations like manual disposal 2. test_engine_property_from_external_engine: Verifies that the engine property returns the same instance when an external engine is injected, confirming users retain control 3. test_engine_property_is_read_only: Verifies that the engine property is read-only and cannot be reassigned, ensuring immutability of the engine reference
1 parent 74e1d74 commit 307d00b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tests/extensions/memory/test_sqlalchemy_session.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Summary,
1515
)
1616
from sqlalchemy import select, text, update
17+
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
1718
from sqlalchemy.sql import Select
1819

1920
pytest.importorskip("sqlalchemy") # Skip tests if SQLAlchemy is not installed
@@ -390,3 +391,55 @@ async def recording_execute(statement: Any, *args: Any, **kwargs: Any) -> Any:
390391

391392
assert _item_ids(retrieved_full) == ["rs_first", "msg_second"]
392393
assert _item_ids(retrieved_limited) == ["rs_first", "msg_second"]
394+
395+
396+
async def test_engine_property_from_url():
397+
"""Test that the engine property returns the underlying AsyncEngine when created via from_url."""
398+
session_id = "engine_property_test"
399+
session = SQLAlchemySession.from_url(session_id, url=DB_URL, create_tables=True)
400+
401+
# Verify engine property returns an AsyncEngine instance
402+
assert isinstance(session.engine, AsyncEngine)
403+
404+
# Verify we can use the engine for advanced operations
405+
# For example, check pool status
406+
assert session.engine.pool is not None
407+
408+
# Verify we can manually dispose the engine
409+
await session.engine.dispose()
410+
411+
412+
async def test_engine_property_from_external_engine():
413+
"""Test that the engine property returns the external engine when created with an injected engine."""
414+
session_id = "external_engine_test"
415+
416+
# Create engine externally
417+
external_engine = create_async_engine(DB_URL)
418+
419+
# Create session with external engine
420+
session = SQLAlchemySession(session_id, engine=external_engine, create_tables=True)
421+
422+
# Verify engine property returns the same engine instance
423+
assert session.engine is external_engine
424+
425+
# Verify we can use the engine
426+
assert isinstance(session.engine, AsyncEngine)
427+
428+
# Clean up - user is responsible for disposing external engine
429+
await external_engine.dispose()
430+
431+
432+
async def test_engine_property_is_read_only():
433+
"""Test that the engine property cannot be modified."""
434+
session_id = "readonly_engine_test"
435+
session = SQLAlchemySession.from_url(session_id, url=DB_URL, create_tables=True)
436+
437+
# Verify engine property exists
438+
assert hasattr(session, "engine")
439+
440+
# Verify it's a property (read-only, cannot be set)
441+
with pytest.raises(AttributeError):
442+
session.engine = create_async_engine(DB_URL)
443+
444+
# Clean up
445+
await session.engine.dispose()

0 commit comments

Comments
 (0)