Spaces:
Sleeping
Sleeping
Download test_numeric_http_contract.py from shazhass/phytoevidence-api: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/spaces/shazhass/phytoevidence-api/resolve/main/test_numeric_http_contract.py
- Command line
-
hf download hf://spaces/shazhass/phytoevidence-api/test_numeric_http_contract.py
-
curl -L -o test_numeric_http_contract.py https://huggingface.co/spaces/shazhass/phytoevidence-api/resolve/main/test_numeric_http_contract.py
2.47 kB
| """Exercise the real numeric route and auth without loading ML models.""" | |
| import ast | |
| import asyncio | |
| from pathlib import Path | |
| from unittest.mock import patch | |
| import httpx | |
| from fastapi import FastAPI, Header, Body | |
| from endpoints import validate_numeric | |
| from security.auth import verify_bearer_token | |
| def numeric_app(): | |
| source = ast.parse(Path(__file__).with_name('app.py').read_text()) | |
| handler = next(node for node in source.body | |
| if isinstance(node, ast.AsyncFunctionDef) | |
| and node.name == 'post_validate_numeric') | |
| handler.decorator_list = [] | |
| module = ast.Module(body=[handler], type_ignores=[]) | |
| namespace = {'Header': Header, 'Body': Body, | |
| 'verify_bearer_token': verify_bearer_token, | |
| 'validate_numeric': validate_numeric} | |
| exec(compile(ast.fix_missing_locations(module), 'app.py', 'exec'), namespace) | |
| app = FastAPI() | |
| app.post('/validate-numeric')(namespace['post_validate_numeric']) | |
| return app | |
| def request(body, token=None): | |
| async def run(): | |
| transport = httpx.ASGITransport(app=numeric_app()) | |
| async with httpx.AsyncClient(transport=transport, base_url='http://test') as client: | |
| headers = {'Authorization': 'Bearer ' + token} if token else {} | |
| return await client.post('/validate-numeric', json=body, headers=headers) | |
| with patch('security.auth.API_KEY', 'test-only-key'): | |
| return asyncio.run(run()) | |
| def test_http_requires_authentication(): | |
| body = {'items': [{'id': 1, 'claim': '22-26 weeks', 'evidence': '22-26 weeks'}]} | |
| assert request(body).status_code == 401 | |
| assert request(body, 'incorrect').status_code == 403 | |
| def test_http_range_matching_and_negative_effects(): | |
| response = request({'relative_tolerance': 0, 'items': [ | |
| {'id': 1, 'claim': '22-26 weeks', 'evidence': '154–182 days'}, | |
| {'id': 2, 'claim': '22-26 weeks', 'evidence': '22-27 weeks'}, | |
| {'id': 3, 'claim': 'MD -2.86; 95% CI -3.18 to -2.54', | |
| 'evidence': 'MD -2.86; 95% CI -3.18 to -2.54'}, | |
| ]}, 'test-only-key') | |
| assert response.status_code == 200 | |
| results = response.json()['results'] | |
| assert [r['numeric_ok'] for r in results] == [True, False, True] | |
| assert results[0]['detail'][0] == { | |
| 'kind': 'duration', 'value': 154, 'upper_value': 182, 'verdict': 'MATCH'} | |
| def test_http_empty_items_rejected(): | |
| assert request({'items': []}, 'test-only-key').status_code == 400 | |