File size: 1,628 Bytes
a0a9254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()