def test_column_metadata_bad_severity(): """Throw exception if severity is not an integer or cannot be converted.""" issue = {"severity": "100"} assert Issue.from_dict(issue).severity == 100 issue["severity"] = "i_am_string" with pytest.raises(ValueError): Issue.from_dict(issue)
def test_column_metadata_optional_fields(): """Ensure that optional fields are populated if missing.""" issue_dict = {"severity": 100} issue = Issue.from_dict(issue_dict) assert issue.explanation assert issue.type assert issue.type.major assert issue.type.minor
def _parse_json_with_schema(schema, json_object): # type: (str, Any) -> Any """Process JSON object according to its schema.""" if json_object is None: # Honor null/None values return None # See if it's an iterable and we need to process it if _is_iterable_schema(schema): if not isinstance(json_object, list): raise ValueError( "Got non-list value for list/set schema {schema}. Value: {value}" .format(schema=schema, value=json_object)) base_schema = _get_base_schema(schema) return ListWrapper([ _parse_json_with_schema(base_schema, element) for element in json_object ]) # Handle "primitive" schemas if schema == "AclTrace": return AclTrace.from_dict(json_object) if schema == "FileLines": return FileLines.from_dict(json_object) if schema == "Flow": return Flow.from_dict(json_object) if schema == "FlowTrace": return FlowTrace.from_dict(json_object) if schema == "Integer" or schema == "Long": return int(json_object) if schema == "Interface": return Interface.from_dict(json_object) if schema == "Ip": return str(json_object) if schema == "Issue": return Issue.from_dict(json_object) if schema == "Node": return json_object["name"] if schema == "BgpRoute": return BgpRoute.from_dict(json_object) if schema == "BgpRouteDiffs": return BgpRouteDiffs.from_dict(json_object) if schema == "Prefix": return str(json_object) if schema == "SelfDescribing": return _parse_json_with_schema(json_object["schema"], json_object.get("value")) if schema == "String": return str(json_object) if schema == "Trace": return Trace.from_dict(json_object) return json_object
def test_column_metadata_no_severity(): """Exception is thrown if severity is missing.""" with pytest.raises(ValueError): Issue.from_dict({"noSeverity": 100}) with pytest.raises(ValueError): Issue.from_dict({}) with pytest.raises(TypeError): Issue.from_dict({"severity": None})
def test_issue_deserialization(): """Test correct deserialization.""" issue_dict = { "severity": 100, "explanation": "itsme", "type": { "major": "maj", "minor": "min" } } issue = Issue.from_dict(issue_dict) assert issue.severity == 100 assert issue.explanation == "itsme" assert issue.type.major == "maj" assert issue.type.minor == "min"
def _get_display_value(schema, json_object): # type (str, Any) -> Any if json_object is None: return None if _is_list_or_set_schema(schema): if not isinstance(json_object, list): raise ValueError("Got non-list value for list/set schema", schema, ":", json_object) output_list = [ str(_get_display_value(_get_base_schema(schema), element)) for element in json_object ] if _get_base_schema(schema) == "FlowTrace": return "\n".join(output_list) else: return output_list if schema == "AclTrace": return AclTrace.from_dict(json_object) if schema == "FileLines": return FileLines.from_dict(json_object) if schema == "Flow": return Flow.from_dict(json_object) if schema == "FlowTrace": return FlowTrace.from_dict(json_object) if schema == "Integer": return int(json_object) if schema == "Interface": return Interface.from_dict(json_object) if schema == "Ip": return str(json_object) if schema == "Issue": return Issue.from_dict(json_object) if schema == "Node": return json_object["name"] if schema == "Prefix": return str(json_object) if schema == "SelfDescribing": return _get_display_value(json_object["schema"], json_object.get("value")) if schema == "String": return str(json_object) return json_object