Download python_lora_dataset_builder/tests/test_nim_reviewer.py from api-relay/sadASDasd: direct link, hf CLI and curl.
- Browser
- Download file 3.73 kB
-
https://huggingface.co/datasets/api-relay/sadASDasd/resolve/main/python_lora_dataset_builder/tests/test_nim_reviewer.py
- Command line
-
hf download hf://datasets/api-relay/sadASDasd/python_lora_dataset_builder/tests/test_nim_reviewer.py
-
curl -L -o test_nim_reviewer.py https://huggingface.co/datasets/api-relay/sadASDasd/resolve/main/python_lora_dataset_builder/tests/test_nim_reviewer.py
3.73 kB
| import json | |
| import httpx | |
| import pytest | |
| from pipeline.nim_reviewer import NIMReviewer | |
| def _reviewer(monkeypatch, retries=3): | |
| monkeypatch.setenv("NIM_MODEL", "test-model") | |
| monkeypatch.setenv("NIM_API_KEY", "test-key") | |
| return NIMReviewer({"functions": "..."}, retries=retries, base_delay=0.001) | |
| class _FakeResponse: | |
| def __init__(self, status_code, json_body=None, text=""): | |
| self.status_code = status_code | |
| self._json_body = json_body or {} | |
| self.text = text | |
| self.reason_phrase = "ERR" | |
| self.request = httpx.Request("POST", "https://example.test/chat/completions") | |
| def json(self): | |
| return self._json_body | |
| def raise_for_status(self): | |
| if self.status_code >= 400: | |
| raise httpx.HTTPStatusError("err", request=self.request, response=self) | |
| def _good_body(): | |
| content = { | |
| "valid": True, | |
| "scores": {"correctness": 90, "instruction_alignment": 90, "quality": 90, | |
| "training_value": 90, "scope_fit": 90}, | |
| "overall_score": 90, "dominant_topic": "functions", "secondary_topics": [], | |
| "summary": "ok", "issues": [], | |
| } | |
| return {"choices": [{"message": {"content": json.dumps(content)}}]} | |
| def test_401_fails_immediately_without_retry(monkeypatch): | |
| reviewer = _reviewer(monkeypatch, retries=3) | |
| calls = {"n": 0} | |
| def fake_post(self, url, headers=None, json=None): | |
| calls["n"] += 1 | |
| return _FakeResponse(401, text="bad api key") | |
| monkeypatch.setattr(httpx.Client, "post", fake_post) | |
| with pytest.raises(RuntimeError, match="nim_request_failed_non_retryable"): | |
| reviewer.review("do x", "", "print(1)") | |
| assert calls["n"] == 1 # no retries burned on a non-retryable 401 | |
| def test_400_fails_immediately_without_retry(monkeypatch): | |
| reviewer = _reviewer(monkeypatch, retries=3) | |
| calls = {"n": 0} | |
| def fake_post(self, url, headers=None, json=None): | |
| calls["n"] += 1 | |
| return _FakeResponse(400, text="bad request") | |
| monkeypatch.setattr(httpx.Client, "post", fake_post) | |
| with pytest.raises(RuntimeError, match="nim_request_failed_non_retryable"): | |
| reviewer.review("do x", "", "print(1)") | |
| assert calls["n"] == 1 | |
| def test_429_is_retried_then_succeeds(monkeypatch): | |
| reviewer = _reviewer(monkeypatch, retries=3) | |
| calls = {"n": 0} | |
| def fake_post(self, url, headers=None, json=None): | |
| calls["n"] += 1 | |
| if calls["n"] < 3: | |
| return _FakeResponse(429, text="rate limited") | |
| return _FakeResponse(200, json_body=_good_body()) | |
| monkeypatch.setattr(httpx.Client, "post", fake_post) | |
| result, raw, attempts = reviewer.review("do x", "", "print(1)") | |
| assert calls["n"] == 3 | |
| assert attempts == 3 | |
| assert result["overall_score"] == 90 | |
| def test_500_retries_and_eventually_exhausts(monkeypatch): | |
| reviewer = _reviewer(monkeypatch, retries=2) | |
| calls = {"n": 0} | |
| def fake_post(self, url, headers=None, json=None): | |
| calls["n"] += 1 | |
| return _FakeResponse(503, text="down") | |
| monkeypatch.setattr(httpx.Client, "post", fake_post) | |
| with pytest.raises(RuntimeError, match="nim_retry_exhausted"): | |
| reviewer.review("do x", "", "print(1)") | |
| assert calls["n"] == 2 # retried up to the configured attempt count, then gave up | |
| def test_200_succeeds_on_first_try(monkeypatch): | |
| reviewer = _reviewer(monkeypatch, retries=3) | |
| def fake_post(self, url, headers=None, json=None): | |
| return _FakeResponse(200, json_body=_good_body()) | |
| monkeypatch.setattr(httpx.Client, "post", fake_post) | |
| result, raw, attempts = reviewer.review("do x", "", "print(1)") | |
| assert attempts == 1 | |
| assert result["dominant_topic"] == "functions" | |