aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / allen / science / test_science_adapter.py
| 1 | import importlib.util |
| 2 | import json |
| 3 | from pathlib import Path |
| 4 | import unittest |
| 5 | from unittest.mock import patch |
| 6 | |
| 7 | # Override for a repository checkout: sibling gateway adapter outside science/. |
| 8 | import os |
| 9 | path=Path(os.environ.get('ALLEN_SCIENCE_ADAPTER',str(Path(__file__).parent.parent/'allen_science.py'))) |
| 10 | spec=importlib.util.spec_from_file_location('gateway_science_adapter',path) |
| 11 | adapter=importlib.util.module_from_spec(spec) |
| 12 | spec.loader.exec_module(adapter) |
| 13 | |
| 14 | class AdapterControls(unittest.TestCase): |
| 15 | def fixture(self): |
| 16 | return {'events':[{'event':'pane.open','data':{'v':1,'id':'p_a','kind':'math','title':'Math'}}, |
| 17 | {'event':'pane.update','data':{'v':1,'id':'p_a','revision':1,'content':{'text':'symbolic result'}, |
| 18 | 'provenance':{'status':'unverified','engine':'','expression':'','result':'','source':'','commit':''}}}], 'result':'Result text'} |
| 19 | def invoke(self, payload): |
| 20 | with patch.object(adapter.http.client,'HTTPConnection') as conn: |
| 21 | response=conn.return_value.getresponse.return_value |
| 22 | response.status=200 |
| 23 | response.read.return_value=json.dumps(payload).encode() |
| 24 | out=adapter.call('math_compute',{'title':'Math','expression':'Sin[x]'}) |
| 25 | conn.assert_called_once_with('127.0.0.1',18113,timeout=15) |
| 26 | conn.return_value.close.assert_called_once() |
| 27 | return out |
| 28 | def test_positive_fixed_loopback(self): |
| 29 | good=self.fixture() |
| 30 | self.assertEqual(self.invoke(good),good) |
| 31 | def test_authority_events_refused(self): |
| 32 | for name in ('human','state','error','done','start','queue'): |
| 33 | data=self.fixture(); data['events'][0]['event']=name |
| 34 | with self.subTest(name=name), self.assertRaises(ValueError): self.invoke(data) |
| 35 | def test_unknown_tool_does_not_connect(self): |
| 36 | with patch.object(adapter.http.client,'HTTPConnection') as conn: |
| 37 | with self.assertRaises(ValueError): adapter.call('shell',{'command':'deploy'}) |
| 38 | conn.assert_not_called() |
| 39 | def test_content_schema_refused(self): |
| 40 | data=self.fixture(); data['events'][1]['data']['content']['html']='<script>bad</script>' |
| 41 | with self.assertRaises(ValueError): self.invoke(data) |
| 42 | def test_update_before_open_refused(self): |
| 43 | data=self.fixture(); data['events'].reverse() |
| 44 | with self.assertRaises(ValueError): self.invoke(data) |
| 45 | def test_output_size_refused(self): |
| 46 | data=self.fixture(); data['result']='x'*8001 |
| 47 | with self.assertRaises(ValueError): self.invoke(data) |
| 48 | def test_revision_replay_refused(self): |
| 49 | data=self.fixture(); data['events'].append(data['events'][1]) |
| 50 | with self.assertRaises(ValueError): self.invoke(data) |
| 51 | |
| 52 | if __name__=='__main__': unittest.main(verbosity=2) |