test: cover ai platform demo workflow
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from openai import APITimeoutError
|
||||
import httpx
|
||||
import pytest
|
||||
from openai import APITimeoutError, AuthenticationError, InternalServerError, RateLimitError
|
||||
|
||||
from apps.modelhub.crypto import encrypt_secret
|
||||
from apps.modelhub.models import ModelDeployment, ModelInvocation, ModelProvider
|
||||
@@ -57,3 +59,43 @@ def test_non_transient_error_fails_without_exposing_raw_error(db, monkeypatch):
|
||||
assert invocation.status == "failed"
|
||||
assert invocation.error_code == "unexpected_error"
|
||||
assert "secret provider detail" not in invocation.error_message
|
||||
|
||||
|
||||
def provider_error(error_type, status_code):
|
||||
request = httpx.Request("POST", "https://models.example.com/v1/chat/completions")
|
||||
response = httpx.Response(status_code, request=request)
|
||||
return error_type("provider raw error", response=response, body={})
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "expected_code", "expected_calls"),
|
||||
[
|
||||
(provider_error(AuthenticationError, 401), "authentication_failed", 1),
|
||||
(provider_error(RateLimitError, 429), "rate_limited", 2),
|
||||
(provider_error(InternalServerError, 500), "provider_unavailable", 2),
|
||||
],
|
||||
)
|
||||
def test_provider_error_classification_and_retry(
|
||||
db,
|
||||
monkeypatch,
|
||||
error,
|
||||
expected_code,
|
||||
expected_calls,
|
||||
):
|
||||
invocation = create_invocation()
|
||||
calls = []
|
||||
|
||||
def request(*args, **kwargs):
|
||||
calls.append(1)
|
||||
raise error
|
||||
|
||||
monkeypatch.setattr("apps.modelhub.services._request_completion", request)
|
||||
monkeypatch.setattr("apps.modelhub.services.sleep", lambda *_: None)
|
||||
|
||||
execute_invocation(invocation, invocation.input_snapshot["messages"])
|
||||
|
||||
invocation.refresh_from_db()
|
||||
assert invocation.status == "failed"
|
||||
assert invocation.error_code == expected_code
|
||||
assert len(calls) == expected_calls
|
||||
assert "provider raw error" not in invocation.error_message
|
||||
|
||||
Reference in New Issue
Block a user