Coverage for src/cstlcore/ydocs/services.py: 58%
19 statements
« prev ^ index » next coverage.py v7.9.1, created at 2026-02-19 12:46 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2026-02-19 12:46 +0000
1from typing import Optional
2from uuid import UUID
4from sqlmodel import Session, select
6from cstlcore.ydocs.models import YDoc
9def get_ydoc_path(ydoc: YDoc) -> str:
10 """Get the full path of a YDoc within its collection."""
11 parts = []
12 current = ydoc
13 while current is not None:
14 parts.append(current.name)
15 current = current.parent
16 parts.reverse()
17 return "/" + "/".join(parts)
20def ydoc_exists(
21 session: Session,
22 collection_id: UUID,
23 name: str,
24 parent_id: Optional[UUID] = None,
25 exclude_id: Optional[UUID] = None,
26) -> bool:
27 """Return True if a YDoc with (collection_id, parent_id, name) exists.
29 This uses a single SQL WHERE clause. SQLAlchemy treats `column == None`
30 as `IS NULL`, so we can simply compare `YDoc.parent_id == parent_id` and
31 avoid calling `is_` explicitly (which caused static-analysis noise).
33 If `exclude_id` is provided, the matching row with that id is ignored
34 (useful when checking uniqueness during updates).
35 """
36 if not name:
37 return False
39 stmt = select(YDoc).where(
40 YDoc.collection_id == collection_id,
41 YDoc.name == name,
42 YDoc.parent_id == parent_id,
43 )
44 if exclude_id is not None:
45 stmt = stmt.where(YDoc.id != exclude_id)
47 return session.exec(stmt).first() is not None