def test_rename_attribute(class_node: yatiml.Node) -> None: assert class_node.has_attribute('attr1') assert not class_node.has_attribute('attr2') attr1_value = class_node.get_attribute('attr1').get_value() class_node.rename_attribute('attr1', 'attr2') assert not class_node.has_attribute('attr1') assert class_node.has_attribute('attr2') assert class_node.get_attribute('attr2').get_value() == attr1_value # make sure that this does not raise class_node.rename_attribute('non_existent_attribute', 'attr3')
def test_remove_attribute(class_node: yatiml.Node) -> None: assert class_node.has_attribute('attr1') class_node.remove_attribute('attr1') assert not class_node.has_attribute('attr1') class_node.set_attribute('attr1', 10) class_node.set_attribute('attr2', 11) assert class_node.has_attribute('attr2') class_node.remove_attribute('attr2') assert not class_node.has_attribute('attr2') class_node.remove_attribute('attr2') assert not class_node.has_attribute('attr2')
def test_index_attribute_to_map1(class_node: yatiml.Node) -> None: class_node.index_attribute_to_map('index1', 'item_id') assert class_node.has_attribute('index1') new_mapping = class_node.get_attribute('index1') assert new_mapping.is_mapping() assert new_mapping.has_attribute('item1') item1_mapping = new_mapping.get_attribute('item1') assert item1_mapping.is_mapping() assert not item1_mapping.has_attribute('item_id') assert item1_mapping.has_attribute('price') assert new_mapping.has_attribute('item2') item2_mapping = new_mapping.get_attribute('item2') assert item2_mapping.is_mapping() assert not item2_mapping.has_attribute('item_id') assert item2_mapping.has_attribute('price') assert new_mapping.has_attribute('item3') item3_mapping = new_mapping.get_attribute('item3') assert item3_mapping.is_mapping() assert not item3_mapping.has_attribute('item_id') assert item3_mapping.has_attribute('price') assert item3_mapping.has_attribute('on_sale') # check that it fails silently if the attribute is missing or not a mapping class_node.index_attribute_to_map('non_existent_attribute', 'item_id') class_node.index_attribute_to_map('attr1', 'item_id')
def test_seq_attribute_to_map(class_node: yatiml.Node, class_node_dup_key: yatiml.Node) -> None: assert class_node.has_attribute('list1') assert class_node.get_attribute('list1').is_sequence() class_node.seq_attribute_to_map('list1', 'item_id', 'price') assert class_node.has_attribute_type('list1', dict) attr_node = class_node.get_attribute('list1') assert attr_node.is_mapping() assert attr_node.has_attribute_type('item1', float) item1_node = attr_node.get_attribute('item1') assert item1_node.get_value() == 100.0 assert attr_node.has_attribute_type('item2', dict) item2_node = attr_node.get_attribute('item2') assert not item2_node.has_attribute('item_id') assert item2_node.has_attribute('price') assert item2_node.has_attribute('on_sale') # check that it fails silently if the attribute is missing or not a list class_node.seq_attribute_to_map('non_existent_attribute', 'item_id') class_node.seq_attribute_to_map('attr1', 'item_id') # check that it raises with strict=True and duplicate keys with pytest.raises(yatiml.SeasoningError): class_node_dup_key.seq_attribute_to_map('dup_list', 'item_id', strict=True)
def _yatiml_savorize(cls, node: yatiml.Node) -> None: if node.has_attribute('multiplicity'): if node.has_attribute_type('multiplicity', int): attr = node.get_attribute('multiplicity') start_mark = attr.yaml_node.start_mark end_mark = attr.yaml_node.end_mark new_seq = yaml.nodes.SequenceNode( 'tag:yaml.org,2002:seq', [attr.yaml_node], start_mark, end_mark) node.set_attribute('multiplicity', new_seq)
def test_set_attribute(class_node: yatiml.Node) -> None: assert class_node.get_attribute('attr1').get_value() == 42 class_node.set_attribute('attr1', 43) attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:int' assert attr1.yaml_node.value == '43' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None class_node.set_attribute('attr1', 'test') attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:str' assert attr1.yaml_node.value == 'test' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None class_node.set_attribute('attr1', 3.14) attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:float' assert attr1.yaml_node.value == '3.14' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None class_node.set_attribute('attr1', True) attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:bool' assert attr1.yaml_node.value == 'true' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None class_node.set_attribute('attr1', None) attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:null' assert attr1.yaml_node.value == '' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None assert not class_node.has_attribute('attr2') class_node.set_attribute('attr2', 'testing') attr2 = class_node.get_attribute('attr2') assert attr2.yaml_node.value == 'testing' assert attr2.yaml_node.start_mark is not None assert attr2.yaml_node.end_mark is not None node = yaml.ScalarNode('tag:yaml.org,2002:str', 'testnode') class_node.set_attribute('attr3', node) assert class_node.get_attribute('attr3').yaml_node == node with pytest.raises(TypeError): class_node.set_attribute('attr4', class_node) # type: ignore
def test_index_attribute_to_map2(class_node: yatiml.Node) -> None: class_node.index_attribute_to_map('index1', 'item_id', 'price') assert class_node.has_attribute('index1') new_mapping = class_node.get_attribute('index1') assert new_mapping.is_mapping() assert new_mapping.has_attribute('item1') item1 = new_mapping.get_attribute('item1') print(item1.yaml_node) assert item1.is_scalar(float) assert item1.get_value() == 100.0 assert new_mapping.has_attribute('item2') item2 = new_mapping.get_attribute('item2') assert item2.is_scalar(float) assert item2.get_value() == 200.0 assert new_mapping.has_attribute('item3') item3_mapping = new_mapping.get_attribute('item3') assert item3_mapping.is_mapping() assert not item3_mapping.has_attribute('item_id') assert item3_mapping.has_attribute('price') assert item3_mapping.has_attribute('on_sale')
def test_has_attribute(class_node: yatiml.Node) -> None: assert class_node.has_attribute('attr1') assert class_node.has_attribute('list1') assert not class_node.has_attribute('non_existent_attribute')
def test_dashes_to_unders_in_keys(class_node: yatiml.Node) -> None: assert class_node.has_attribute('dashed-attr') assert class_node.has_attribute('list1') class_node.dashes_to_unders_in_keys() assert class_node.has_attribute('dashed_attr') assert class_node.has_attribute('list1')
def test_unders_to_dashes_in_keys(class_node: yatiml.Node) -> None: assert class_node.has_attribute('undered_attr') assert class_node.has_attribute('attr1') class_node.unders_to_dashes_in_keys() assert class_node.has_attribute('undered-attr') assert class_node.has_attribute('attr1')
def _yatiml_savorize(cls, node: yatiml.Node) -> None: if not node.has_attribute('settings'): node.set_attribute('settings', None) node.map_attribute_to_index('implementations', 'name', 'script') node.map_attribute_to_index('resources', 'name', 'num_cores')