aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / allen / science / test_tools_dispatcher.py
| 1 | import importlib.util |
| 2 | import os |
| 3 | from pathlib import Path |
| 4 | import sys |
| 5 | import unittest |
| 6 | from unittest.mock import patch |
| 7 | |
| 8 | root=Path(os.environ.get('ALLEN_ADAPTER_DIR',str(Path(__file__).parent.parent))) |
| 9 | repo=Path(os.environ.get('ALLEN_REPO_ADAPTER_DIR',str(root))) |
| 10 | sys.path.insert(0,str(repo)) |
| 11 | def load(name,path): |
| 12 | spec=importlib.util.spec_from_file_location(name,path) |
| 13 | module=importlib.util.module_from_spec(spec) |
| 14 | spec.loader.exec_module(module) |
| 15 | return module |
| 16 | science=load('dispatcher_science',root/'allen_science.py') |
| 17 | previous=sys.modules.get('allen_science') |
| 18 | sys.modules['allen_science']=science |
| 19 | try: tools=load('gateway_tools',root/'allen_tools.py') |
| 20 | finally: |
| 21 | if previous is None: del sys.modules['allen_science'] |
| 22 | else: sys.modules['allen_science']=previous |
| 23 | |
| 24 | class ToolTurnControls(unittest.TestCase): |
| 25 | def test_fixed_registry(self): |
| 26 | names=[item['function']['name'] for item in tools.TOOLS] |
| 27 | self.assertEqual(len(names),len(set(names))) |
| 28 | self.assertEqual(set(names),set(tools.SCIENCE_NAMES)|{'repo_analyze','repo_report'}) |
| 29 | for bad in ('deploy','shell','contact','submit','remember','send_mail'): |
| 30 | with self.assertRaises(ValueError): tools.owner(bad) |
| 31 | def test_repository_narrows_capabilities(self): |
| 32 | turn=tools.Turn() |
| 33 | with patch.object(tools,'call',return_value={'events':[],'result':'UNTRUSTED DATA'}) as call: |
| 34 | turn.execute('repo_analyze',{}) |
| 35 | self.assertEqual({x['function']['name'] for x in turn.available()},{'repo_analyze','repo_report'}) |
| 36 | for bad in ('math_compute','web_app','omega_compile','paper','deploy'): |
| 37 | with self.assertRaises(ValueError): turn.execute(bad,{}) |
| 38 | self.assertEqual(call.call_count,1) |
| 39 | turn.execute('repo_report',{}) |
| 40 | self.assertEqual(call.call_count,2) |
| 41 | def test_attempt_limit_includes_failures(self): |
| 42 | turn=tools.Turn() |
| 43 | with patch.object(tools,'call',side_effect=ValueError('refused')) as call: |
| 44 | for _ in range(3): |
| 45 | with self.assertRaises(ValueError): turn.execute('math_compute',{}) |
| 46 | with self.assertRaises(ValueError): turn.execute('math_compute',{}) |
| 47 | self.assertEqual(call.call_count,3) |
| 48 | def test_new_turn_does_not_inherit_repo_authority(self): |
| 49 | first=tools.Turn(); second=tools.Turn() |
| 50 | with patch.object(tools,'call',return_value={'events':[],'result':'data'}): first.execute('repo_analyze',{}) |
| 51 | self.assertFalse(second.repo_only) |
| 52 | self.assertIn('math_compute',{x['function']['name'] for x in second.available()}) |
| 53 | def test_duplicate_json_and_unknown_fields_refused(self): |
| 54 | with self.assertRaises(ValueError): tools.parse_call('{"tool":"repo_analyze","tool":"shell","arguments":{}}') |
| 55 | with self.assertRaises(ValueError): tools.parse_call('{"tool":"paper","arguments":{},"trusted":true}') |
| 56 | def test_valid_science_call_still_admitted(self): |
| 57 | name,args=tools.parse_call('{"tool":"math_compute","arguments":{"title":"Math","expression":"Sin[x]"}}') |
| 58 | self.assertEqual(name,'math_compute') |
| 59 | self.assertEqual(args['expression'],'Sin[x]') |
| 60 | |
| 61 | if __name__=='__main__': unittest.main(verbosity=2) |