feat: bulk upsert API with idempotent writes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-05 14:54:33 +08:00
parent efc8c474fc
commit 0b94b6765d
5 changed files with 310 additions and 1 deletions

41
tests/test_api.py Normal file
View File

@@ -0,0 +1,41 @@
import pytest
from openclaw.models import Session
BULK_URL = "/api/sessions/bulk_upsert/"
def _minimal_payload():
return {
"agent_name": "test",
"source_node": "macmini",
"sessions": [
{
"session_id": "test-session",
"model_provider": "test",
"model_id": "test-model",
}
],
"messages": [],
"tool_calls": [],
}
@pytest.mark.django_db
class TestBulkUpsertAPI:
def test_bulk_upsert_ok(self, client):
resp = client.post(BULK_URL, _minimal_payload(), content_type="application/json")
assert resp.status_code == 200
data = resp.json()
assert data["status"] == "ok"
assert data["sessions_upserted"] == 1
def test_bulk_upsert_idempotent(self, client):
client.post(BULK_URL, _minimal_payload(), content_type="application/json")
resp = client.post(BULK_URL, _minimal_payload(), content_type="application/json")
data = resp.json()
assert data["sessions_upserted"] == 0
def test_bulk_upsert_missing_fields_returns_400(self, client):
resp = client.post(BULK_URL, {}, content_type="application/json")
assert resp.status_code == 400