NanoJev-Web / tests /test_contract.py
candypunk's picture
Release NanoJev-Web browser action model
a0a9254 verified
Raw History Blame Contribute Delete
1.63 kB
import unittest,sys
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'python'))
from schema import validate_request
from predictor import prepare_examples,unique_object,reject_nonfinite
import json
def request(kind='choice'):
return {'states':[{'id':'one','state':'A browser form','questions':{'next':{'type':kind,'instructions':'Choose','criteria':{'fill':'Fill supplied value','DONE':'Finish'}}}}]}
class ContractTests(unittest.TestCase):
def test_browser_choice(self):self.assertEqual(validate_request(request())[0]['id'],'one')
def test_other_question_types_rejected(self):
for kind in ['boolean','score','game','shell']:
with self.assertRaises(ValueError):validate_request(request(kind))
def test_duplicate_state_rejected(self):
x=request();x['states']*=2
with self.assertRaises(ValueError):validate_request(x)
def test_empty_candidates_rejected(self):
x=request();x['states'][0]['questions']['next']['criteria']={}
with self.assertRaises(ValueError):validate_request(x)
def test_invalid_json_rejected(self):
for raw in ['{"x":1,"x":2}','{"x":NaN}']:
with self.assertRaises(ValueError):json.loads(raw,object_pairs_hook=unique_object,parse_constant=reject_nonfinite)
def test_context_overflow_is_not_silently_truncated(self):
class Tokenizer:
eos_token_id=0
def encode(self,text,add_special_tokens=False):return list(text.encode())
with self.assertRaises(ValueError):prepare_examples(request(),Tokenizer(),4)
if __name__=='__main__':unittest.main()