|
| 1 | +from types import SimpleNamespace |
| 2 | + |
| 3 | +from langfuse.api.commons.errors.not_found_error import NotFoundError |
| 4 | +from tests.support.api_wrapper import LangfuseAPI as SupportLangfuseAPI |
| 5 | +from tests.support.utils import get_api |
| 6 | + |
| 7 | + |
| 8 | +def test_get_api_retries_not_found(monkeypatch): |
| 9 | + monkeypatch.setattr("tests.support.retry.sleep", lambda _: None) |
| 10 | + |
| 11 | + attempts = {"count": 0} |
| 12 | + |
| 13 | + class FakeTraceService: |
| 14 | + def get(self, trace_id): |
| 15 | + attempts["count"] += 1 |
| 16 | + |
| 17 | + if attempts["count"] < 3: |
| 18 | + raise NotFoundError( |
| 19 | + body={ |
| 20 | + "error": "LangfuseNotFoundError", |
| 21 | + "message": f"Trace {trace_id} not found within authorized project", |
| 22 | + } |
| 23 | + ) |
| 24 | + |
| 25 | + return {"id": trace_id} |
| 26 | + |
| 27 | + class FakeClient: |
| 28 | + trace = FakeTraceService() |
| 29 | + |
| 30 | + monkeypatch.setattr("tests.support.utils.LangfuseAPI", lambda **_: FakeClient()) |
| 31 | + |
| 32 | + trace = get_api().trace.get("trace-123") |
| 33 | + |
| 34 | + assert trace == {"id": "trace-123"} |
| 35 | + assert attempts["count"] == 3 |
| 36 | + |
| 37 | + |
| 38 | +def test_get_api_retries_filtered_lists(monkeypatch): |
| 39 | + monkeypatch.setattr("tests.support.retry.sleep", lambda _: None) |
| 40 | + |
| 41 | + attempts = {"count": 0} |
| 42 | + |
| 43 | + class FakeTraceService: |
| 44 | + def list(self, **kwargs): |
| 45 | + attempts["count"] += 1 |
| 46 | + |
| 47 | + if attempts["count"] < 3: |
| 48 | + return SimpleNamespace(data=[]) |
| 49 | + |
| 50 | + return SimpleNamespace(data=[kwargs["name"]]) |
| 51 | + |
| 52 | + class FakeClient: |
| 53 | + trace = FakeTraceService() |
| 54 | + |
| 55 | + monkeypatch.setattr("tests.support.utils.LangfuseAPI", lambda **_: FakeClient()) |
| 56 | + |
| 57 | + response = get_api().trace.list(name="ready-trace") |
| 58 | + |
| 59 | + assert response.data == ["ready-trace"] |
| 60 | + assert attempts["count"] == 3 |
| 61 | + |
| 62 | + |
| 63 | +def test_get_api_retry_can_be_disabled(monkeypatch): |
| 64 | + attempts = {"count": 0} |
| 65 | + |
| 66 | + class FakeTraceService: |
| 67 | + def list(self, **kwargs): |
| 68 | + attempts["count"] += 1 |
| 69 | + return SimpleNamespace(data=[]) |
| 70 | + |
| 71 | + class FakeClient: |
| 72 | + trace = FakeTraceService() |
| 73 | + |
| 74 | + monkeypatch.setattr("tests.support.utils.LangfuseAPI", lambda **_: FakeClient()) |
| 75 | + |
| 76 | + response = get_api(retry=False).trace.list(name="missing-trace") |
| 77 | + |
| 78 | + assert response.data == [] |
| 79 | + assert attempts["count"] == 1 |
| 80 | + |
| 81 | + |
| 82 | +def test_raw_api_wrapper_retries_not_found_payload(monkeypatch): |
| 83 | + monkeypatch.setattr("tests.support.retry.sleep", lambda _: None) |
| 84 | + |
| 85 | + attempts = {"count": 0} |
| 86 | + |
| 87 | + class FakeResponse: |
| 88 | + def __init__(self, status_code, payload): |
| 89 | + self.status_code = status_code |
| 90 | + self._payload = payload |
| 91 | + self.headers = {} |
| 92 | + |
| 93 | + def json(self): |
| 94 | + return self._payload |
| 95 | + |
| 96 | + def fake_get(*args, **kwargs): |
| 97 | + attempts["count"] += 1 |
| 98 | + |
| 99 | + if attempts["count"] < 3: |
| 100 | + return FakeResponse( |
| 101 | + 404, |
| 102 | + { |
| 103 | + "error": "LangfuseNotFoundError", |
| 104 | + "message": "Trace trace-123 not found within authorized project", |
| 105 | + }, |
| 106 | + ) |
| 107 | + |
| 108 | + return FakeResponse(200, {"id": "trace-123", "observations": []}) |
| 109 | + |
| 110 | + monkeypatch.setattr("tests.support.api_wrapper.httpx.get", fake_get) |
| 111 | + |
| 112 | + api = SupportLangfuseAPI(username="user", password="pass", base_url="http://test") |
| 113 | + trace = api.get_trace("trace-123") |
| 114 | + |
| 115 | + assert trace["id"] == "trace-123" |
| 116 | + assert attempts["count"] == 3 |
0 commit comments