Skip to content

Commit 05efe87

Browse files
phernandezclaude
andcommitted
test: Verify update() returns entity with eager-loaded relations
Add test confirming entity_repository.update() returns the entity with observations and relations eagerly loaded, eliminating the need for a separate find_by_id() call after update. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 0eaf30b commit 05efe87

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

tests/repository/test_entity_repository.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,54 @@ async def test_update_entity(entity_repository: EntityRepository, sample_entity:
176176
assert db_entity.title == "Updated title"
177177

178178

179+
@pytest.mark.asyncio
180+
async def test_update_entity_returns_with_relations_and_observations(
181+
entity_repository: EntityRepository, entity_with_observations, test_project: Project
182+
):
183+
"""Test that update() returns entity with observations and relations eagerly loaded."""
184+
entity = entity_with_observations
185+
186+
# Create a target entity and relation
187+
async with db.scoped_session(entity_repository.session_maker) as session:
188+
target = Entity(
189+
project_id=test_project.id,
190+
title="target",
191+
entity_type="test",
192+
permalink="target/target",
193+
file_path="target/target.md",
194+
content_type="text/markdown",
195+
created_at=datetime.now(timezone.utc),
196+
updated_at=datetime.now(timezone.utc),
197+
)
198+
session.add(target)
199+
await session.flush()
200+
201+
relation = Relation(
202+
from_id=entity.id,
203+
to_id=target.id,
204+
to_name=target.title,
205+
relation_type="connects_to",
206+
)
207+
session.add(relation)
208+
209+
# Now update the entity
210+
updated = await entity_repository.update(entity.id, {"title": "Updated with relations"})
211+
212+
# Verify returned entity has observations and relations accessible
213+
# (would raise DetachedInstanceError if not eagerly loaded)
214+
assert updated is not None
215+
assert updated.title == "Updated with relations"
216+
217+
# Access observations - should NOT raise DetachedInstanceError
218+
assert len(updated.observations) == 2
219+
assert updated.observations[0].content in ["First observation", "Second observation"]
220+
221+
# Access relations - should NOT raise DetachedInstanceError
222+
assert len(updated.relations) == 1
223+
assert updated.relations[0].relation_type == "connects_to"
224+
assert updated.relations[0].to_name == "target"
225+
226+
179227
@pytest.mark.asyncio
180228
async def test_delete_entity(entity_repository: EntityRepository, sample_entity):
181229
"""Test deleting an entity."""

0 commit comments

Comments
 (0)