def test_rule_set(self): ruleset = validation.read_in_ruleset("test_data/test_ruleset.json") section_names = [ 'standard', 'warships', 'transports', 'dummy transport' ] self.assertListEqual(ruleset.get_all_section_names(), section_names) self.assertRaises(ValueError, ruleset.get_section_by_name, 'not existing') warships_section = ruleset.get_section_by_name('warships') self.assertEqual(warships_section.get_section_name(), "warships") # test add rule section new_section = Ruleset.RuleSection('new') duplicate_section = Ruleset.RuleSection('standard') self.assertRaises(ValueError, ruleset.add_rule_section, duplicate_section) ruleset.add_rule_section(new_section) section_names.append('new') self.assertListEqual(ruleset.get_all_section_names(), section_names) filename = "test_data/test_data.json" # NOTE: file are supposed to exists while testing with open(filename) as infile: data = json.load(infile) ruleset.validate(data[0], "id")
def read_in_ruleset(self, ruleset_filename): try: self.ruleset = validation.read_in_ruleset(ruleset_filename) except json.JSONDecodeError as message: logger.error("Error with 'https://www.ebi.ac.uk/ols/api/': %s" % (str(message))) raise OntologyCacheError( "Issue with 'https://www.ebi.ac.uk/ols/api/'")
def test_rule_section(self): ruleset = validation.read_in_ruleset("test_data/test_ruleset.json") warships_section = ruleset.get_section_by_name('warships') self.assertRaises(TypeError, warships_section.add_condition, 12, 'value') self.assertRaises(TypeError, warships_section.add_condition, True, 'value') self.assertRaises(TypeError, warships_section.add_condition, 'field', -12.34) self.assertRaises(TypeError, warships_section.add_condition, 'field', False) expected_conditions = {'role': 'warship'} self.assertDictEqual(warships_section.get_conditions(), expected_conditions) self.assertRaises(ValueError, warships_section.add_condition, 'role', 'second role') self.assertRaises( ValueError, warships_section.add_rule, Ruleset.RuleField("weapon", "limited value", 'mandatory')) rules = warships_section.get_rules() self.assertTrue('mandatory' in rules) self.assertTrue('weapon' in rules['mandatory']) rule = rules['mandatory']['weapon'] self.assertTrue(type(rule) is Ruleset.RuleField) self.assertRaises(TypeError, warships_section.check_contain_rule_for_field, False) self.assertRaises(TypeError, warships_section.check_contain_rule_for_field, 12) self.assertFalse( warships_section.check_contain_rule_for_field('random')) self.assertTrue( warships_section.check_contain_rule_for_field('weapon')) # test meet condition filename = "test_data/test_data.json" # NOTE: file are supposed to exists while testing with open(filename) as infile: data = json.load(infile) transport_data = data[0] warship_data = data[1] self.assertRaises(TypeError, warships_section.meet_condition, 'dict') self.assertRaises(TypeError, warships_section.meet_condition, 12) self.assertRaises(KeyError, warships_section.meet_condition, transport_data['attributes']) self.assertFalse(warships_section.meet_condition(transport_data)) self.assertTrue(warships_section.meet_condition(warship_data)) standard_section = ruleset.get_section_by_name("standard") self.assertTrue(standard_section.meet_condition(warship_data)) self.assertTrue(standard_section.meet_condition(transport_data))
def test_check_ruleset(self): filename = "test_data/test_error_ruleset_missing_attributes.json" self.assertRaises(KeyError, validation.read_in_ruleset, filename) expected: List[str] = [ 'Error: Valid units provided for field id having type as text which does not expect units', 'Error: Field crew_capacity has type as number but no valid units provided', 'Error: No valid values should be provided to field class as being of text type', 'Error: No valid terms provided to field Fake which is essential to be of ontology_id type', 'Error: There is no allowed values for field Availability being of limited value type', 'Error: No valid values should be provided to field manufacturer country as being of ontology_id type', 'Warning: Ontology terms are provided for field color. ' 'Please re-consider whether it needs to change to ontology_id type.' ] filename = "test_data/test_error_ruleset.json" ruleset = validation.read_in_ruleset(filename) results = validation.check_ruleset(ruleset) self.assertListEqual(expected, results.get_messages()) print("here") print(results.get_messages()) expected = [] ruleset = validation.read_in_ruleset(static_parameters.ruleset_filename) results = validation.check_ruleset(ruleset) self.assertListEqual(expected, results.get_messages())
def test_image_sample(self): # get image test file filename = "test_data/image_sample.json" # NOTE: file are supposed to exists while testing with open(filename) as infile: sample = json.load(infile) # read ruleset ruleset = validation.read_in_ruleset( static_parameters.ruleset_filename) # now check sample result = ruleset.validate(sample).get_messages() self.assertEqual(result, [])
def test_validate(self): ruleset = validation.read_in_ruleset("test_data/test_ruleset.json") expected_result: Dict[str, List[str]] = { "cardinality": [ 'Error: Multiple values supplied for field passenger_capacity which does not allow multiple values ' '(standard section) for Record 404-T-132-4FE274A', 'Error: Mandatory field crew_capacity has empty value (standard section) for Record 404-T-132-4FE274A', 'Error: Maximum of 2 values allowed for field color but 3 values provided (standard section)' ' for Record 404-T-132-4FE274A', 'Warning: recommended field manufacturer country has empty value, ' 'better remove the field (standard section) for Record 502-W-133-4FE274B', "{'Pass': 0, 'Warning': 1, 'Error': 1}" ], "units": [ 'Error: One of km, m need to be present for the field length ' '(standard section) for Record 404-T-132-4FE274A', 'Error: g for field cargo_capacity is not in the valid units list (kg) ' '(transports section) for Record 404-T-132-4FE274A', 'Warning: No units required but person is used as unit for field id ' '(standard section) for Record 502-W-133-4FE274B', "{'Pass': 0, 'Warning': 1, 'Error': 1}" ], "values": [ 'Error: <None> of field Availability is neither "no longer available" nor a valid URI ' '(standard section) for Record 404-T-132-4FE274A', 'Error: <purple> of field color is not in the valid values list ' '(<red>, <yellow>, <blue>) (standard section) for Record 502-W-133-4FE274B', "{'Pass': 0, 'Warning': 0, 'Error': 2}" ], "allowed_terms": [ 'Error: Invalid URI value wrong url in field manufacturer country ' '(standard section) for Record 404-T-132-4FE274A', 'Warning: Ontology provided for field role however there is no requirement in the ruleset ' '(standard section) for Record 404-T-132-4FE274A', 'Error: Not valid ontology term PATO_0000383 in field manufacturer country' ' (standard section) for Record 502-W-133-4FE274B', "{'Pass': 0, 'Warning': 0, 'Error': 2}" ], "extra_and_missing_field": [ 'Error: Mandatory field passenger_capacity in standard section could not be found ' 'for Record 404-T-132-4FE274A', 'Warning: Column extra could not be found in ruleset for Record 502-W-133-4FE274B', "{'Pass': 0, 'Warning': 1, 'Error': 1}" ], "types": [ 'Error: For field crew_capacity the provided value 5 is not represented as/of the expected type Number ' '(standard section) for Record 404-T-132-4FE274A', 'Error: For field instruction the provided value 5 is not of the expected type doi ' '(standard section) for Record 404-T-132-4FE274A', 'Error: Invalid URI value wrong url for field video demo ' '(standard section) for Record 404-T-132-4FE274A', 'Error: The date value 15-07-2408 does not match to the format YYYY-MM-DD ' '(standard section) for Record 404-T-132-4FE274A', 'Error: No url found for the field manufacturer country which has the type of ontology_id ' '(standard section) for Record 404-T-132-4FE274A', 'Error: Invalid DOI value supplied in the field instruction ' '(standard section) for Record 502-W-133-4FE274B', 'Error: One of YYYY-MM-DD, YYYY-MM need to be present for the field production date ' '(standard section) for Record 502-W-133-4FE274B', 'Warning: Provided iri http://wrong.iri.com/obo/NCIT_C17233 does not match ' 'the iri retrieved from OLS in the field manufacturer country ' '(standard section) for Record 502-W-133-4FE274B', "{'Pass': 0, 'Warning': 0, 'Error': 2}" ], "types2": [ 'Error: Email address must have prefix "mailto:" in the field video demo ' '(standard section) for Record 404-T-132-4FE274A', 'Warning: Provided value United kingdom has different letter case to the term ' 'referenced by http://purl.obolibrary.org/obo/NCIT_C17233 (standard section) ' 'for Record 404-T-132-4FE274A', 'Error: mailto must be at position 1 to be a valid email value in the field video demo ' '(standard section) for Record 502-W-133-4FE274B', 'Error: Provided value United Kingdom does not match to the provided ontology ' 'http://purl.obolibrary.org/obo/NCIT_C16699 (standard section) for Record ' '502-W-133-4FE274B', "{'Pass': 0, 'Warning': 0, 'Error': 2}" ] } self.maxDiff = None for error_type in expected_result.keys(): filename = "test_data/data/test_error_rule_" + error_type + ".json" # NOTE: file are supposed to exists while testing with open(filename) as infile: data = json.load(infile) submission_result: List[ ValidationResult.ValidationResultRecord] = [] actual_values: List[str] = [] for record in data: record_result = ruleset.validate(record, "id") if record_result.is_empty(): record_result.add_validation_result_column( ValidationResult.ValidationResultColumn( "Pass", "", record_result.record_id, "")) submission_result.append(record_result) for msg in record_result.get_messages(): actual_values.append(msg) summary, vcr_summary, vcr_details = \ validation.deal_with_validation_results(submission_result, verbose=False) summary_str = str(summary) actual_values.append(summary_str) self.assertListEqual(expected_result[error_type], actual_values)
def test_rule_set_serializer(self): ruleset = validation.read_in_ruleset("test_data/test_ruleset.json") # test string serialization self.assertIsInstance(ruleset.to_json(), str)