Spaces:
Sleeping
Sleeping
Download test_org_sheet_sync.py from StudentCompanion/alu-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 5.91 kB
-
https://huggingface.co/spaces/StudentCompanion/alu-chatbot/resolve/main/test_org_sheet_sync.py
- Command line
-
hf download hf://spaces/StudentCompanion/alu-chatbot/test_org_sheet_sync.py
-
curl -L -o test_org_sheet_sync.py https://huggingface.co/spaces/StudentCompanion/alu-chatbot/resolve/main/test_org_sheet_sync.py
5.91 kB
| """ | |
| 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() | |
| 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"] | |