anderson-ufrj
commited on
Commit
·
5a2433b
1
Parent(s):
3b7b010
test(integration): add transparency API real integration test
Browse files
tests/integration/test_transparency_api_real.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Test script to validate real API integration
|
| 4 |
+
Run this to test if the Portal da Transparência integration is working
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import asyncio
|
| 8 |
+
import os
|
| 9 |
+
import sys
|
| 10 |
+
import json
|
| 11 |
+
|
| 12 |
+
# Add src to path
|
| 13 |
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
|
| 14 |
+
|
| 15 |
+
async def test_real_api_integration():
|
| 16 |
+
"""Test the real API integration"""
|
| 17 |
+
print("🧪 Testing Real API Integration")
|
| 18 |
+
print("=" * 50)
|
| 19 |
+
|
| 20 |
+
# Test 1: Check API key availability
|
| 21 |
+
api_key = os.getenv("TRANSPARENCY_API_KEY")
|
| 22 |
+
print(f"1. API Key Status: {'✅ Available' if api_key else '❌ Not found'}")
|
| 23 |
+
|
| 24 |
+
if not api_key:
|
| 25 |
+
print(" ⚠️ Set TRANSPARENCY_API_KEY environment variable to test real API")
|
| 26 |
+
print(" 🔄 Will use fallback mock data\n")
|
| 27 |
+
else:
|
| 28 |
+
print(f" 🔑 API Key: {api_key[:20]}...{api_key[-10:]}\n")
|
| 29 |
+
|
| 30 |
+
# Test 2: Import and test API client
|
| 31 |
+
try:
|
| 32 |
+
from tools.transparency_api import TransparencyAPIClient, TransparencyAPIFilter
|
| 33 |
+
print("2. API Client Import: ✅ Success")
|
| 34 |
+
|
| 35 |
+
if api_key:
|
| 36 |
+
# Test real API call
|
| 37 |
+
try:
|
| 38 |
+
async with TransparencyAPIClient(api_key=api_key) as client:
|
| 39 |
+
filters = TransparencyAPIFilter(
|
| 40 |
+
codigo_orgao="26000", # Health Ministry
|
| 41 |
+
ano=2024,
|
| 42 |
+
tamanho_pagina=5
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
response = await client.get_contracts(filters)
|
| 46 |
+
print(f"3. Real API Test: ✅ Success - {len(response.data)} contracts fetched")
|
| 47 |
+
|
| 48 |
+
if response.data:
|
| 49 |
+
sample = response.data[0]
|
| 50 |
+
print(f" 📄 Sample contract: {sample.get('objeto', 'N/A')[:60]}...")
|
| 51 |
+
print(f" 💰 Value: R$ {sample.get('valorInicial', 0):,.2f}")
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"3. Real API Test: ❌ Failed - {str(e)}")
|
| 55 |
+
else:
|
| 56 |
+
print("3. Real API Test: ⏭️ Skipped (no API key)")
|
| 57 |
+
|
| 58 |
+
except ImportError as e:
|
| 59 |
+
print(f"2. API Client Import: ❌ Failed - {str(e)}")
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
# Test 3: Test embedded Zumbi agent
|
| 63 |
+
try:
|
| 64 |
+
# Import the ZumbiAgent from app.py
|
| 65 |
+
import importlib.util
|
| 66 |
+
spec = importlib.util.spec_from_file_location("app", "app.py")
|
| 67 |
+
app_module = importlib.util.module_from_spec(spec)
|
| 68 |
+
spec.loader.exec_module(app_module)
|
| 69 |
+
|
| 70 |
+
zumbi = app_module.ZumbiAgent()
|
| 71 |
+
print("4. Zumbi Agent: ✅ Initialized")
|
| 72 |
+
|
| 73 |
+
# Test investigation
|
| 74 |
+
request = app_module.InvestigationRequest(
|
| 75 |
+
query="Analisar contratos de informática com valores suspeitos",
|
| 76 |
+
data_source="contracts",
|
| 77 |
+
max_results=50
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
result = await zumbi.investigate(request)
|
| 81 |
+
print(f"5. Investigation Test: ✅ Success")
|
| 82 |
+
print(f" 🔍 Status: {result.status}")
|
| 83 |
+
print(f" 📊 Anomalies found: {result.anomalies_found}")
|
| 84 |
+
print(f" ⏱️ Processing time: {result.processing_time_ms}ms")
|
| 85 |
+
print(f" 🎯 Confidence: {result.confidence_score:.2f}")
|
| 86 |
+
|
| 87 |
+
if result.results:
|
| 88 |
+
print("\n📋 Sample anomalies:")
|
| 89 |
+
for i, anomaly in enumerate(result.results[:3], 1):
|
| 90 |
+
print(f" {i}. {anomaly.get('description', 'N/A')[:50]}...")
|
| 91 |
+
print(f" 💰 Value: R$ {anomaly.get('value', 0):,.2f}")
|
| 92 |
+
print(f" 🚨 Risk: {anomaly.get('risk_level', 'unknown')}")
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"4. Zumbi Agent Test: ❌ Failed - {str(e)}")
|
| 96 |
+
return
|
| 97 |
+
|
| 98 |
+
print("\n" + "=" * 50)
|
| 99 |
+
print("🎉 Integration test completed!")
|
| 100 |
+
|
| 101 |
+
if api_key and result.status == "completed":
|
| 102 |
+
print("✅ System is using REAL Portal da Transparência data")
|
| 103 |
+
elif result.status == "completed_fallback":
|
| 104 |
+
print("🔄 System is using fallback demo data (API key needed for real data)")
|
| 105 |
+
else:
|
| 106 |
+
print("❌ System encountered errors")
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
asyncio.run(test_real_api_integration())
|