Spaces:
Sleeping
Sleeping
File size: 2,474 Bytes
60bae3f | 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 | """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
|