fix tests

This commit is contained in:
phernandez
2024-12-24 20:29:21 -06:00
parent d713097333
commit 90ca41fead
16 changed files with 137 additions and 138 deletions
@@ -79,8 +79,12 @@ class EntityRepository(Repository[Entity]):
def get_load_options(self) -> List[LoaderOption]:
return [
selectinload(Entity.observations),
# Load from_relations and both entities for each relation
selectinload(Entity.from_relations).selectinload(Relation.from_entity),
selectinload(Entity.from_relations).selectinload(Relation.to_entity),
# Load to_relations and both entities for each relation
selectinload(Entity.to_relations).selectinload(Relation.from_entity),
selectinload(Entity.to_relations).selectinload(Relation.to_entity),
]
async def find_by_path_ids(self, path_ids: List[str]) -> Sequence[Entity]:
@@ -1,10 +1,10 @@
"""Repository for managing Relation objects."""
from typing import Sequence, List
from sqlalchemy import and_
from typing import Sequence, List, Optional
from sqlalchemy import select
from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.orm import selectinload
from sqlalchemy.orm import selectinload, aliased
from sqlalchemy.orm.interfaces import LoaderOption
from basic_memory.models import Relation, Entity
@@ -17,6 +17,25 @@ class RelationRepository(Repository[Relation]):
def __init__(self, session_maker: async_sessionmaker):
super().__init__(session_maker, Relation)
async def find_relation(self, from_path_id: str, to_path_id: str, relation_type: str) -> Optional[Relation]:
"""Find a relation by its from and to path IDs."""
from_entity = aliased(Entity)
to_entity = aliased(Entity)
query = (
select(Relation)
.join(from_entity, Relation.from_id == from_entity.id)
.join(to_entity, Relation.to_id == to_entity.id)
.where(
and_(
from_entity.path_id == from_path_id,
to_entity.path_id == to_path_id,
Relation.relation_type == relation_type
)
)
)
return await self.find_one(query)
async def find_by_entity(self, from_entity_id: int) -> Sequence[Relation]:
"""Find all relations from a specific entity."""
query = select(Relation).filter(Relation.from_id == from_entity_id)