def test_process_registry_add_by_name(): reg = ProcessRegistry() reg.add_spec_by_name("max") assert set(reg._processes.keys()) == {"max"} spec = reg.get_spec('max') assert spec['id'] == 'max' assert 'largest value' in spec['description'] assert all(k in spec for k in ['parameters', 'returns'])
def test_process_registry_add_hidden(): reg = ProcessRegistry() @reg.add_hidden def foo(*args): return 42 new_foo = reg.get_function('foo') assert new_foo() == 42 with pytest.raises(ProcessUnsupportedException): reg.get_spec('foo')
def test_process_registry_add_simple_function(): reg = ProcessRegistry(argument_names=["args", "env"]) @reg.add_simple_function def add(x: int, y: int = 100): return x + y process = reg.get_function("add") assert process(args={"x": 2, "y": 3}, env=None) == 5 assert process(args={"x": 2}, env=None) == 102 with pytest.raises(ProcessParameterRequiredException): _ = process(args={}, env=None)
def test_process_registry_add_function(): reg = ProcessRegistry() @reg.add_function def max(*args): return max(*args) assert set(reg._processes.keys()) == {"max"} spec = reg.get_spec('max') assert spec['id'] == 'max' assert 'largest value' in spec['description'] assert all(k in spec for k in ['parameters', 'returns']) assert reg.get_function('max') is max
def test_process_registry_add_function_other_name(): reg = ProcessRegistry() @reg.add_function(name="max") def madmax(*args): return max(*args) assert reg.contains("max") spec = reg.get_spec('max') assert spec['id'] == 'max' assert 'largest value' in spec['description'] assert all(k in spec for k in ['parameters', 'returns']) assert reg.get_function('max') is madmax
def test_process_registry_add_by_name_and_namespace(): reg = ProcessRegistry() reg.add_spec_by_name("max", namespace="foo") assert not reg.contains("max") assert reg.contains("max", namespace="foo") with pytest.raises(ProcessUnsupportedException): reg.get_spec('max') spec = reg.get_spec('max', namespace="foo") assert spec['id'] == 'max' assert 'largest value' in spec['description'] assert all(k in spec for k in ['parameters', 'returns'])
def test_process_registry_add_hidden_with_namespace(): reg = ProcessRegistry() def bar(*args): return 42 reg.add_hidden(bar, name="boz", namespace="secret") new_bar = reg.get_function('boz', namespace="secret") assert new_bar() == 42 with pytest.raises(ProcessUnsupportedException): reg.get_spec('bar', namespace="secret") with pytest.raises(ProcessUnsupportedException): reg.get_spec('boz', namespace="secret")
def test_process_registry_add_simple_function_with_name(): reg = ProcessRegistry(argument_names=["args", "env"]) @reg.add_simple_function(name="if") def if_(value, accept, reject=None): return accept if value else reject process = reg.get_function("if") assert process(args={"value": True, "accept": 3}, env=None) == 3 assert process(args={"value": False, "accept": 3}, env=None) is None assert process(args={ "value": False, "accept": 3, "reject": 5 }, env=None) == 5 with pytest.raises(ProcessParameterRequiredException): _ = process(args={}, env=None)
def test_process_registry_add_function_argument_names(): reg = ProcessRegistry(argument_names=["args", "env"]) @reg.add_function def max(args, env=None): return max(*args) with pytest.raises(ProcessRegistryException): @reg.add_function def min(args): return min(*args) assert reg.contains("max") spec = reg.get_spec('max') assert spec['id'] == 'max' assert 'largest value' in spec['description'] assert all(k in spec for k in ['parameters', 'returns']) assert reg.get_function('max') is max
def test_process_registry_load_predefined_specs(): """Test if all spec json files load properly""" reg = ProcessRegistry() for name in reg.list_predefined_specs().keys(): spec = reg.load_predefined_spec(name) assert spec["id"] == name
def test_process_registry_get_specs_namepsaces(): reg = ProcessRegistry() reg.add_spec_by_name("min", namespace="stats") reg.add_spec_by_name("max", namespace="stats") reg.add_spec_by_name("sin", namespace="math") assert set(p['id'] for p in reg.get_specs()) == set() assert set(p['id'] for p in reg.get_specs(namespace="stats")) == {"max", "min"} assert set(p['id'] for p in reg.get_specs(namespace="math")) == {"sin"} assert set(p['id'] for p in reg.get_specs("", namespace="stats")) == { "max", "min" } assert set(p['id'] for p in reg.get_specs("m", namespace="math")) == set() assert set(p['id'] for p in reg.get_specs("in", namespace="stats")) == {"min"} assert set(p['id'] for p in reg.get_specs("in", namespace="math")) == {"sin"}
def test_process_registry_contains(): reg = ProcessRegistry() assert not reg.contains("max") reg.add_spec_by_name("max") assert reg.contains("max")