def test_scope_start(self): cfg = { "path": "dummy", "func_name": "dummy", "provides_results": [ { "name": "attr", "path": "list[IDX].attr.path2" }, ], } # No scope cmp = HubitModelComponent.from_cfg(cfg, 0) assert cmp.scope_start == (None, None) # All slice cfg.update({"index_scope": {"IDX": "1:4"}}) cmp = HubitModelComponent.from_cfg(cfg, 0) assert cmp.scope_start == ("IDX", 1) # All indices cfg.update({"index_scope": {"IDX": ":"}}) cmp = HubitModelComponent.from_cfg(cfg, 0) assert cmp.scope_start == ("IDX", 0)
def test_provides_nothing(self): """ Componet provides nothing => error """ cfg = { "path": "dummy", "func_name": "dummy", "consumes_input": [{ "name": "attr", "path": "shared.input.attr.path" }], } with self.assertRaises(HubitModelComponentError): HubitModelComponent.from_cfg(cfg, 0)
def test_2(self): """ Initialize a simple worker with no idxids """ func = None version = None cfg = { "path": "dummy", "func_name": "dummy", "provides_results": [ {"name": "attr1", "path": "shared.results.attr1.path"}, {"name": "attr2", "path": "shared.results.attr2.path"}, ], "consumes_input": [{"name": "attr", "path": "shared.input.attr.path"}], } component = HubitModelComponent.from_cfg(cfg, 0) # No index IDs in model tree_for_idxcontext = {"": DummyLengthTree()} # Query something known to exist querystring = HubitQueryPath(component.provides_results[0].path) w = _Worker( lambda x: x, lambda x: x, component, querystring, func, version, tree_for_idxcontext, dryrun=True, )
def test_1(self): """ Fails since query does not match. """ func = None version = None cfg = { "path": "dummy", "func_name": "dummy", "provides_results": [ {"name": "attr1", "path": "shared.results.attr1.path"}, {"name": "attr2", "path": "shared.results.attr2.path"}, ], "consumes_input": [{"name": "attr", "path": "shared.input.attr.path"}], } component = HubitModelComponent.from_cfg(cfg, 0) # No index IDs in model tree = DummyLengthTree() querystring = HubitQueryPath("shared.attr.path") with self.assertRaises(HubitWorkerError) as context: w = _Worker( lambda x: x, lambda x: x, component, querystring, func, version, tree, dryrun=True, )
def test_circular_refs(self): cfg = { "path": "dummy", "func_name": "dummy", "provides_results": [{ "name": "attr", "path": "shared.input.attr.path" }], "consumes_results": [{ "name": "attr", "path": "shared.input.attr.path" }], } with self.assertRaises(AssertionError): HubitModelComponent.from_cfg(cfg, 0)
def test_invalid_scope(self): # Multiple scopes not allowed cfg = { "path": "dummy", "func_name": "dummy", "index_scope": { "IDX1": "1:", "IDX2": ":" }, "provides_results": [{ "name": "attr", "path": "shared.input.attr.path1" }], "consumes_results": [{ "name": "attr", "path": "shared.input.attr.path2" }], } with self.assertRaises(AssertionError): HubitModelComponent.from_cfg(cfg, 0) # Scope has invalid range cfg = { "path": "dummy", "func_name": "dummy", "index_scope": { "IDX": "1:chars" }, "provides_results": [{ "name": "attr", "path": "shared.input.attr.path1" }], "consumes_results": [{ "name": "attr", "path": "shared.input.attr.path2" }], } with self.assertRaises(HubitError): HubitModelComponent.from_cfg(cfg, 0)
def test_get_paths(self): cfg = { "path": "dummy", "func_name": "dummy", "provides_results": [ { "name": "attr", "path": "list[IDX].attr.path2" }, ], "consumes_input": [ { "name": "attr1", "path": "shared.input.attr.path1" }, { "name": "attr2", "path": "shared.input.attr.path2" }, ], "consumes_results": [ { "name": "attr3", "path": "shared.input.attr.path3" }, { "name": "attr4", "path": "shared.input.attr.path4" }, ], } cmp = HubitModelComponent.from_cfg(cfg, 0) result = set(cmp.consumes_input_paths) expected_result = set([ HubitModelPath("shared.input.attr.path1"), HubitModelPath("shared.input.attr.path2"), ]) assert result == expected_result result = set(cmp.consumes_results_paths) expected_result = set([ HubitModelPath("shared.input.attr.path3"), HubitModelPath("shared.input.attr.path4"), ]) assert result == expected_result result = set(cmp.provides_results_paths) expected_result = set([ HubitModelPath("list[IDX].attr.path2"), ]) assert result == expected_result
def _make_worker(): qrunner = Mock() qrunner.check_cache.return_value = None cname = None func = dummy_function version = None cfg = { "path": "dummy", "func_name": "dummy_fun", "provides_results": [ { "name": "attrs1", "path": "items_outer[:@IDX_OUTER].attr.items_inner[:@IDX_INNER].path1", } ], "consumes_input": [ { "name": "attrs", "path": "items_outer[:@IDX_OUTER].attr.items_inner[:@IDX_INNER].path", }, {"name": "number", "path": "some_number"}, ], "consumes_results": [ {"name": "dependency", "path": "value"}, {"name": "dependency2", "path": "items_outer[:@IDX_OUTER].value"}, ], } component = HubitModelComponent.from_cfg(cfg, 0) # Required for shape inference. TODO: change when shapes are defined in model inputdata = { "items_outer": [ {"attr": {"items_inner": [{"path": 2}, {"path": 1}]}}, {"attr": {"items_inner": [{"path": 3}, {"path": 4}]}}, ], "some_number": 33, } querystring = HubitQueryPath("items_outer[1].attr.items_inner[0].path1") _tree_for_idxcontext = tree_for_idxcontext([component], inputdata) w = _Worker( lambda x: x, lambda x: x, component, querystring, func, version, _tree_for_idxcontext, dryrun=True, # Use dryrun to easily predict the result ) return w
def test_names_reused(self): """ Names shared between """ cfg = { "path": "dummy", "func_name": "dummy", "provides_results": [{ "name": "attr", "path": "shared.input.attr.path3" }], "consumes_input": [{ "name": "attr", "path": "shared.input.attr.path1" }], "consumes_results": [{ "name": "attr", "path": "shared.input.attr.path2" }], } with self.assertRaises(AssertionError): HubitModelComponent.from_cfg(cfg, 0)
def test_5(self): """ Initialize worker with ILOC locations in query and ILOC wildcards in bindings """ func = None version = None cfg = """ path: dummy, func_name: dummy, provides_results: - name: k_therm path: segments[IDX_SEG].layers[:@IDX_LAY].k_therm consumes_input: - name: material path: segments[IDX_SEG].layers[:@IDX_LAY].material """ component = HubitModelComponent.from_cfg( yaml.load(cfg, Loader=yaml.FullLoader), 0 ) # Query something known to exist querystr = HubitQueryPath("segments[0].layers[0].k_therm") seg_node = LengthNode(2) lay_nodes = LengthNode(2), LengthNode(2) seg_node.set_children(lay_nodes) nodes = [seg_node] nodes.extend(lay_nodes) level_names = "IDX_SEG", "IDX_LAY" tree = LengthTree(nodes, level_names) _tree_for_idxcontext = {tree.index_context: tree} querystr = HubitQueryPath(querystr) w = _Worker( lambda x: x, lambda x: x, component, querystr, func, version, _tree_for_idxcontext, dryrun=True, )
def test_8(self): """ Test compression of indices. The query does not include any indices """ func = None version = None comp_yml = """ path: dummy, func_name: dummy, provides_results: - name: mylist path: factors consumes_input: - name: factors path: list[:@IDX1].some_attr.factors """ component = HubitModelComponent.from_cfg( yaml.load(comp_yml, Loader=yaml.FullLoader), 0 ) # Query something known to exist querystr = "factors" idx1_node = LengthNode(2) nodes = [idx1_node] level_names = ("IDX1",) tree = LengthTree(nodes, level_names) dummy_tree = DummyLengthTree() _tree_for_idxcontext = {"": dummy_tree, tree.index_context: tree} querystr = HubitQueryPath(querystr) w = _Worker( lambda x: x, lambda x: x, component, querystr, func, version, _tree_for_idxcontext, dryrun=True, )
def test_get_func(self): # Location of model file base_path = THIS_DIR cfg = { "path": "dummy", "func_name": "dummy_func", "provides_results": [ { "name": "attr", "path": "list[IDX].attr.path1" }, ], } component = HubitModelComponent.from_cfg(cfg, 0) # Components already known components_known = {} # cannot find module "dummy" with pytest.raises(AssertionError): _QueryRunner._get_func(base_path, component, components_known) cfg["path"] = "config_test.py" component = HubitModelComponent.from_cfg(cfg, 0) # Cannot find attribute "dummy_func" with pytest.raises(AttributeError): _QueryRunner._get_func(base_path, component, components_known) cfg["path"] = "qrun_test.py" cfg["func_name"] = "function_for_test" component = HubitModelComponent.from_cfg(cfg, 0) func, version, components_known = _QueryRunner._get_func( base_path, component, components_known) assert func.__name__ == function_for_test.__name__ assert func.__module__ == function_for_test.__module__.split(".")[-1] assert version == "None" assert components_known == {component.id: (func, version)} # Run a second time so that we test components_known is not empty func, version, components_known = _QueryRunner._get_func( base_path, component, components_known) assert func.__name__ == function_for_test.__name__ assert func.__module__ == function_for_test.__module__.split(".")[-1] assert version == "None" assert components_known == {component.id: (func, version)} cfg["path"] = "math" cfg["func_name"] = "cos" cfg["is_dotted_path"] = True components_known = {} component = HubitModelComponent.from_cfg(cfg, 0) func, version, components_known = _QueryRunner._get_func( base_path, component, components_known) print(func.__module__) assert func.__name__ == "cos" assert func.__module__ == "math" assert version == "None" assert components_known == {component.id: (func, version)}
def test_validate_scope(self): """ The scope is validated. TODO: Separate validation or make it optional to mak the test more specific """ cfg = { "path": "dummy", "func_name": "dummy", "provides_results": [ { "name": "attr", "path": "list[IDX].attr.path1" }, { "name": "attr", "path": "list[IDX-1].attr.path2" }, ], } # No scope HubitModelComponent.from_cfg(cfg, 0) # Add various scopes cfg.update({"index_scope": {"IDX": "1"}}) HubitModelComponent.from_cfg(cfg, 0) cfg.update({"index_scope": {"IDX": "1:4"}}) HubitModelComponent.from_cfg(cfg, 0) cfg.update({"index_scope": {"IDX": ":"}}) HubitModelComponent.from_cfg(cfg, 0) # Component with explicit index reference cfg = { "path": "dummy", "func_name": "dummy", "provides_results": [ { "name": "attr", "path": "list[2@IDX].attr.path1" }, ], } # Add various scopes cfg.update({"index_scope": {"IDX": "2"}}) HubitModelComponent.from_cfg(cfg, 0) cfg.update({"index_scope": {"IDX": "1:4"}}) HubitModelComponent.from_cfg(cfg, 0) cfg.update({"index_scope": {"IDX": ":"}}) HubitModelComponent.from_cfg(cfg, 0) # Component with index wildcard cfg = { "path": "dummy", "func_name": "dummy", "provides_results": [ { "name": "attr", "path": "list[:@IDX].attr.path2" }, ], } # Add various scopes cfg.update({"index_scope": {"IDX": "2"}}) with pytest.raises(HubitModelComponentError): HubitModelComponent.from_cfg(cfg, 0) cfg.update({"index_scope": {"IDX": "1:4"}}) with pytest.raises(HubitModelComponentError): HubitModelComponent.from_cfg(cfg, 0) cfg.update({"index_scope": {"IDX": ":"}}) HubitModelComponent.from_cfg(cfg, 0)