104 lines
3.3 KiB
Python
104 lines
3.3 KiB
Python
"""
|
|
Tests for GenAI Config CRUD operations.
|
|
"""
|
|
import io
|
|
import pytest
|
|
|
|
# Dummy PEM key for OCI config prerequisite
|
|
_DUMMY_PEM = b"""-----BEGIN RSA PRIVATE KEY-----
|
|
MIIBogIBAAJBALRiMLAHudeSA/x3hB2f+2NRkJmcOUNKwNuvTT0GKQFYSNDF0FNv
|
|
TESTDATAFORTHEMOCKPEMFILEXYZABC0123456789ABCDEF0123456789
|
|
-----END RSA PRIVATE KEY-----
|
|
"""
|
|
|
|
_state: dict = {}
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
async def oci_config_id(client, admin_headers):
|
|
"""Create an OCI config that GenAI configs can reference."""
|
|
r = await client.post(
|
|
"/api/oci/config",
|
|
data={
|
|
"tenancy_name": "GenAI-OCI-Dep",
|
|
"tenancy_ocid": "ocid1.tenancy.oc1..genaidep",
|
|
"user_ocid": "ocid1.user.oc1..genaidep",
|
|
"fingerprint": "11:22:33:44",
|
|
"region": "us-ashburn-1",
|
|
"auth_type": "api_key",
|
|
},
|
|
files={"private_key": ("key.pem", io.BytesIO(_DUMMY_PEM), "application/x-pem-file")},
|
|
headers=admin_headers,
|
|
)
|
|
assert r.status_code == 200, r.text
|
|
cid = r.json()["id"]
|
|
yield cid
|
|
await client.delete(f"/api/oci/configs/{cid}", headers=admin_headers)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_get_models(client, admin_headers):
|
|
"""GET /api/genai/models — returns models dict and regions list."""
|
|
r = await client.get("/api/genai/models", headers=admin_headers)
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert "models" in body and isinstance(body["models"], dict)
|
|
assert "regions" in body and isinstance(body["regions"], list)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_create_genai_config(client, admin_headers, oci_config_id):
|
|
"""POST /api/genai/config — create via JSON body."""
|
|
r = await client.post(
|
|
"/api/genai/config",
|
|
json={
|
|
"name": "TestGenAI",
|
|
"oci_config_id": oci_config_id,
|
|
"model_id": "openai.gpt-4.1",
|
|
"compartment_id": "ocid1.compartment.oc1..test",
|
|
"genai_region": "us-ashburn-1",
|
|
},
|
|
headers=admin_headers,
|
|
)
|
|
assert r.status_code == 200, r.text
|
|
body = r.json()
|
|
assert "id" in body
|
|
_state["gid"] = body["id"]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_list_genai_configs(client, admin_headers):
|
|
"""GET /api/genai/configs — list must include created config."""
|
|
r = await client.get("/api/genai/configs", headers=admin_headers)
|
|
assert r.status_code == 200
|
|
configs = r.json()
|
|
assert isinstance(configs, list)
|
|
assert any(c["id"] == _state["gid"] for c in configs)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_update_genai_config(client, admin_headers, oci_config_id):
|
|
"""PUT /api/genai/configs/{gid} — update temperature."""
|
|
gid = _state["gid"]
|
|
r = await client.put(
|
|
f"/api/genai/configs/{gid}",
|
|
json={
|
|
"name": "TestGenAI",
|
|
"oci_config_id": oci_config_id,
|
|
"model_id": "openai.gpt-4.1",
|
|
"compartment_id": "ocid1.compartment.oc1..test",
|
|
"genai_region": "us-ashburn-1",
|
|
"temperature": 0.5,
|
|
},
|
|
headers=admin_headers,
|
|
)
|
|
assert r.status_code == 200, r.text
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_delete_genai_config(client, admin_headers):
|
|
"""DELETE /api/genai/configs/{gid} — remove config."""
|
|
gid = _state["gid"]
|
|
r = await client.delete(f"/api/genai/configs/{gid}", headers=admin_headers)
|
|
assert r.status_code == 200, r.text
|