Skip to content

Commit

Permalink
Fix a bug in CoreJsonSchemaValidator regarding Extended Error Messages .
Browse files Browse the repository at this point in the history
Remove redundant 'type' in test data, to remove confusion with  entityTypeID , and the jsonschema keyword 'type' .
adapt tests and test data for new requriements
  • Loading branch information
MohitYadav-codes committed Jun 27, 2024
1 parent 67212c2 commit 4b5f5c1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
4 changes: 2 additions & 2 deletions akm_tools/validation/data_instance_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def validate(self, instance: dict, **kwargs):
self.object_validators_dict[instance["entityTypeID"]].iter_errors(instance),
key=lambda e: e.path,
)
base_error_msg += "\n".join(x.message for x in additioanl_error_info)
base_error_msg += "\n"
base_error_msg += "\n".join(x.message for x in additioanl_error_info)
base_error_msg += "\n"
return False, base_error_msg
except Exception as e:
raise e
43 changes: 22 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ def simple_schema():
"type": {"type": "string"},
"age": {"type": "number"},
"id": {"type": "string"},
"entityTypeID": {"type": "string"},
},
"required": ["id"],
"required": ["id", "entityTypeID"],
"additionalProperties": False,
}
return schema
Expand All @@ -22,24 +23,27 @@ def simple_schema():
@pytest.fixture
def simple_data():
data = [
{"type": "John", "age": 30, "id": "unique_id_1"},
{"type": "Jane", "age": 25, "id": "unique_id_2"},
{"type": "John", "age": 30, "id": "unique_id_1", "entityTypeID": "type1"},
{"type": "Jane", "age": 25, "id": "unique_id_2", "entityTypeID": "type2"},
]
return data


@pytest.fixture
def simple_data_with_more_attributes():
data = [
{"type": "John", "age": 30, "id": "unique_id_1", "extra_attribute": "wild"},
{"type": "Jane", "age": 25, "id": "unique_id_2", "extra_attribute": "grass"},
{"type": "John", "age": 30, "id": "unique_id_1", "entityTypeID": "type1", "extra_attribute": "wild"},
{"type": "Jane", "age": 25, "id": "unique_id_2", "entityTypeID": "type2", "extra_attribute": "grass"},
]
return data


@pytest.fixture
def simple_data_without_required_attribute():
data = [{"type": "John", "age": 30}, {"type": "Jane", "age": 25}]
data = [
{"type": "John", "age": 30, "entityTypeID": "type1"},
{"type": "Jane", "age": 25, "entityTypeID": "type2"}
]
return data


Expand All @@ -58,9 +62,10 @@ def complex_schema_with_defs():
"type": "object",
"properties": {
"id": {"type": "string"},
"entityTypeID": {"type": "string"},
"definition": {"type": "string"},
},
"required": ["id"],
"required": ["id", "entityTypeID"],
},
"ObjectType1": {
"$id": "complexSchema.ObjectType1",
Expand All @@ -69,7 +74,6 @@ def complex_schema_with_defs():
"properties": {
"name": {"type": "string"},
"description": {"type": "string"},
"type": {"type": "string", "const": "ObjectType1"},
},
"required": ["name", "type"],
"unevaluatedProperties": False,
Expand All @@ -80,7 +84,6 @@ def complex_schema_with_defs():
"allOf": [{"$ref": "complexSchema.BaseClass"}],
"properties": {
"age": {"type": "number"},
"type": {"type": "string", "const": "ObjectType2"},
},
"required": ["type"],
"unevaluatedProperties": False,
Expand All @@ -97,27 +100,25 @@ def complex_data():
data = [
{
"id": "unique_id_1",
"entityTypeID": "type1",
"definition": "Some def1",
"name": "AttributeName",
"type": "ObjectType1",
"description": "some desc",
},
{"id": "unique_id_2", "type": "ObjectType2", "age": 10},
{"id": "unique_id_2", "entityTypeID": "type2", "age": 10},
]
return data


@pytest.fixture
def complex_data_missing_required_attributes(): ## id/type is missing.
def complex_data_missing_required_attributes(): ## id/entityTypeID is missing.
data = [
{
"definition": "Some def1",
"name": "AttributeName",
"type": "ObjectType1",
"description": "some desc",
},
{
"type": "ObjectType2",
"age": 10,
},
]
Expand All @@ -129,15 +130,15 @@ def complex_data_with_additional_attributes():
data = [
{
"id": "unique_id_1",
"entityTypeID": "typObjectType1e1",
"definition": "Some def1",
"name": "AttributeName",
"type": "ObjectType1",
"description": "some desc",
"extra_attribute": "wild",
},
{
"id": "unique_id_2",
"type": "ObjectType2",
"entityTypeID": "ObjectType2",
"age": 10,
"extra_attribute": "grass",
},
Expand All @@ -150,24 +151,24 @@ def data_with_duplicate_ids():
data = [
{
"id": "unique_id_1",
"entityTypeID": "type1",
"definition": "Some def1",
"name": "AttributeName",
"type": "ObjectType1",
"description": "some desc",
},
{
"id": "unique_id_1",
"entityTypeID": "type1",
"definition": "Some def2",
"name": "AttributeName2",
"type": "ObjectType2",
"description": "some desc2",
},
]
return data


@pytest.fixture
def scehma_with_extensions():
def schema_with_extensions():
schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "schema_with_extensions",
Expand All @@ -180,7 +181,7 @@ def scehma_with_extensions():
"description": {"type": "string"},
"entityType": {"type": "string", "const": "ObjectType3"},
},
"required": ["entityType"],
"required": ["id","entityType"],
"unevaluatedProperties": False,
}
schema_extension = {
Expand Down Expand Up @@ -225,7 +226,7 @@ def overlay_existing_data_with_addional_properties():


@pytest.fixture
def ovewrite_existing_data():
def overwrite_existing_data():
data = [
{
"id": "unique_id1",
Expand Down
19 changes: 15 additions & 4 deletions tests/test_CoreJsonSchemaValidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def test_complex_data_validator_with_invalid_attribute(complex_schema_with_defs,
assert all(valid_data) == False


def test_complex_data_validator_with_extended_data(scehma_with_extensions, data_with_extended_properties):
schema, registry = scehma_with_extensions
def test_complex_data_validator_with_extended_data(schema_with_extensions, data_with_extended_properties):
schema, registry = schema_with_extensions
complex_data_validator = CoreJsonSchemaValidator(schema=schema, extended_schema_dir=None)
complex_data_validator.configure_registry(registry)
valid_data = []
Expand All @@ -79,12 +79,23 @@ def test_complex_data_validator_with_extended_data(scehma_with_extensions, data_
assert all(valid_data) == True


def test_complex_data_validator_with_extended_data(scehma_with_extensions, overlay_existing_data_with_addional_properties):
schema, registry = scehma_with_extensions
def test_complex_data_validator_with_overlaid_data(schema_with_extensions, overlay_existing_data_with_addional_properties):
schema, registry = schema_with_extensions
complex_data_validator = CoreJsonSchemaValidator(schema=schema, extended_schema_dir=None)
complex_data_validator.configure_registry(registry)
valid_data = []
for instance in overlay_existing_data_with_addional_properties:
is_valid, _ = complex_data_validator.validate(instance=instance)
valid_data.append(is_valid)
assert all(valid_data) == True


def test_complex_data_validator_with_overwritten_data(schema_with_extensions, overwrite_existing_data):
schema, registry = schema_with_extensions
complex_data_validator = CoreJsonSchemaValidator(schema=schema, extended_schema_dir=None)
complex_data_validator.configure_registry(registry)
valid_data = []
for instance in overwrite_existing_data:
is_valid, _ = complex_data_validator.validate(instance=instance)
valid_data.append(is_valid)
assert all(valid_data) == False

0 comments on commit 4b5f5c1

Please sign in to comment.