refactor(db)!: finalize pooled WNDB v2 migration
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
@@ -12,6 +14,7 @@ from app.auth.metadata_dependencies import (
|
||||
)
|
||||
from app.infra.db.metadb.repositories.metadata_repository import ProjectDbRouting
|
||||
from app.infra.db.project_routing import get_project_pgconn_string
|
||||
from app.native.wndb.core.database import MaterializedViewRefreshAfterCommitError
|
||||
from tests.conftest import build_test_app
|
||||
|
||||
|
||||
@@ -144,3 +147,47 @@ def test_model_update_uses_project_business_routing(monkeypatch):
|
||||
"dsn": "postgresql://user:password@biz.example/routed_business",
|
||||
}
|
||||
repo.get_project_db_routing.assert_awaited_once_with(project_id, "biz_data")
|
||||
|
||||
|
||||
def test_gb18030_upload_is_normalized_to_utf8() -> None:
|
||||
content = "[TITLE]\n天津供水\n[JUNCTIONS]\n".encode("gb18030")
|
||||
|
||||
class FakeUpload:
|
||||
filename = "model.inp"
|
||||
|
||||
async def read(self, _limit: int) -> bytes:
|
||||
return content
|
||||
|
||||
normalized, filename = asyncio.run(model_import._read_upload(FakeUpload()))
|
||||
|
||||
assert filename == "model.inp"
|
||||
assert normalized.decode("utf-8") == "[TITLE]\n天津供水\n[JUNCTIONS]\n"
|
||||
|
||||
|
||||
def test_model_update_runs_blocking_import_in_threadpool(monkeypatch) -> None:
|
||||
calls: list[tuple[object, tuple[object, ...]]] = []
|
||||
|
||||
async def fake_threadpool(function, *args):
|
||||
calls.append((function, args))
|
||||
|
||||
monkeypatch.setattr(model_import, "run_in_threadpool", fake_threadpool)
|
||||
|
||||
asyncio.run(model_import._update_from_inp(b"[TITLE]\n", "demo"))
|
||||
|
||||
assert calls == [(model_import._update_from_inp_sync, (b"[TITLE]\n", "demo"))]
|
||||
|
||||
|
||||
def test_committed_refresh_failure_is_not_wrapped_as_retryable_500(
|
||||
monkeypatch,
|
||||
) -> None:
|
||||
error = MaterializedViewRefreshAfterCommitError("demo")
|
||||
|
||||
async def fail_update(_content: bytes, _project_code: str) -> None:
|
||||
raise error
|
||||
|
||||
monkeypatch.setattr(model_import, "_update_from_inp", fail_update)
|
||||
|
||||
with pytest.raises(MaterializedViewRefreshAfterCommitError) as exc_info:
|
||||
asyncio.run(model_import._apply_model_update(b"[TITLE]\n", "demo"))
|
||||
|
||||
assert exc_info.value is error
|
||||
|
||||
Reference in New Issue
Block a user