Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit coverage JSON strings and objects #74

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions p3analysis/data/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@

import json
import pkgutil
from typing import Any

import jsonschema


def _validate_coverage_json(json_string: str) -> object:
def _validate_coverage_json(json_data: Any) -> object:
"""
Validate coverage JSON string against schema.
Validate coverage JSON against schema.

Parameters
----------
json_string : String
The JSON string to validate.
json_data : Any
The JSON string or object to validate.

Returns
-------
Object
A Python object corresponding to the JSON string.
A Python object corresponding to the JSON provided.

Raises
------
ValueError
If the JSON string fails to validate.

TypeError
If the JSON string is not a string.
If the JSON fails to validate.
"""
if not isinstance(json_string, str):
raise TypeError("Coverage must be a JSON string.")

instance = json.loads(json_string)
if isinstance(json_data, str):
instance = json.loads(json_data)
else:
instance = json_data

schema_string = pkgutil.get_data(__name__, "coverage-0.3.0.schema")
if not schema_string:
Expand All @@ -44,7 +42,7 @@ def _validate_coverage_json(json_string: str) -> object:
try:
jsonschema.validate(instance=instance, schema=schema)
except jsonschema.exceptions.ValidationError:
msg = "Coverage string failed schema validation"
msg = "Coverage data failed schema validation"
raise ValueError(msg)
except jsonschema.exceptions.SchemaError:
msg = "coverage-0.3.0.schema is not a valid schema"
Expand Down
9 changes: 7 additions & 2 deletions tests/data/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ class TestValidation(unittest.TestCase):

def test_coverage_json_valid(self):
"""p3analysis.data.validation.coverage_json_valid"""
expected_object = [{"file": "path", "id": "sha", "lines": [1, 2, [3, 5]]}]

json_string = '[{"file": "path", "id": "sha", "lines": [1, 2, [3, 5]]}]'
result_object = _validate_coverage_json(json_string)
expected_object = [{"file": "path", "id": "sha", "lines": [1, 2, [3, 5]]}]
self.assertTrue(result_object == expected_object)

json_object = expected_object
result_object = _validate_coverage_json(json_object)
self.assertTrue(result_object == expected_object)

def test_coverage_json_invalid(self):
Expand All @@ -24,7 +29,7 @@ def test_coverage_json_invalid(self):
with self.assertRaises(ValueError):
_validate_coverage_json(json_string)

with self.assertRaises(TypeError):
with self.assertRaises(ValueError):
json_object = [{"file": "path", "id": "sha", "lines": [["1"]]}]
_validate_coverage_json(json_object)

Expand Down
Loading