Buckets:

download
raw
6.95 kB
"""Tests for soft-delete query helpers in tessera.db.queries."""
from datetime import UTC, datetime
from uuid import uuid4
import pytest
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from tessera.db.models import (
AssetDB,
AssetDependencyDB,
AuditEventDB,
RepoDB,
ServiceDB,
TeamDB,
UserDB,
)
from tessera.db.queries import (
active_dependencies,
active_only,
active_repos,
active_services,
active_users,
)
pytestmark = pytest.mark.asyncio
async def _create_team(session: AsyncSession) -> TeamDB:
"""Create a minimal team for FK references."""
team = TeamDB(name=f"team-{uuid4().hex[:8]}")
session.add(team)
await session.flush()
await session.refresh(team)
return team
class TestActiveOnly:
"""Tests for the generic active_only helper."""
async def test_filters_deleted_at_model(self, test_session: AsyncSession) -> None:
"""Filters out rows where deleted_at is set on a deleted_at model."""
team = await _create_team(test_session)
active_repo = RepoDB(
name="active-repo",
git_url="https://github.com/org/active",
owner_team_id=team.id,
)
deleted_repo = RepoDB(
name="deleted-repo",
git_url="https://github.com/org/deleted",
owner_team_id=team.id,
deleted_at=datetime.now(UTC),
)
test_session.add_all([active_repo, deleted_repo])
await test_session.flush()
query = active_only(select(RepoDB), RepoDB)
result = await test_session.execute(query)
repos = result.scalars().all()
repo_names = [r.name for r in repos]
assert "active-repo" in repo_names
assert "deleted-repo" not in repo_names
async def test_filters_deactivated_at_model(self, test_session: AsyncSession) -> None:
"""Filters out rows where deactivated_at is set on a deactivated_at model."""
active_user = UserDB(
username=f"active-{uuid4().hex[:8]}",
name="Active User",
)
deactivated_user = UserDB(
username=f"deactivated-{uuid4().hex[:8]}",
name="Deactivated User",
deactivated_at=datetime.now(UTC),
)
test_session.add_all([active_user, deactivated_user])
await test_session.flush()
query = active_only(select(UserDB), UserDB)
result = await test_session.execute(query)
users = result.scalars().all()
usernames = [u.username for u in users]
assert active_user.username in usernames
assert deactivated_user.username not in usernames
async def test_passthrough_for_model_without_soft_delete(
self, test_session: AsyncSession
) -> None:
"""Returns query unchanged for models with no soft-delete column."""
base_query = select(AuditEventDB)
filtered = active_only(base_query, AuditEventDB)
assert filtered is base_query
class TestActiveDependencies:
"""Tests for active_dependencies helper."""
async def test_excludes_soft_deleted(self, test_session: AsyncSession) -> None:
"""Returns only non-deleted dependencies."""
team = await _create_team(test_session)
asset_a = AssetDB(fqn=f"a-{uuid4().hex[:8]}", owner_team_id=team.id)
asset_b = AssetDB(fqn=f"b-{uuid4().hex[:8]}", owner_team_id=team.id)
test_session.add_all([asset_a, asset_b])
await test_session.flush()
await test_session.refresh(asset_a)
await test_session.refresh(asset_b)
active_dep = AssetDependencyDB(
dependent_asset_id=asset_a.id,
dependency_asset_id=asset_b.id,
)
deleted_dep = AssetDependencyDB(
dependent_asset_id=asset_b.id,
dependency_asset_id=asset_a.id,
deleted_at=datetime.now(UTC),
)
test_session.add_all([active_dep, deleted_dep])
await test_session.flush()
result = await test_session.execute(active_dependencies())
deps = result.scalars().all()
dep_ids = [d.id for d in deps]
assert active_dep.id in dep_ids
assert deleted_dep.id not in dep_ids
class TestActiveRepos:
"""Tests for active_repos helper."""
async def test_excludes_soft_deleted(self, test_session: AsyncSession) -> None:
"""Returns only non-deleted repos."""
team = await _create_team(test_session)
alive = RepoDB(
name="alive-repo",
git_url="https://github.com/org/alive",
owner_team_id=team.id,
)
gone = RepoDB(
name="gone-repo",
git_url="https://github.com/org/gone",
owner_team_id=team.id,
deleted_at=datetime.now(UTC),
)
test_session.add_all([alive, gone])
await test_session.flush()
result = await test_session.execute(active_repos())
repos = result.scalars().all()
names = [r.name for r in repos]
assert "alive-repo" in names
assert "gone-repo" not in names
class TestActiveServices:
"""Tests for active_services helper."""
async def test_excludes_soft_deleted(self, test_session: AsyncSession) -> None:
"""Returns only non-deleted services."""
team = await _create_team(test_session)
repo = RepoDB(
name="svc-repo",
git_url="https://github.com/org/svc",
owner_team_id=team.id,
)
test_session.add(repo)
await test_session.flush()
await test_session.refresh(repo)
alive = ServiceDB(name="alive-svc", repo_id=repo.id)
gone = ServiceDB(
name="gone-svc",
repo_id=repo.id,
deleted_at=datetime.now(UTC),
)
test_session.add_all([alive, gone])
await test_session.flush()
result = await test_session.execute(active_services())
svcs = result.scalars().all()
names = [s.name for s in svcs]
assert "alive-svc" in names
assert "gone-svc" not in names
class TestActiveUsers:
"""Tests for active_users helper."""
async def test_excludes_deactivated(self, test_session: AsyncSession) -> None:
"""Returns only non-deactivated users."""
active = UserDB(
username=f"active-{uuid4().hex[:8]}",
name="Active",
)
deactivated = UserDB(
username=f"gone-{uuid4().hex[:8]}",
name="Gone",
deactivated_at=datetime.now(UTC),
)
test_session.add_all([active, deactivated])
await test_session.flush()
result = await test_session.execute(active_users())
users = result.scalars().all()
usernames = [u.username for u in users]
assert active.username in usernames
assert deactivated.username not in usernames

Xet Storage Details

Size:
6.95 kB
·
Xet hash:
462273006b8b9d6f9dc8550e8fe4c2fbbe666de845cd794cc63fd5153deb1eef

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.