Skip to content

Commit

Permalink
validate_json_schema function to utils, refactored implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-bryson committed Apr 20, 2023
1 parent 7791bd4 commit 199aeaa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 86 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pytest-coverage.txt
.coverage
.pytest_cache
.DS_Store
__pycache__/
__pycache__/
venv/
98 changes: 13 additions & 85 deletions tests/validate/dcatus/test_dcat_us.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import jsonschema
from utils.json_utilities import validate_json_schema


def test_numerical_title(open_catalog_schema, open_numerical_title_json):
dataset_schema = open_catalog_schema
json_data = open_numerical_title_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert False
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert True
validate_json_schema(json_data, dataset_schema)


def test_collection_1_parent_2_children(
Expand All @@ -20,39 +14,21 @@ def test_collection_1_parent_2_children(
dataset_schema = open_catalog_schema
json_data = open_collection_1_parent_2_children_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert True
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert False
validate_json_schema(json_data, dataset_schema)


def test_missing_catalog(open_catalog_schema, open_missing_catalog_json):
dataset_schema = open_catalog_schema
json_data = open_missing_catalog_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert False
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert True
validate_json_schema(json_data, dataset_schema)


def test_ny(open_catalog_schema, open_ny_json):
dataset_schema = open_catalog_schema
json_data = open_ny_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert False
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert True
validate_json_schema(json_data, dataset_schema)


def test_missing_identifier_title(
Expand All @@ -61,65 +37,35 @@ def test_missing_identifier_title(
dataset_schema = open_catalog_schema
json_data = open_missing_identifier_title_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert False
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert True
validate_json_schema(json_data, dataset_schema)


def test_usda_gov(open_catalog_schema, open_usda_gov_json):
dataset_schema = open_catalog_schema
json_data = open_usda_gov_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert True
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert False
validate_json_schema(json_data, dataset_schema)


def test_arm(open_catalog_schema, open_arm_json):
dataset_schema = open_catalog_schema
json_data = open_arm_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert True
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert False
validate_json_schema(json_data, dataset_schema)


def test_large_spatial(open_catalog_schema, open_large_spatial_json):
dataset_schema = open_catalog_schema
json_data = open_large_spatial_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert True
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert False
validate_json_schema(json_data, dataset_schema)


def test_reserved_title(open_catalog_schema, open_reserved_title_json):
dataset_schema = open_catalog_schema
json_data = open_reserved_title_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert True
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert False
validate_json_schema(json_data, dataset_schema)


def test_collection_2_parent_4_children(
Expand All @@ -128,36 +74,18 @@ def test_collection_2_parent_4_children(
dataset_schema = open_catalog_schema
json_data = open_collection_2_parent_4_children_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert True
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert False
validate_json_schema(json_data, dataset_schema)


def test_geospatial(open_catalog_schema, open_geospatial_json):
dataset_schema = open_catalog_schema
json_data = open_geospatial_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert True
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert False
validate_json_schema(json_data, dataset_schema)


def test_null_spatial(open_catalog_schema, open_null_spatial_json):
dataset_schema = open_catalog_schema
json_data = open_null_spatial_json

try:
jsonschema.validate(json_data, schema=dataset_schema)
assert True
except jsonschema.ValidationError as e:
error_message = "error: " + e.message + ". offending element: " + e.json_path
print(error_message)
assert False
validate_json_schema(json_data, dataset_schema)
12 changes: 12 additions & 0 deletions utils/json_utilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import json

import jsonschema


def open_json(file_path):
with open(file_path) as fp:
return json.load(fp)


def validate_json_schema(json_data, dataset_schema):
try:
jsonschema.validate(json_data, schema=dataset_schema)
assert False
except jsonschema.ValidationError as e:
error_message = f"error: {e.message}. offending element: {e.json_path}"
print(error_message)
assert True

1 comment on commit 199aeaa

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
datagovharvester
   __init__.py111 0%
   example.py999 0%
TOTAL10100% 

Tests Skipped Failures Errors Time
12 0 💤 8 ❌ 0 🔥 7.205s ⏱️

Please sign in to comment.