File size: 11,855 Bytes
824bf31
 
 
 
 
 
 
 
9730fbc
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9730fbc
824bf31
 
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
Module: tools.api_test
Description: Testing utilities for government transparency APIs
Author: Anderson H. Silva
Date: 2025-01-15
"""

import asyncio
from src.core import json_utils
from datetime import datetime, timedelta
from typing import Dict, Any, Optional
import logging

from .transparency_api import TransparencyAPIClient, TransparencyAPIFilter
from ..core.config import settings
from ..core.exceptions import TransparencyAPIError, DataNotFoundError

logger = logging.getLogger(__name__)


class APITester:
    """Test suite for government transparency APIs."""
    
    def __init__(self):
        self.client = TransparencyAPIClient()
        self.test_results = []
    
    async def __aenter__(self):
        return self
    
    async def __aexit__(self, exc_type, exc_val, exc_tb):
        await self.client.close()
    
    def _log_test_result(self, test_name: str, success: bool, details: Dict[str, Any]):
        """Log test result."""
        result = {
            "test_name": test_name,
            "success": success,
            "timestamp": datetime.now().isoformat(),
            "details": details
        }
        self.test_results.append(result)
        
        if success:
            logger.info(f"✅ {test_name}: PASSED", extra=details)
        else:
            logger.error(f"❌ {test_name}: FAILED", extra=details)
    
    async def test_api_connection(self) -> bool:
        """Test basic API connectivity."""
        try:
            # Test with minimal filters
            filters = TransparencyAPIFilter(
                ano=2024,
                tamanho_pagina=1
            )
            
            response = await self.client.get_expenses(filters)
            
            success = len(response.data) > 0
            self._log_test_result(
                "API Connection",
                success,
                {
                    "total_records": response.total_records,
                    "response_size": len(response.data)
                }
            )
            return success
            
        except Exception as e:
            self._log_test_result(
                "API Connection",
                False,
                {"error": str(e)}
            )
            return False
    
    async def test_contracts_endpoint(self) -> bool:
        """Test contracts endpoint."""
        try:
            # Test recent contracts
            filters = TransparencyAPIFilter(
                ano=2024,
                tamanho_pagina=5
            )
            
            response = await self.client.get_contracts(filters)
            
            success = isinstance(response.data, list)
            self._log_test_result(
                "Contracts Endpoint",
                success,
                {
                    "total_records": response.total_records,
                    "data_count": len(response.data),
                    "sample_fields": list(response.data[0].keys()) if response.data else []
                }
            )
            return success
            
        except Exception as e:
            self._log_test_result(
                "Contracts Endpoint",
                False,
                {"error": str(e)}
            )
            return False
    
    async def test_expenses_endpoint(self) -> bool:
        """Test expenses endpoint."""
        try:
            # Test recent expenses
            filters = TransparencyAPIFilter(
                ano=2024,
                mes=1,
                tamanho_pagina=5
            )
            
            response = await self.client.get_expenses(filters)
            
            success = isinstance(response.data, list)
            self._log_test_result(
                "Expenses Endpoint",
                success,
                {
                    "total_records": response.total_records,
                    "data_count": len(response.data),
                    "sample_fields": list(response.data[0].keys()) if response.data else []
                }
            )
            return success
            
        except Exception as e:
            self._log_test_result(
                "Expenses Endpoint",
                False,
                {"error": str(e)}
            )
            return False
    
    async def test_biddings_endpoint(self) -> bool:
        """Test biddings endpoint."""
        try:
            # Test recent biddings
            filters = TransparencyAPIFilter(
                ano=2024,
                tamanho_pagina=3
            )
            
            response = await self.client.get_biddings(filters)
            
            success = isinstance(response.data, list)
            self._log_test_result(
                "Biddings Endpoint",
                success,
                {
                    "total_records": response.total_records,
                    "data_count": len(response.data),
                    "sample_fields": list(response.data[0].keys()) if response.data else []
                }
            )
            return success
            
        except Exception as e:
            self._log_test_result(
                "Biddings Endpoint",
                False,
                {"error": str(e)}
            )
            return False
    
    async def test_rate_limiting(self) -> bool:
        """Test rate limiting functionality."""
        try:
            # Make multiple rapid requests
            filters = TransparencyAPIFilter(
                ano=2024,
                tamanho_pagina=1
            )
            
            start_time = datetime.now()
            
            # Make 5 requests rapidly
            for i in range(5):
                await self.client.get_expenses(filters)
            
            end_time = datetime.now()
            duration = (end_time - start_time).total_seconds()
            
            # Should take some time due to rate limiting
            success = duration > 2  # At least 2 seconds for 5 requests
            
            self._log_test_result(
                "Rate Limiting",
                success,
                {
                    "requests_made": 5,
                    "duration_seconds": duration,
                    "avg_per_request": duration / 5
                }
            )
            return success
            
        except Exception as e:
            self._log_test_result(
                "Rate Limiting",
                False,
                {"error": str(e)}
            )
            return False
    
    async def test_data_quality(self) -> bool:
        """Test data quality and structure."""
        try:
            filters = TransparencyAPIFilter(
                ano=2024,
                tamanho_pagina=10
            )
            
            response = await self.client.get_contracts(filters)
            
            if not response.data:
                self._log_test_result(
                    "Data Quality",
                    False,
                    {"error": "No data returned"}
                )
                return False
            
            # Check data structure
            sample = response.data[0]
            required_fields = ['id', 'numero', 'objeto']  # Common contract fields
            
            has_required_fields = any(field in sample for field in required_fields)
            has_numeric_values = any(isinstance(v, (int, float)) for v in sample.values())
            has_text_values = any(isinstance(v, str) for v in sample.values())
            
            success = has_required_fields and has_numeric_values and has_text_values
            
            self._log_test_result(
                "Data Quality",
                success,
                {
                    "sample_fields": list(sample.keys()),
                    "has_required_fields": has_required_fields,
                    "has_numeric_values": has_numeric_values,
                    "has_text_values": has_text_values
                }
            )
            return success
            
        except Exception as e:
            self._log_test_result(
                "Data Quality",
                False,
                {"error": str(e)}
            )
            return False
    
    async def test_error_handling(self) -> bool:
        """Test error handling with invalid requests."""
        try:
            # Test with invalid filters
            filters = TransparencyAPIFilter(
                ano=1900,  # Invalid year
                tamanho_pagina=1
            )
            
            try:
                await self.client.get_contracts(filters)
                # If no error, test fails
                success = False
                error_msg = "Expected error but got success"
            except (TransparencyAPIError, DataNotFoundError) as e:
                # Expected error
                success = True
                error_msg = str(e)
            except Exception as e:
                # Unexpected error
                success = False
                error_msg = f"Unexpected error: {str(e)}"
            
            self._log_test_result(
                "Error Handling",
                success,
                {"error_message": error_msg}
            )
            return success
            
        except Exception as e:
            self._log_test_result(
                "Error Handling",
                False,
                {"error": str(e)}
            )
            return False
    
    async def run_all_tests(self) -> Dict[str, Any]:
        """Run all tests and return comprehensive results."""
        logger.info("🚀 Starting API test suite...")
        
        # List of all test methods
        tests = [
            self.test_api_connection,
            self.test_contracts_endpoint,
            self.test_expenses_endpoint,
            self.test_biddings_endpoint,
            self.test_rate_limiting,
            self.test_data_quality,
            self.test_error_handling
        ]
        
        # Run all tests
        results = {}
        passed = 0
        total = len(tests)
        
        for test in tests:
            test_name = test.__name__.replace('test_', '').replace('_', ' ').title()
            try:
                success = await test()
                results[test_name] = success
                if success:
                    passed += 1
            except Exception as e:
                logger.error(f"Test {test_name} crashed: {str(e)}")
                results[test_name] = False
        
        # Summary
        summary = {
            "total_tests": total,
            "passed": passed,
            "failed": total - passed,
            "success_rate": (passed / total) * 100,
            "results": results,
            "detailed_results": self.test_results,
            "timestamp": datetime.now().isoformat()
        }
        
        logger.info(f"📊 Test suite completed: {passed}/{total} tests passed ({summary['success_rate']:.1f}%)")
        
        return summary


async def run_api_tests() -> Dict[str, Any]:
    """
    Convenience function to run all API tests.
    
    Returns:
        Test results summary
    """
    async with APITester() as tester:
        return await tester.run_all_tests()


async def quick_api_test() -> bool:
    """
    Quick API connectivity test.
    
    Returns:
        True if API is working, False otherwise
    """
    try:
        async with APITester() as tester:
            return await tester.test_api_connection()
    except Exception as e:
        logger.error(f"Quick API test failed: {str(e)}")
        return False


if __name__ == "__main__":
    # Run tests when executed directly
    async def main():
        results = await run_api_tests()
        print(json_utils.dumps(results, indent=2))
    
    asyncio.run(main())