dineshb commited on
Commit
431a8be
·
verified ·
1 Parent(s): 96497a7

Add Gemini REST resilience tests

Browse files
Files changed (1) hide show
  1. 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
- from datapilot.analyst import (
5
- ai_context,
6
- build_data_dictionary,
7
- gemini_dataset_summary,
8
- inspect_dataset,
9
- )
10
-
11
-
12
- def test_immediate_profile_and_target_ranking():
13
- frame = pd.DataFrame(
14
- {
15
- "customer_id": [1, 2, 3, 4],
16
- "age": [24, None, 39, 41],
17
- "churn": ["no", "no", "yes", "yes"],
18
- }
19
- )
20
- profile = inspect_dataset(frame)
21
- assert profile["brief"].rows == 4
22
- assert profile["brief"].missing_cells == 1
23
- assert profile["targets"][0]["column"] == "churn"
24
-
25
-
26
- def test_dictionary_and_ai_context_redact_pii():
27
- frame = pd.DataFrame(
28
- {"email": ["a@x.com", "b@x.com"], "revenue": [99.0, 101.0], "secret": ["x", "y"]}
29
- )
30
- profile = inspect_dataset(frame)
31
- dictionary = build_data_dictionary(frame).set_index("column")
32
- context = ai_context(frame, profile, ["secret"])
33
- assert "Potential PII" in dictionary.loc["email", "issues"]
34
- assert context["sample"] == [{"revenue": 99.0}, {"revenue": 101.0}]
35
- assert set(context["excluded_columns"]) == {"email", "secret"}
36
-
37
-
38
- def test_gemini_requires_a_key():
39
- frame = pd.DataFrame({"x": [1, 2], "target": [0, 1]})
40
- with pytest.raises(ValueError, match="API key"):
41
- gemini_dataset_summary(frame, inspect_dataset(frame), "", "gemini", [])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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", [])