Spaces:
Sleeping
Sleeping
File size: 5,907 Bytes
2b18225 | 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | """
Per-organization Sheet sync: each university's Sheet feeds its OWN collection.
The property that matters is isolation — University A editing their Sheet must
never change what University B's students are told. These tests use fake
engines and a fake db rather than a real Sheet or ChromaDB, because what's
being proven is the routing decision (which Sheet went to which collection),
not the embedding behaviour that test_org_registry.py already covers against
real ChromaDB.
"""
import sys
import types
import pytest
class _FakeCollection:
"""Records what was written so a test can assert on it."""
def __init__(self):
self.upserts = []
def upsert(self, ids=None, documents=None, metadatas=None, **kwargs):
self.upserts.append({"ids": ids, "documents": documents, "metadatas": metadatas})
def get(self, **kwargs):
return {"ids": [], "metadatas": []}
def delete(self, **kwargs):
pass
class _FakeEngine:
def __init__(self, name):
self.name = name
self.collection = _FakeCollection()
@pytest.fixture
def fake_world(monkeypatch, tmp_path):
"""Two organizations, each with their own Sheet and their own engine."""
import sheet_sync
# Version markers must not collide between orgs; keep them in tmp_path.
monkeypatch.setattr(sheet_sync, "DATA_DIR", tmp_path)
monkeypatch.setattr(sheet_sync, "VERSION_FILE", tmp_path / "sheet_version.json")
engines = {"org-a": _FakeEngine("org-a"), "org-b": _FakeEngine("org-b")}
fake_db = types.ModuleType("db")
fake_db.list_organizations_with_sheets = lambda: [
{"id": "org-a", "name": "University A", "sheet_url": "https://example.test/a"},
{"id": "org-b", "name": "University B", "sheet_url": "https://example.test/b"},
]
monkeypatch.setitem(sys.modules, "db", fake_db)
fake_registry = types.ModuleType("org_registry")
fake_registry.get_engine_for_org = lambda org_id, alu_organization_id=None: engines[org_id]
monkeypatch.setitem(sys.modules, "org_registry", fake_registry)
# Each org's Sheet returns content naming that org, so a mix-up is visible.
payloads = {
"https://example.test/a": {
"version": "va",
"entries": [{"id": "1", "title": "A deadline", "content": "University A add/drop is Sept 12"}],
},
"https://example.test/b": {
"version": "vb",
"entries": [{"id": "1", "title": "B deadline", "content": "University B add/drop is Oct 3"}],
},
}
monkeypatch.setattr(
sheet_sync.SheetSync, "_fetch_payload", lambda self: payloads[self.json_url]
)
return engines
def test_each_organizations_sheet_lands_in_its_own_collection(fake_world):
import org_sheet_sync
summary = org_sheet_sync.sync_all_organizations()
assert summary["ok"], summary
a_text = str(fake_world["org-a"].collection.upserts)
b_text = str(fake_world["org-b"].collection.upserts)
# Each collection has its own university's content...
assert "University A" in a_text
assert "University B" in b_text
# ...and crucially, not the other's.
assert "University B" not in a_text
assert "University A" not in b_text
def test_one_organizations_failure_does_not_stop_the_others(fake_world, monkeypatch):
import org_sheet_sync
import sheet_sync
original = sheet_sync.SheetSync.refresh
def _fail_for_a(self, force=False):
if self.organization_id == "org-a":
raise RuntimeError("org-a's sheet is unreachable")
return original(self, force)
monkeypatch.setattr(sheet_sync.SheetSync, "refresh", _fail_for_a)
summary = org_sheet_sync.sync_all_organizations()
assert summary["ok"] is False
assert summary["failed"] == 1
assert summary["synced"] == 1
# B still got its content despite A failing.
assert "University B" in str(fake_world["org-b"].collection.upserts)
def test_organizations_get_separate_version_markers(fake_world, tmp_path):
"""A shared marker would make org B look 'unchanged' after org A synced."""
from sheet_sync import SheetSync
a = SheetSync(fake_world["org-a"], json_url="https://example.test/a", organization_id="org-a")
b = SheetSync(fake_world["org-b"], json_url="https://example.test/b", organization_id="org-b")
assert a._version_file != b._version_file
a.refresh(force=False)
# B must still see itself as unsynced, not inherit A's version.
assert b._read_version() is None
def test_unchanged_sheet_is_skipped_on_the_second_run(fake_world):
"""Re-embedding an unchanged Sheet is the main avoidable cost here."""
import org_sheet_sync
first = org_sheet_sync.sync_all_organizations()
assert all(not o["result"]["skipped_unchanged"] for o in first["organizations"])
second = org_sheet_sync.sync_all_organizations()
assert all(o["result"]["skipped_unchanged"] for o in second["organizations"])
def test_no_configured_sheets_is_a_normal_empty_result(monkeypatch):
"""An organization without a Sheet is supported, not an error."""
import org_sheet_sync
fake_db = types.ModuleType("db")
fake_db.list_organizations_with_sheets = lambda: []
monkeypatch.setitem(sys.modules, "db", fake_db)
summary = org_sheet_sync.sync_all_organizations()
assert summary == {"ok": True, "organizations": []}
def test_database_failure_does_not_raise(monkeypatch):
"""Called from a startup hook and a poll loop; it must never take them down."""
import org_sheet_sync
fake_db = types.ModuleType("db")
def _boom():
raise RuntimeError("aurora unreachable")
fake_db.list_organizations_with_sheets = _boom
monkeypatch.setitem(sys.modules, "db", fake_db)
summary = org_sheet_sync.sync_all_organizations()
assert summary["ok"] is False
assert "aurora unreachable" in summary["error"]
|