Spaces:
Sleeping
Sleeping
Add Gemini REST resilience tests
Browse files- tests/test_analyst.py +72 -41
tests/test_analyst.py
CHANGED
|
@@ -1,41 +1,72 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import pytest
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
assert profile["brief"].
|
| 23 |
-
assert profile["
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
assert
|
| 35 |
-
assert
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import pytest
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
from datapilot.analyst import (
|
| 6 |
+
ai_context,
|
| 7 |
+
build_data_dictionary,
|
| 8 |
+
gemini_dataset_summary,
|
| 9 |
+
inspect_dataset,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def test_immediate_profile_and_target_ranking():
|
| 14 |
+
frame = pd.DataFrame(
|
| 15 |
+
{
|
| 16 |
+
"customer_id": [1, 2, 3, 4],
|
| 17 |
+
"age": [24, None, 39, 41],
|
| 18 |
+
"churn": ["no", "no", "yes", "yes"],
|
| 19 |
+
}
|
| 20 |
+
)
|
| 21 |
+
profile = inspect_dataset(frame)
|
| 22 |
+
assert profile["brief"].rows == 4
|
| 23 |
+
assert profile["brief"].missing_cells == 1
|
| 24 |
+
assert profile["targets"][0]["column"] == "churn"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_dictionary_and_ai_context_redact_pii():
|
| 28 |
+
frame = pd.DataFrame(
|
| 29 |
+
{"email": ["a@x.com", "b@x.com"], "revenue": [99.0, 101.0], "secret": ["x", "y"]}
|
| 30 |
+
)
|
| 31 |
+
profile = inspect_dataset(frame)
|
| 32 |
+
dictionary = build_data_dictionary(frame).set_index("column")
|
| 33 |
+
context = ai_context(frame, profile, ["secret"])
|
| 34 |
+
assert "Potential PII" in dictionary.loc["email", "issues"]
|
| 35 |
+
assert context["sample"] == [{"revenue": 99.0}, {"revenue": 101.0}]
|
| 36 |
+
assert set(context["excluded_columns"]) == {"email", "secret"}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_gemini_requires_a_key():
|
| 40 |
+
frame = pd.DataFrame({"x": [1, 2], "target": [0, 1]})
|
| 41 |
+
with pytest.raises(ValueError, match="API key"):
|
| 42 |
+
gemini_dataset_summary(frame, inspect_dataset(frame), "", "gemini", [])
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_gemini_rest_success(monkeypatch):
|
| 46 |
+
frame = pd.DataFrame({"x": [1, 2], "target": [0, 1]})
|
| 47 |
+
|
| 48 |
+
class Response:
|
| 49 |
+
status_code = 200
|
| 50 |
+
ok = True
|
| 51 |
+
|
| 52 |
+
@staticmethod
|
| 53 |
+
def json():
|
| 54 |
+
return {"candidates": [{"content": {"parts": [{"text": "## Finding\nGrounded"}]}}]}
|
| 55 |
+
|
| 56 |
+
monkeypatch.setattr(requests, "post", lambda *args, **kwargs: Response())
|
| 57 |
+
result = gemini_dataset_summary(
|
| 58 |
+
frame, inspect_dataset(frame), "test-key", "gemini-2.5-flash", []
|
| 59 |
+
)
|
| 60 |
+
assert "Grounded" in result
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test_gemini_rest_quota_error(monkeypatch):
|
| 64 |
+
frame = pd.DataFrame({"x": [1, 2], "target": [0, 1]})
|
| 65 |
+
|
| 66 |
+
class Response:
|
| 67 |
+
status_code = 429
|
| 68 |
+
ok = False
|
| 69 |
+
|
| 70 |
+
monkeypatch.setattr(requests, "post", lambda *args, **kwargs: Response())
|
| 71 |
+
with pytest.raises(ValueError, match="quota"):
|
| 72 |
+
gemini_dataset_summary(frame, inspect_dataset(frame), "test-key", "gemini-2.5-flash", [])
|