-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_mixpeek_production.py
More file actions
333 lines (285 loc) · 12.1 KB
/
Copy pathtest_mixpeek_production.py
File metadata and controls
333 lines (285 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# End-to-end test against the production Mixpeek API.
#
# Exercises the full buyer-agent contextual enrichment flow:
# 1. Health check (public endpoint)
# 2. MCP tools discovery (public endpoint)
# 3. IAB taxonomy classification of real ad content
# 4. Brand-safety scoring of safe and sensitive content
# 5. Contextual inventory search via retriever pipeline
#
# Requires:
# MIXPEEK_API_KEY — a valid Mixpeek API key
# MIXPEEK_NAMESPACE — namespace with IAB data (default: golden_adtech_iab)
#
# Run:
# MIXPEEK_API_KEY=mxp_sk_... pytest tests/e2e/test_mixpeek_production.py -v
from __future__ import annotations
import os
import pytest
import pytest_asyncio
from ad_buyer.clients.mixpeek_client import MixpeekClient, MixpeekError
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
API_KEY = os.environ.get("MIXPEEK_API_KEY", "")
BASE_URL = os.environ.get("MIXPEEK_BASE_URL", "https://api.mixpeek.com")
NAMESPACE = os.environ.get("MIXPEEK_NAMESPACE", "golden_adtech_iab")
# Known retriever in golden_adtech_iab namespace
IAB_TEXT_RETRIEVER = os.environ.get("MIXPEEK_IAB_RETRIEVER_ID", "ret_f7fbefced358bd")
pytestmark = [
pytest.mark.e2e,
pytest.mark.skipif(not API_KEY, reason="MIXPEEK_API_KEY not set"),
]
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest_asyncio.fixture
async def client():
c = MixpeekClient(
api_key=API_KEY,
base_url=BASE_URL,
namespace=NAMESPACE,
)
yield c
await c.close()
# ---------------------------------------------------------------------------
# 1. Health & Discovery
# ---------------------------------------------------------------------------
class TestHealthAndDiscovery:
@pytest.mark.asyncio
async def test_mcp_health(self, client: MixpeekClient):
"""MCP server health check returns healthy status."""
result = await client.health()
assert result["status"] == "healthy"
assert result["tools_count"] >= 40 # currently 48
@pytest.mark.asyncio
async def test_mcp_tools_list(self, client: MixpeekClient):
"""Public tools endpoint returns the full tool catalog."""
tools = await client.get_tools()
assert len(tools) >= 40
names = {t["name"] for t in tools}
# Spot-check a few expected tools
assert "create_namespace" in names
assert "execute_retriever" in names
@pytest.mark.asyncio
async def test_list_retrievers(self, client: MixpeekClient):
"""Namespace has IAB retriever pipelines."""
retrievers = await client.list_retrievers()
assert len(retrievers) > 0
names = {r["retriever_name"] for r in retrievers}
assert any("iab" in n.lower() for n in names), f"No IAB retriever found. Available: {names}"
# ---------------------------------------------------------------------------
# 2. IAB Content Classification
# ---------------------------------------------------------------------------
class TestIABClassification:
@pytest.mark.asyncio
async def test_classify_sports_content(self, client: MixpeekClient):
"""NFL content classifies as Sports > American Football."""
result = await client.classify_content(
retriever_id=IAB_TEXT_RETRIEVER,
text=(
"Breaking: NFL playoff scores and highlights from Sunday "
"night football. Tom Brady analysis and Super Bowl predictions."
),
)
docs = result["documents"]
assert len(docs) > 0
top = docs[0]
assert top["score"] > 0.80
assert "Sports" in top["iab_path"]
# Top result should be American Football or Sports
assert top["iab_category_name"] in (
"American Football",
"Sports",
"College Football",
)
@pytest.mark.asyncio
async def test_classify_automotive_content(self, client: MixpeekClient):
"""Luxury car review classifies as Automotive > Luxury Cars."""
result = await client.classify_content(
retriever_id=IAB_TEXT_RETRIEVER,
text=(
"The 2026 Mercedes-Benz S-Class review: luxury sedan with "
"cutting-edge autonomous driving features, premium leather "
"interior, and a 496-horsepower twin-turbo V8 engine."
),
)
docs = result["documents"]
assert len(docs) > 0
top = docs[0]
assert top["score"] > 0.80
assert "Automotive" in top["iab_path"]
# Top result should be an Automotive subcategory
assert "Automotive" in top["iab_path"]
@pytest.mark.asyncio
async def test_classify_food_content(self, client: MixpeekClient):
"""Cooking recipe classifies as Food & Drink > Cooking."""
result = await client.classify_content(
retriever_id=IAB_TEXT_RETRIEVER,
text=(
"Easy homemade pasta recipe: combine fresh eggs, semolina "
"flour, and olive oil. Roll the dough thin, cut into "
"fettuccine, and cook in salted boiling water for 3 minutes."
),
)
docs = result["documents"]
assert len(docs) > 0
top = docs[0]
assert top["score"] > 0.80
# Should be in Food & Drink or Cooking
paths_flat = [cat for d in docs[:3] for cat in d.get("iab_path", [])]
assert "Food & Drink" in paths_flat or "Cooking" in paths_flat
@pytest.mark.asyncio
async def test_classify_technology_content(self, client: MixpeekClient):
"""AI/ML content classifies as Technology > Artificial Intelligence."""
result = await client.classify_content(
retriever_id=IAB_TEXT_RETRIEVER,
text=(
"Artificial intelligence and machine learning are "
"transforming enterprise software. New AI startups raised "
"$10B in Q1 2026 for large language model development."
),
)
docs = result["documents"]
assert len(docs) > 0
top = docs[0]
assert top["score"] > 0.85
assert top["iab_category_name"] == "Artificial Intelligence"
assert "Technology & Computing" in top["iab_path"]
@pytest.mark.asyncio
async def test_classify_returns_hierarchical_paths(self, client: MixpeekClient):
"""Results include full IAB hierarchy: tier, path, category name."""
result = await client.classify_content(
retriever_id=IAB_TEXT_RETRIEVER,
text="Professional basketball NBA playoffs Lakers vs Celtics",
)
doc = result["documents"][0]
assert "iab_category_name" in doc
assert "iab_path" in doc
assert "iab_tier" in doc
assert isinstance(doc["iab_path"], list)
assert doc["iab_tier"] in (1, 2, 3, 4)
# ---------------------------------------------------------------------------
# 3. Brand Safety
# ---------------------------------------------------------------------------
class TestBrandSafety:
@pytest.mark.asyncio
async def test_safe_content(self, client: MixpeekClient):
"""Benign sports content is flagged as safe."""
result = await client.check_brand_safety(
retriever_id=IAB_TEXT_RETRIEVER,
text=(
"Local high school basketball team wins state championship "
"in an exciting overtime game at the civic center."
),
)
assert result["safe"] is True
assert result["risk_level"] == "low"
assert len(result["flagged_categories"]) == 0
@pytest.mark.asyncio
async def test_gambling_content_flagged(self, client: MixpeekClient):
"""Gambling content is flagged as brand-unsafe."""
result = await client.check_brand_safety(
retriever_id=IAB_TEXT_RETRIEVER,
text=(
"Online poker tournament with $1M prize pool. Texas Hold'em "
"strategy guide for casino gambling. Bet on sports with our "
"new odds calculator."
),
)
assert result["safe"] is False
assert result["risk_level"] in ("medium", "high")
flagged_names = [c["category"] for c in result["flagged_categories"]]
assert any(
cat in flagged_names
for cat in (
"Poker and Professional Gambling",
"Casinos & Gambling",
"Casino Games",
)
), f"Expected gambling categories, got: {flagged_names}"
@pytest.mark.asyncio
async def test_brand_safety_threshold(self, client: MixpeekClient):
"""Higher threshold filters out lower-confidence matches."""
result_low = await client.check_brand_safety(
retriever_id=IAB_TEXT_RETRIEVER,
text="Online poker tournament guide",
threshold=0.70,
)
result_high = await client.check_brand_safety(
retriever_id=IAB_TEXT_RETRIEVER,
text="Online poker tournament guide",
threshold=0.95,
)
# More categories pass at lower threshold
assert len(result_low["categories"]) >= len(result_high["categories"])
# ---------------------------------------------------------------------------
# 4. Contextual Search
# ---------------------------------------------------------------------------
class TestContextualSearch:
@pytest.mark.asyncio
async def test_search_returns_results(self, client: MixpeekClient):
"""Contextual search returns ranked results with scores."""
result = await client.search_content(
retriever_id=IAB_TEXT_RETRIEVER,
query="sports and athletics",
limit=5,
)
assert "documents" in result
docs = result["documents"]
assert len(docs) > 0
# Each result has a score
for d in docs:
assert "score" in d
assert d["score"] > 0
@pytest.mark.asyncio
async def test_search_results_have_iab_metadata(self, client: MixpeekClient):
"""Search results include IAB category metadata."""
result = await client.search_content(
retriever_id=IAB_TEXT_RETRIEVER,
query="technology computing artificial intelligence",
limit=3,
)
doc = result["documents"][0]
assert "iab_category_name" in doc
assert "iab_path" in doc
assert "score" in doc
@pytest.mark.asyncio
async def test_search_returns_iab_enriched_results(self, client: MixpeekClient):
"""Search results contain IAB enrichment from the retriever."""
result = await client.search_content(
retriever_id=IAB_TEXT_RETRIEVER,
query="food cooking recipes",
)
docs = result["documents"]
assert len(docs) > 0
# Results from IAB retriever have category metadata
for d in docs[:3]:
assert "iab_category_name" in d
assert "iab_path" in d
# ---------------------------------------------------------------------------
# 5. Error Handling
# ---------------------------------------------------------------------------
class TestErrorHandling:
@pytest.mark.asyncio
async def test_invalid_retriever_id(self, client: MixpeekClient):
"""Invalid retriever ID raises MixpeekError."""
with pytest.raises(MixpeekError) as exc_info:
await client.classify_content(
retriever_id="ret_nonexistent",
text="test content",
)
assert exc_info.value.status_code in (404, 400, 422)
@pytest.mark.asyncio
async def test_invalid_api_key(self):
"""Invalid API key raises MixpeekError."""
bad_client = MixpeekClient(
api_key="invalid_key",
namespace=NAMESPACE,
)
try:
with pytest.raises(MixpeekError) as exc_info:
await bad_client.list_retrievers()
assert exc_info.value.status_code in (401, 403, 404)
finally:
await bad_client.close()