Spaces:
Sleeping
Sleeping
Create tests/test_adapter_contract.py
Browse files
tests/test_adapter_contract.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import inspect
|
| 2 |
+
import asyncio
|
| 3 |
+
import pytest
|
| 4 |
+
from typing import Dict, List
|
| 5 |
+
|
| 6 |
+
from ingest.generic_public_foia import GenericFOIAAdapter
|
| 7 |
+
from ingest.registry import get_all_adapters
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.mark.asyncio
|
| 11 |
+
async def test_adapters_implement_search():
|
| 12 |
+
"""
|
| 13 |
+
Every adapter must implement an async search() method.
|
| 14 |
+
"""
|
| 15 |
+
adapters = get_all_adapters()
|
| 16 |
+
|
| 17 |
+
assert adapters, "No adapters registered"
|
| 18 |
+
|
| 19 |
+
for name, adapter in adapters.items():
|
| 20 |
+
assert isinstance(adapter, GenericFOIAAdapter), f"{name} not subclassing GenericFOIAAdapter"
|
| 21 |
+
|
| 22 |
+
method = adapter.search
|
| 23 |
+
assert callable(method), f"{name}.search is not callable"
|
| 24 |
+
assert inspect.iscoroutinefunction(method), f"{name}.search must be async"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@pytest.mark.asyncio
|
| 28 |
+
async def test_search_returns_list_of_dicts():
|
| 29 |
+
"""
|
| 30 |
+
search() must always return List[Dict], even on failure.
|
| 31 |
+
"""
|
| 32 |
+
adapters = get_all_adapters()
|
| 33 |
+
|
| 34 |
+
for name, adapter in adapters.items():
|
| 35 |
+
try:
|
| 36 |
+
results = await adapter.search("test")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
pytest.fail(f"{name}.search() raised exception: {e}")
|
| 39 |
+
|
| 40 |
+
assert isinstance(results, list), f"{name}.search did not return a list"
|
| 41 |
+
|
| 42 |
+
for r in results:
|
| 43 |
+
assert isinstance(r, dict), f"{name} returned non-dict result"
|