Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
worked on PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Pyrathon committed Nov 29, 2018
1 parent 3e56122 commit 626079e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
14 changes: 7 additions & 7 deletions python-packages/order_utils/src/zero_ex/json_schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import jsonschema


class LocalRefResolver(jsonschema.RefResolver):
class _LocalRefResolver(jsonschema.RefResolver):
"""Resolve package-local JSON schema id's."""

def __init__(self):
"""Initialize a new LocalRefResolver instance."""
"""Initialize a new instance."""
self.ref_to_file = {
"/addressSchema": "address_schema.json",
"/hexSchema": "hex_schema.json",
Expand All @@ -32,7 +32,7 @@ def resolve_from_url(self, url: str) -> str:
:param url: a string representing the URL of the JSON schema to fetch.
:returns: a string representing the deserialized JSON schema
:raises jsonschema.ValidationError: when the resource associated with
`url` does not exist.
`url` does not exist.
"""
ref = url.replace("file://", "")
if ref in self.ref_to_file:
Expand All @@ -47,10 +47,10 @@ def resolve_from_url(self, url: str) -> str:
)


# Instantiate the `LocalRefResolver()` only once so that `assert_valid()` can
# Instantiate the `_LocalRefResolver()` only once so that `assert_valid()` can
# perform multiple schema validations without reading from disk the schema
# every time.
LOCAL_RESOLVER = LocalRefResolver()
_LOCAL_RESOLVER = _LocalRefResolver()


def assert_valid(data: Mapping, schema_id: str) -> None:
Expand All @@ -70,5 +70,5 @@ def assert_valid(data: Mapping, schema_id: str) -> None:
"""
# noqa

_, schema = LOCAL_RESOLVER.resolve(schema_id)
jsonschema.validate(data, schema, resolver=LOCAL_RESOLVER)
_, schema = _LOCAL_RESOLVER.resolve(schema_id)
jsonschema.validate(data, schema, resolver=_LOCAL_RESOLVER)
10 changes: 8 additions & 2 deletions python-packages/order_utils/stubs/jsonschema/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from typing import Any, Dict
from typing import Any, Dict, Tuple

class RefResolver: pass

class RefResolver:
def resolve(self, url: str) -> Tuple[str, Dict]:
...


class ValidationError(Exception): pass

def validate(instance: Any, schema: Dict, cls=None, *args, **kwargs) -> None: pass
16 changes: 12 additions & 4 deletions python-packages/order_utils/test/test_json_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@


from zero_ex.order_utils import make_empty_order
from zero_ex.json_schemas import LOCAL_RESOLVER, assert_valid
from zero_ex.json_schemas import _LOCAL_RESOLVER, assert_valid


def test_assert_valid_caches_resources():
"""Test that the JSON ref resolver in `assert_valid()` caches resources"""
LOCAL_RESOLVER._remote_cache.cache_clear()
"""Test that the JSON ref resolver in `assert_valid()` caches resources
In order to test the cache we much access the private class of
`json_schemas` and reset the LRU cache on `_LocalRefResolver`.
For this to happen, we need to disable errror `W0212`
on _LOCAL_RESOLVER
"""
_LOCAL_RESOLVER._remote_cache.cache_clear() # pylint: disable=W0212

assert_valid(make_empty_order(), "/orderSchema")
cache_info = LOCAL_RESOLVER._remote_cache.cache_info()
cache_info = (
_LOCAL_RESOLVER._remote_cache.cache_info() # pylint: disable=W0212
)
assert cache_info.currsize == 4
assert cache_info.hits == 10

0 comments on commit 626079e

Please sign in to comment.