niqun123123 commited on
Commit
b4d3599
·
verified ·
1 Parent(s): ed84b45

Update code/smoke_test.py

Browse files
Files changed (1) hide show
  1. smoke_test.py +24 -0
smoke_test.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import argparse,json,urllib.request,urllib.error,math
3
+ from pathlib import Path
4
+ p=argparse.ArgumentParser();p.add_argument('--url',default='http://127.0.0.1:8000');a=p.parse_args()
5
+ body=json.loads((Path(__file__).resolve().parents[1]/'examples/request.json').read_text())
6
+ with urllib.request.urlopen(a.url+'/health',timeout=30) as r:assert json.load(r)['status']=='ready'
7
+ def post(b):
8
+ q=urllib.request.Request(a.url+'/v1/systemone',data=json.dumps(b).encode(),headers={'Content-Type':'application/json'})
9
+ with urllib.request.urlopen(q,timeout=180) as r:return json.load(r)
10
+ for b in [dict(body,questions={'intent':body['questions']['intent']}),body]:
11
+ result=post(b);assert set(result['answers'])==set(b['questions'])
12
+ for k,q in b['questions'].items():
13
+ ans=result['answers'][k];assert ans['type']==q['type']
14
+ if q['type']=='choice':
15
+ probs=ans['probabilities'];assert set(probs)==set(q['criteria'])
16
+ assert all(math.isfinite(x) and 0<=x<=1 for x in probs.values()) and abs(sum(probs.values())-1)<0.01
17
+ assert ans['choice'] in probs
18
+ else:assert math.isfinite(ans['noul']) and 0<=ans['noul']<=1
19
+ print(json.dumps(result))
20
+ try:post(dict(body,state='hello '*20000))
21
+ except urllib.error.HTTPError as e:
22
+ detail=e.read().decode();assert e.code==422 and 'exceeds maximum context length' in detail,detail
23
+ else:raise AssertionError('Expected explicit capacity rejection, not truncation')
24
+ print('HTTP_SMOKE_OK; not a full evaluation or numerical parity certification')