def test_none_like_2(self): """ Get a nested list corresponding to a tree with only node element per nested list """ # yml_input = """ segments: - layers: - dummy1: 0.1 dummy2: dummy_value test: positions: - 1 """ input_data = yaml.load(yml_input, Loader=yaml.FullLoader) # Point to all elements path = HubitModelPath( "segments[:@IDX_SEG].layers[:@IDX_LAY].test.positions[:@IDX_POS]") tree = LengthTree.from_data(path, input_data) # print(tree) # print(tree.to_list()) result = tree.none_like() print(result) # expected_result = [None] # self.assertListEqual(result, expected_result) path = HubitModelPath( "segments[:@IDX_SEG].layers[:@IDX_LAY].test.positions[IDX_POS]") tree = LengthTree.from_data(path, input_data) # print(tree) # print(tree.to_list()) result = tree.none_like() print(result)
def test_validate_tree_fail(self): qexp = TestQueryExpansion._get_qexp() # Tree corresponds to something 'segments[IDX_SEG].layers[IDX_LAY]' # while qexp was decomposed for the identifier 'IDX_TANK' path = HubitModelPath("segments[IDX_SEG].layers[:@IDX_LAY]") yml_input = """ segments: - layers: - thickness: 0.1 # [m] material: brick - thickness: 0.1 material: brick - layers: - thickness: 0.15 material: concrete - thickness: 0.1 material: concrete """ input_data = yaml.load(yml_input, Loader=yaml.FullLoader) qexp.tree = LengthTree.from_data(path, input_data) with pytest.raises(HubitError): qexp._validate_tree()
def _get_qexp(): path = HubitModelPath( "lines[IDX_LINE].tanks[0@IDX_TANK].vol_outlet_flow") yml_input = """ lines: - tanks: - tank1: 1 - tank2: 2 - tank3: 3 - tanks: - tank1: 1 - tank2: 2 - tank3: 3 - tank4: 4 """ input_data = yaml.load(yml_input, Loader=yaml.FullLoader) tree = LengthTree.from_data(path, input_data) qpath = HubitQueryPath("lines[:].tanks[:].vol_outlet_flow") mpaths = [ "lines[IDX_LINE].tanks[0@IDX_TANK].vol_outlet_flow", "lines[IDX_LINE].tanks[1@IDX_TANK].vol_outlet_flow", "lines[IDX_LINE].tanks[2@IDX_TANK].vol_outlet_flow", ] mpaths = [HubitModelPath(mpath) for mpath in mpaths] cmps = get_mock_components(mpaths) qexp = _QueryExpansion(qpath, mpaths, tree, cmps) return qexp
def test_validate_tree(self): qexp = TestQueryExpansion._get_qexp() qexp._validate_tree() # DummyTree passes validation path = HubitModelPath("segments.layers.positions") qexp.tree = LengthTree.from_data(path, {}) qexp._validate_tree()
def test_from_data1(self): """Extract lengths from input for path""" yml_input = """ segments: - layers: - thickness: 0.1 # [m] material: brick test: positions: [1, ] - thickness: 0.02 material: air test: positions: [1, 2, 3] - thickness: 0.1 material: brick test: positions: [1, 3] inside: temperature: 320. outside: temperature: 273. - layers: - thickness: 0.15 material: concrete test: positions: [1, 2, 3, 4, 5] - thickness: 0.025 material: EPS test: positions: [1,] - thickness: 0.1 material: concrete test: positions: [1, 2,] - thickness: 0.001 material: paint test: positions: [1, 2, 3, 4] inside: temperature: 300. outside: temperature: 273. """ input_data = yaml.load(yml_input, Loader=yaml.FullLoader) # path = "segments[:@IDX_SEG].layers[:@IDX_LAY].test.positions[:@IDX_POS]" path = HubitModelPath( "segments[IDX_SEG].layers[:@IDX_LAY].test.positions[:@IDX_POS]") tree = LengthTree.from_data(path, input_data) tree_as_list = tree.to_list() # print(tree_as_list) # TODO test paths in this case # path = "segments[:@IDX_SEG].layers[:@IDX_LAY].test" expected_lengths = [2, [3, 4], [[1, 3, 2], [5, 1, 2, 4]]] self.assertSequenceEqual(expected_lengths, tree_as_list)
def test_expand_mpath6(self): """As test 8 but the consumed path only subscribes to element 1 of the (leftmost) items. Thus, the expansion leads to a flat list corresponding to the (rightmost) items """ input_data = { "items_a": [ { "attr": { "items": [{ "path": 2 }, { "path": 1 }] } }, { "attr": { "items": [{ "path": 3 }, { "path": 4 }] } }, ], "some_number": 33, } path_consumed_for_name = { "attrs": HubitModelPath("items_a[1@IDX1].attr.items[:@IDX2].path"), "number": HubitModelPath("some_number"), } expected_result = { "attrs": ["items_a[1].attr.items[0].path", "items_a[1].attr.items[1].path"], "number": ["some_number"], } tree_for_name = { name: LengthTree.from_data(path, input_data, prune=True) for name, path in path_consumed_for_name.items() } tree_for_name["attrs"].prune_from_path(path_consumed_for_name["attrs"]) result = { name: tree.expand_path(path_consumed_for_name[name]) for name, tree in tree_for_name.items() } self.assertDictEqual(expected_result, result)
def test_expand_mpath5(self): """Expand subscription path with two wildcards gives a nested list""" input_data = { "items": [ { "attr": { "items": [{ "path": 2 }, { "path": 1 }] } }, { "attr": { "items": [{ "path": 3 }, { "path": 4 }] } }, ], "some_number": 33, } path_consumed_for_name = { "attrs": HubitModelPath("items[:@IDX1].attr.items[:@IDX2].path"), "number": HubitModelPath("some_number"), } expected_result = { "attrs": [ ["items[0].attr.items[0].path", "items[0].attr.items[1].path"], ["items[1].attr.items[0].path", "items[1].attr.items[1].path"], ], "number": ["some_number"], } tree_for_name = { name: LengthTree.from_data(path, input_data) for name, path in path_consumed_for_name.items() } result = { name: tree.prune_from_path(path_consumed_for_name[name]).expand_path( path_consumed_for_name[name]) for name, tree in tree_for_name.items() } self.assertDictEqual(expected_result, result)
def test_none_like_4(self): """ Get a nested list corresponding to a tree with only node element per nested list """ # Two nested lists of length 1 yml_inputs = { "1d": """ layers: - dummy1: 0.1 dummy2: dummy_value test: positions: - 1 """, "2d": """ layers: - dummy1: 0.1 dummy2: dummy_value test: positions: - 1 - 2 - dummy1: 0.1 dummy2: dummy_value test: positions: - 3 - 4 """, } test_items = [ ( HubitModelPath("layers[:@IDX_LAY].test.positions[:@IDX_POS]"), None, { "1d": [[None]], "2d": [[None, None], [None, None]] }, ), ( HubitModelPath("layers[0@IDX_LAY].test.positions[0@IDX_POS]"), None, { "1d": None, "2d": None }, ), ( HubitModelPath("layers[0@IDX_LAY].test.positions[:@IDX_POS]"), None, { "1d": [None], "2d": [None, None] }, ), ( HubitModelPath("layers[:@IDX_LAY].test.positions[0@IDX_POS]"), None, { "1d": [None], "2d": [None, None] }, ), ( HubitModelPath("layers[:@IDX_LAY].test.positions[:@IDX_POS]"), HubitQueryPath("layers[:].test.positions[:]"), { "1d": [[None]], "2d": [[None, None], [None, None]] }, ), ( HubitModelPath("layers[:@IDX_LAY].test.positions[:@IDX_POS]"), HubitQueryPath("layers[0].test.positions[0]"), { "1d": None, "2d": None }, ), ( HubitModelPath("layers[:@IDX_LAY].test.positions[:@IDX_POS]"), HubitQueryPath("layers[:].test.positions[0]"), { "1d": [None], "2d": [None, None] }, ), ( HubitModelPath("layers[:@IDX_LAY].test.positions[:@IDX_POS]"), HubitQueryPath("layers[0].test.positions[:]"), { "1d": [None], "2d": [None, None] }, ), ] for input_id, yml_input in yml_inputs.items(): input_data = yaml.load(yml_input, Loader=yaml.FullLoader) for test_item in test_items: mpath, qpath, expected_result = test_item expected_result = expected_result[input_id] tree = LengthTree.from_data(mpath, input_data, prune=True) if qpath is not None: tree.prune_from_path(qpath) print("Test:", input_id, mpath, qpath) result = tree.none_like() with self.subTest( result=result, expected_result=expected_result, mapth=mpath, qpath=qpath, input_id=input_id, ): if expected_result is None: self.assertEqual(result, expected_result) else: self.assertListEqual(result, expected_result)
def setUp(self): self.tree = LengthTree.from_data( HubitModelPath("segments.layers.positions"), {}) assert isinstance(self.tree, DummyLengthTree)
def test_from_data2(self): """No lengths since there are no index IDs in path""" path = HubitModelPath("segments.layers.positions") calculated_tree = LengthTree.from_data(path, {}) self.assertIsInstance(calculated_tree, DummyLengthTree)