def compare_inferencing_reports(data_graph: GraphLike, expected_graph: GraphLike, expected_result: GraphLike): expected_object = set(expected_graph.objects(expected_result, RDF_object)) if len(expected_object) < 1: raise ReportableRuntimeError( "Cannot check the expected result, the given expectedResult does not have an rdf:object." ) expected_object = next(iter(expected_object)) expected_subject = set(expected_graph.objects(expected_result, RDF_subject)) if len(expected_subject) < 1: raise ReportableRuntimeError( "Cannot check the expected result, the given expectedResult does not have an rdf:subject." ) expected_subject = next(iter(expected_subject)) expected_predicate = set( expected_graph.objects(expected_result, RDF_predicate)) if len(expected_predicate) < 1: raise ReportableRuntimeError( "Cannot check the expected result, the given expectedResult does not have an rdf:predicate." ) expected_predicate = next(iter(expected_predicate)) if isinstance(expected_object, Literal): found_objs = set( data_graph.objects(expected_subject, expected_predicate)) if len(found_objs) < 1: return False found = False for o in found_objs: if isinstance(o, Literal): found = 0 == order_graph_literal(expected_graph, expected_object, data_graph, o) return found elif isinstance(expected_object, BNode): found_objs = set( data_graph.objects(expected_subject, expected_predicate)) if len(found_objs) < 1: return False found = False for o in found_objs: if isinstance(o, BNode): found = 0 == compare_blank_node(expected_graph, expected_object, data_graph, o) return found else: found_triples = set( data_graph.triples( (expected_subject, expected_predicate, expected_object))) if len(found_triples) < 1: return False return True
def compare_validation_reports(report_graph, expected_graph, expected_result): expected_conforms = expected_graph.objects(expected_result, SH_conforms) expected_conforms = set(expected_conforms) if len(expected_conforms) < 1: # pragma: no cover raise ReportableRuntimeError( "Cannot check the expected result, the given expectedResult does not have an sh:conforms." ) expected_conforms = next(iter(expected_conforms)) expected_result_nodes = expected_graph.objects(expected_result, SH_result) expected_result_nodes = set(expected_result_nodes) expected_result_node_count = len(expected_result_nodes) validation_reports = report_graph.subjects(RDF_type, SH_ValidationReport) validation_reports = set(validation_reports) if len(validation_reports) < 1: # pragma: no cover raise ReportableRuntimeError( "Cannot check the validation report, the report graph does not contain a ValidationReport" ) validation_report = next(iter(validation_reports)) clean_validation_reports(report_graph, validation_report, expected_graph, expected_result) eq = compare_blank_node(report_graph, validation_report, expected_graph, expected_result) if eq != 0: return False report_conforms = report_graph.objects(validation_report, SH_conforms) report_conforms = set(report_conforms) if len(report_conforms) < 1: # pragma: no cover raise ReportableRuntimeError( "Cannot check the validation report, the report graph does not have an sh:conforms." ) report_conforms = next(iter(report_conforms)) if bool(expected_conforms.value) != bool(report_conforms.value): # TODO:coverage: write a test for this log.error( "Expected Result Conforms value is different from Validation Report's Conforms value." ) return False report_result_nodes = report_graph.objects(validation_report, SH_result) report_result_nodes = set(report_result_nodes) report_result_node_count = len(report_result_nodes) if expected_result_node_count != report_result_node_count: # TODO:coverage: write a test for this log.error( "Number of expected result's sh:result entries is different from Validation Report's sh:result entries.\n" "Expected {}, got {}.".format(expected_result_node_count, report_result_node_count)) return False return True