def test_child_insert_index(): # Part 1 eml = Node(names.EML) eml.add_attribute("packageId", "edi.23.1") eml.add_attribute("system", "metapype") access = Node(names.ACCESS, parent=eml) eml.add_child(access) additional_metadata = Node(names.ADDITIONALMETADATA, parent=eml) eml.add_child(additional_metadata) r = rule.get_rule(names.EML) dataset = Node(names.DATASET, parent=eml) index = r.child_insert_index(eml, dataset) eml.add_child(dataset, index=index) validate.node(eml) # Part 2 r = rule.get_rule(names.ASSOCIATEDPARTY) associated_party = Node(names.ASSOCIATEDPARTY) organization_name = Node(names.ORGANIZATIONNAME) index = r.child_insert_index(associated_party, organization_name) associated_party.add_child(organization_name, index) address = Node(names.ADDRESS) associated_party.add_child(address) online_url = Node(names.ONLINEURL) associated_party.add_child(online_url) position_name = Node(names.POSITIONNAME) index = r.child_insert_index(associated_party, position_name) associated_party.add_child(position_name, index) role = Node(names.ROLE) index = r.child_insert_index(associated_party, role) associated_party.add_child(role, index) validate.node(associated_party)
def test_get_rule(): r = rule.get_rule(names.ACCESS) assert r.name == rule.RULE_ACCESS assert isinstance(r.attributes, dict) assert isinstance(r.children, list) assert isinstance(r.content_rules, list) assert isinstance(r.content_enum, list)
def test_is_in_path(): associated_party = Node(names.ASSOCIATEDPARTY) organization_name = Node(names.ORGANIZATIONNAME) associated_party.add_child(organization_name) address = Node(names.ADDRESS) associated_party.add_child(address) online_url = Node(names.ONLINEURL) associated_party.add_child(online_url) phone = Node(names.PHONE) r = rule.get_rule(names.ASSOCIATEDPARTY) print("\n") assert Rule._is_in_path(r.children, phone)
def prune(n: Node, strict: bool = False) -> list: """ Prune in place all non-valid nodes from the tree Args: n: Node strict: Returns: List of pruned nodes Side-effects: Non-valid nodes are pruned from the tree """ pruned = list() if n.name != "metadata": try: node(n) except UnknownNodeError as ex: logger.debug(f"Pruning: {n.name}") pruned.append((n, str(ex))) if n.parent is not None: n.parent.remove_child(n) Node.delete_node_instance(n.id) return pruned except ChildNotAllowedError as ex: r = rule.get_rule(n.name) children = n.children.copy() for child in children: if not r.is_allowed_child(child.name): logger.debug(f"Pruning: {child.name}") pruned.append((child, str(ex))) n.remove_child(child) Node.delete_node_instance(child.id) except MetapypeRuleError as ex: logger.info(ex) children = n.children.copy() for child in children: pruned += prune(child, strict) if strict and child not in pruned: try: node(child) except MetapypeRuleError as ex: logger.debug(f"Pruning: {child.name}") pruned.append((child, str(ex))) n.remove_child(child) Node.delete_node_instance(child.id) return pruned
def test_missing_numerical_unit(): unit = Node(names.UNIT, parent=None) r = rule.get_rule(names.UNIT) with pytest.raises(MetapypeRuleError): r.validate_rule(unit) # Check error errs = [] validate.tree(unit, errs) assert len(errs) == 1 err_code, msg, node, *args = errs[0] assert err_code == ValidationError.MIN_CHOICE_UNMET assert args[0] == 'unit' # With a customUnit, it should be ok custom_unit = Node(names.CUSTOMUNIT, parent=unit) custom_unit.content = 'bushels per parsec' unit.add_child(custom_unit) validate.tree(unit)
def node(n: Node, errs: list = None) -> None: """ Validates a given node for rule compliance. Args: n: Node instance to be validated errs: List container for validation errors (fail fast if None) Returns: None Raises: MetapypeRuleError: An unknown type of node for EML """ if n.name not in rule.node_mappings: msg = f"Unknown node rule type: {n.name}" if errs is None: raise UnknownNodeError(msg) else: errs.append((ValidationError.UNKNOWN_NODE, msg, n)) else: node_rule = rule.get_rule(n.name) node_rule.validate_rule(n, errs)
def test_is_allowed_child(): r = rule.get_rule(names.EML) allowed = r.is_allowed_child(names.ACCESS) assert allowed allowed = r.is_allowed_child(names.INDIVIDUALNAME) assert not allowed
def test_str_content(node): permission_allow = node.find_descendant(names.PERMISSION) permission_allow.content = 1 r = rule.get_rule(names.PERMISSION) with pytest.raises(MetapypeRuleError): r.validate_rule(permission_allow)
def test_non_empty_content(node): principal_allow = node.find_descendant(names.PRINCIPAL) principal_allow.content = None r = rule.get_rule(names.PRINCIPAL) with pytest.raises(MetapypeRuleError): r.validate_rule(principal_allow)
def test_empty_content(node): access = node.find_child(names.ACCESS) access.content = "some content" r = rule.get_rule(names.ACCESS) with pytest.raises(MetapypeRuleError): r.validate_rule(access)
def test_rule_validation(node): r = rule.get_rule(names.ACCESS) access = node.find_child("access") assert r.validate_rule(access) is None