def test_mandatory_and_repeatable_default_to_empty_strings(): """The Boolean elements remain empty strings if no value is declared.""" sc = TAPStatementTemplate() sc.propertyID = "wdt:P31" sc._normalize_booleans_mandatory_repeatable() assert sc.mandatory == "" assert sc.repeatable == ""
def test_value_node_type_not_parsed_as_list(): """When element not configured to be parsed as list, just pass through.""" config_dict["list_elements"] = [] sc = TAPStatementTemplate() sc.valueNodeType = "iri bnode" sc._parse_elements_configured_as_list_elements(config_dict) assert sc.valueNodeType == "iri bnode"
def test_list_elements_comma_separated(): """Elements enumerated in config settings are parsed as lists.""" config_dict["list_item_separator"] = "," sc = TAPStatementTemplate() sc.propertyID = "dcterms:creator,dcterms:date" sc._parse_elements_configured_as_list_elements(config_dict) assert sc.propertyID == ["dcterms:creator", "dcterms:date"]
def test_warn_if_propertyID_not_URI(): """In DCTAP, propertyID _should_ be an IRI.""" sc = TAPStatementTemplate() sc.propertyID = "P31" sc._warn_if_propertyID_or_valueDataType_not_IRIlike() print(sc.state_warns) print(dict(sc.state_warns)) print(len(dict(sc.state_warns))) assert len(dict(sc.state_warns)) == 1
def test_list_item_separator_defaults_to_single_blank(): """Setting list_item_separator of None defaults to single blank.""" sc = TAPStatementTemplate() config_dict = get_config() config_dict["list_elements"] = ["valueNodeType"] config_dict["list_item_separator"] = None sc.valueNodeType = "iri bnode" sc._parse_elements_configured_as_list_elements(config_dict) assert sc.valueNodeType == ["iri", "bnode"]
def test_valueConstraintType_languagetag_parse(): """If valueConstraintType list, valueConstraint parsed on whitespace.""" sc = TAPStatementTemplate() sc.propertyID = "dcterms:creator" sc.valueConstraintType = "languagetag" sc.valueConstraint = "fr it de" sc._valueConstraintType_languageTag_parse(config_dict) assert sc.valueConstraint == ["fr", "it", "de"]
def test_warn_if_valueConstraintType_pattern_used_with_any_value_shape(): """Regular expressions cannot conform to value shapes.""" sc = TAPStatementTemplate() sc.propertyID = ":status" sc.valueConstraintType = "pattern" sc.valueShape = "Person" sc._valueConstraintType_pattern_warn_if_used_with_value_shape() assert len(sc.state_warns) == 1
def test_warn_if_valueNodeType_literal_used_with_any_value_shape(): """@@@""" sc = TAPStatementTemplate() sc.propertyID = ":status" sc.valueNodeType = "literal" sc.valueShape = "Person" sc._valueDataType_warn_if_valueNodeType_literal_used_with_any_valueShape() assert len(sc.state_warns) == 1
def test_valueConstraintType_pattern_warn_if_not_valid_regex(): """For valueConstraintType pattern, warns if valueConstraint not valid regex.""" sc = TAPStatementTemplate() sc.propertyID = ":status" sc.valueConstraintType = "pattern" sc.valueConstraint = "approved_(*" sc._valueConstraintType_pattern_warn_if_valueConstraint_not_valid_regex() assert len(sc.state_warns) == 1
def test_valueConstraintType_picklist_parse(): """If valueConstraintType picklist, valueConstraint parsed on whitespace.""" st = TAPStatementTemplate() st.propertyID = "dcterms:creator" st.valueConstraintType = "picklist" st.valueConstraint = "one two three" st._valueConstraintType_picklist_parse(config_dict) assert st.valueConstraint == ["one", "two", "three"]
def test_valueConstraintType_picklist_parse_case_insensitive(): """Value constraint types are case-insensitive.""" st = TAPStatementTemplate() st.propertyID = "dcterms:creator" st.valueConstraintType = "PICKLIST" st.valueConstraint = "one two three" # extra whitespace st._valueConstraintType_picklist_parse(config_dict) assert st.valueConstraint == ["one", "two", "three"]
def test_list_elements_single_space_is_default(): """Space is default list item separator.""" sc = TAPStatementTemplate() sc.propertyID = "dcterms:creator dcterms:date" sc.valueNodeType = "iri bnode" sc.valueDataType = "xsd:date xsd:time" sc.valueShape = "a b c d" sc._parse_elements_configured_as_list_elements(config_dict) assert sc.propertyID == ["dcterms:creator", "dcterms:date"] assert sc.valueNodeType == ["iri", "bnode"] assert sc.valueDataType == ["xsd:date", "xsd:time"] assert sc.valueShape == ["a", "b", "c", "d"]
def test_mandatory_repeatable_true_given_supported_boolean_values(): """Literal 'True' (case-insensitive) is a supported Boolean value.""" sc = TAPStatementTemplate() sc.propertyID = "wdt:P31" sc.mandatory = "true" sc.repeatable = "TRUE" sc._normalize_booleans_mandatory_repeatable() assert sc.mandatory is "true" assert sc.repeatable is "true"
def test_mandatory_and_repeatable_one_zero_normalized_to_true_false(): """The integers 0 and 1 are supported Boolean values.""" sc = TAPStatementTemplate() sc.propertyID = "wdt:P31" sc.mandatory = "1" sc.repeatable = "0" sc._normalize_booleans_mandatory_repeatable() assert sc.mandatory is "true" assert sc.repeatable is "false"
def test_list_elements(): """Elements enumerated in config settings are parsed as lists.""" config_dict["list_item_separator"] = " " sc = TAPStatementTemplate() sc.propertyID = "dcterms:creator dcterms:date" sc.valueNodeType = "iri bnode" sc.valueDataType = "xsd:date xsd:time" sc.valueShape = "a b c d" sc._parse_elements_configured_as_list_elements(config_dict) assert sc.propertyID == ["dcterms:creator", "dcterms:date"] assert sc.valueNodeType == ["iri", "bnode"] assert sc.valueDataType == ["xsd:date", "xsd:time"] assert sc.valueShape == ["a", "b", "c", "d"]
def test_mandatory_and_repeatable_raise_warn_unsupported_boolean_values(): """@@@""" sc = TAPStatementTemplate() sc.propertyID = "dc:creator" sc.mandatory = "WAHR" sc.repeatable = "WAHR" sc._normalize_booleans_mandatory_repeatable() print(sc.state_warns) print(dict(sc.state_warns)) print(len(dict(sc.state_warns))) print(f"Mandatory: {sc.mandatory}") print(f"Repeatable: {sc.repeatable}") assert len(sc.state_warns) == 2 assert sc.mandatory is "WAHR" assert sc.repeatable is "WAHR"
def test_valueConstraintType_pattern_is_valid_regex(): """For valueConstraintType pattern, valueConstraint must be valid regex.""" sc = TAPStatementTemplate() sc.propertyID = ":status" sc.valueConstraintType = "pattern" sc.valueConstraint = "approved_*" sc._valueConstraintType_pattern_warn_if_valueConstraint_not_valid_regex() assert sc.valueConstraint sc.valueConstraint = "/approved_*/" sc._valueConstraintType_pattern_warn_if_valueConstraint_not_valid_regex() assert sc.valueConstraint == "/approved_*/" sc.valueConstraint = "^2020 August" sc._valueConstraintType_pattern_warn_if_valueConstraint_not_valid_regex() assert sc.valueConstraint sc.valueConstraint = "'confidential'" sc._valueConstraintType_pattern_warn_if_valueConstraint_not_valid_regex() assert sc.valueConstraint == "'confidential'"
def test_valueConstraintType_warn_if_used_without_valueConstraint(): sc = TAPStatementTemplate() sc.propertyID = ":status" sc.valueConstraintType = "pattern" sc._valueConstraintType_warn_if_used_without_valueConstraint() assert len(sc.state_warns) == 1
def _get_tapshapes(rows, config_dict): """Return tuple: (shapes dict, warnings dict).""" # pylint: disable=too-many-locals # pylint: disable=too-many-branches # pylint: disable=too-many-statements try: dshape = config_dict.get("default_shape_identifier") except KeyError: dshape = "default" (main_stems, xtra_stems) = get_stems(TAPStatementTemplate, config_dict) # fmt: off shapes = {} # Dict for shapeID-to-TAPShape_list. warns = defaultdict(dict) # Dict for shapeID-to-warnings_list. for row in rows: sh_id = "" if row.get("propertyID"): if row.get("shapeID"): sh_id = row.get("shapeID") elif not row.get("shapeID"): try: sh_id = list(shapes)[-1] except IndexError: sh_id = row["shapeID"] = dshape elif row.get("shapeID"): sh_id = row.get("shapeID") if sh_id: if sh_id not in list(shapes): sh_obj = _mkshape(row, config_dict) sh_obj.normalize(config_dict) shapes[sh_id] = sh_obj warns[sh_id] = {} sh_warns = sh_obj.get_warnings() for (elem, warn) in sh_warns.items(): try: warns[sh_id][elem].append(warn) except KeyError: warns[sh_id][elem] = [] warns[sh_id][elem].append(warn) if not row.get("propertyID"): continue st = TAPStatementTemplate() for col in row: if col in main_stems: setattr(st, col, row[col]) elif col in xtra_stems: st.state_extras[col] = row[col] st.normalize(config_dict) shapes[sh_id].state_list.append(st) st_warns = st.get_warnings() for (elem, warn) in st_warns.items(): try: warns[sh_id][elem].append(warn) except KeyError: warns[sh_id][elem] = [] warns[sh_id][elem].append(warn) warns_dict = dict(warns) shapes_dict = {} list_of_shapes = [] shapes_dict["shapes"] = list_of_shapes for sh_obj in list(shapes.values()): sh_dict = asdict(sh_obj) sh_dict["statement_templates"] = sh_dict.pop("state_list") list_of_shapes.append(sh_dict) shapes_dict = _simplify(shapes_dict) return (shapes_dict, warns_dict)