Skip to content

Commit

Permalink
Merge pull request #306 from 4dn-dcic/drr_more_schema_utils
Browse files Browse the repository at this point in the history
More schema utils
  • Loading branch information
drio18 authored May 28, 2024
2 parents a9820a0 + db30f42 commit d2670cc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Change Log
----------


8.9.0
=====

* Add more schema parsing functions to `schema_utils`.


8.8.6
=====

Expand Down
16 changes: 16 additions & 0 deletions dcicutils/schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class JsonSchemaConstants:


class EncodedSchemaConstants:
DESCRIPTION = "description"
IDENTIFYING_PROPERTIES = "identifyingProperties"
LINK_TO = "linkTo"
MERGE_REF = "$merge"
Expand Down Expand Up @@ -187,6 +188,21 @@ def get_one_of_formats(schema: Dict[str, Any]) -> List[str]:
]


def is_link(property_schema: Dict[str, Any]) -> bool:
"""Is property schema a link?"""
return bool(property_schema.get(SchemaConstants.LINK_TO))


def get_enum(property_schema: Dict[str, Any]) -> List[str]:
"""Return the enum of a property schema."""
return property_schema.get(SchemaConstants.ENUM, [])


def get_description(schema: Dict[str, Any]) -> str:
"""Return the description of a schema."""
return schema.get(SchemaConstants.DESCRIPTION, "")


class Schema:

def __init__(self, schema: dict, type: Optional[str] = None) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcicutils"
version = "8.8.6"
version = "8.9.0"
description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources"
authors = ["4DN-DCIC Team <[email protected]>"]
license = "MIT"
Expand Down
47 changes: 45 additions & 2 deletions test/test_schema_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict
from typing import Any, Dict, List

import pytest
from dcicutils import schema_utils
Expand Down Expand Up @@ -41,7 +41,16 @@
}
FORMAT = "email"
PATTERN = "some_regex"
STRING_SCHEMA = {"type": "string", "format": FORMAT, "pattern": PATTERN}
ENUM = ["foo", "bar"]
DESCRIPTION = "foo"
STRING_SCHEMA = {
"type": "string",
"format": FORMAT,
"pattern": PATTERN,
"linkTo": "foo",
"enum": ENUM,
"description": DESCRIPTION,
}
ARRAY_SCHEMA = {"type": "array", "items": [STRING_SCHEMA]}
OBJECT_SCHEMA = {"type": "object", "properties": {"foo": STRING_SCHEMA}}
NUMBER_SCHEMA = {"type": "number"}
Expand Down Expand Up @@ -320,3 +329,37 @@ def test_get_conditional_formats(
schema: Dict[str, Any], expected: Dict[str, Any]
) -> None:
assert schema_utils.get_conditional_formats(schema) == expected


@pytest.mark.parametrize(
"schema,expected",
[
({}, False),
(STRING_SCHEMA, True),
(FORMAT_SCHEMA, False),
],
)
def test_is_link(schema: Dict[str, Any], expected: bool) -> None:
assert schema_utils.is_link(schema) == expected


@pytest.mark.parametrize(
"schema,expected",
[
({}, []),
(STRING_SCHEMA, ENUM),
],
)
def test_get_enum(schema: Dict[str, Any], expected: List[str]) -> None:
assert schema_utils.get_enum(schema) == expected


@pytest.mark.parametrize(
"schema,expected",
[
({}, ""),
(STRING_SCHEMA, DESCRIPTION),
],
)
def test_get_description(schema: Dict[str, Any], expected: str) -> None:
assert schema_utils.get_description(schema) == expected

0 comments on commit d2670cc

Please sign in to comment.