Spaces:
Sleeping
Sleeping
File size: 4,930 Bytes
4b6e3ab 661a924 4b6e3ab 661a924 4b6e3ab 661a924 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import pytest
from src.repositories.state_safety import StateSafetyError
from src.repositories.supabase_state import SupabaseBootstrapStore
class FakeSupabaseClient:
def __init__(self) -> None:
self.tables: dict[str, list[dict[str, object]]] = {
"projects": [
{
"project_id": "old-id",
"project_slug": "old-project",
"name": "Old Project",
"dataset_repo_id": "org/old-project",
"visibility": "collaborative",
"owner_username": "owner",
"dataset_token": None,
"active": True,
}
],
"user_project_access": [],
"pending_invites": [],
}
self.patches: list[tuple[str, dict[str, object], dict[str, str]]] = []
def select(self, table: str, *, query: dict[str, str] | None = None) -> list[dict[str, object]]:
_ = query
return [dict(row) for row in self.tables.get(table, [])]
def upsert(self, table: str, rows: list[dict[str, object]], *, on_conflict: str) -> None:
_ = on_conflict
if table != "projects":
self.tables[table] = [dict(row) for row in rows]
return
by_slug = {
str(row.get("project_slug") or ""): row
for row in self.tables.get("projects", [])
}
for row in rows:
by_slug[str(row.get("project_slug") or "")] = dict(row)
self.tables["projects"] = list(by_slug.values())
def insert(self, table: str, row: dict[str, object]) -> None:
self.tables.setdefault(table, []).append(dict(row))
def patch(self, table: str, row: dict[str, object], *, query: dict[str, str]) -> None:
self.patches.append((table, dict(row), dict(query)))
if table != "projects":
return
project_slug_filter = str(query.get("project_slug") or "")
if not project_slug_filter.startswith("eq."):
return
project_slug = project_slug_filter[3:]
for existing in self.tables.get("projects", []):
if existing.get("project_slug") == project_slug:
existing.update(row)
def delete(self, table: str, *, query: dict[str, str]) -> None:
_ = query
self.tables[table] = []
def test_supabase_persist_deactivates_projects_missing_from_current_state() -> None:
client = FakeSupabaseClient()
store = SupabaseBootstrapStore(client) # type: ignore[arg-type]
store.persist(
projects=[
{
"project_id": "new-id",
"project_slug": "new-project",
"name": "New Project",
"dataset_repo_id": "org/new-project",
"visibility": "collaborative",
"owner_username": "owner",
"dataset_token": None,
"active": True,
}
],
user_access={"owner": {"new-project": "admin"}},
invites={},
allowed_removed_project_slugs={"old-project"},
)
old_project = next(row for row in client.tables["projects"] if row["project_slug"] == "old-project")
assert old_project["active"] is False
assert any(table == "projects" and row["active"] is False for table, row, _ in client.patches)
def test_supabase_persist_blocks_unplanned_project_removal() -> None:
client = FakeSupabaseClient()
store = SupabaseBootstrapStore(client) # type: ignore[arg-type]
with pytest.raises(StateSafetyError):
store.persist(
projects=[
{
"project_id": "new-id",
"project_slug": "new-project",
"name": "New Project",
"dataset_repo_id": "org/new-project",
"visibility": "collaborative",
"owner_username": "owner",
"dataset_token": None,
"active": True,
}
],
user_access={"owner": {"new-project": "admin"}},
invites={},
)
assert client.tables["projects"][0]["active"] is True
def test_supabase_load_projects_skips_inactive_rows() -> None:
client = FakeSupabaseClient()
client.tables["projects"].append(
{
"project_id": "active-id",
"project_slug": "active-project",
"name": "Active Project",
"dataset_repo_id": "org/active-project",
"visibility": "collaborative",
"owner_username": "owner",
"dataset_token": None,
"active": True,
}
)
client.tables["projects"][0]["active"] = False
store = SupabaseBootstrapStore(client) # type: ignore[arg-type]
projects = store.load_projects()
assert [project.project_slug for project in projects] == ["active-project"]
|