Skip to content

Commit

Permalink
Remove depreciated get_schema code (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
proelke authored Sep 16, 2021
1 parent 1051320 commit 8d1968a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 87 deletions.
48 changes: 0 additions & 48 deletions ocpp/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import json
import decimal
import warnings
from typing import Callable, Dict
from dataclasses import asdict, is_dataclass

Expand All @@ -15,7 +14,6 @@
ProtocolError, ValidationError,
UnknownCallErrorCodeError)

_schemas: Dict[str, Dict] = {}
_validators: Dict[str, Draft4Validator] = {}


Expand Down Expand Up @@ -95,52 +93,6 @@ def pack(msg):
return msg.to_json()


def get_schema(message_type_id, action, ocpp_version, parse_float=float):
"""
Read schema from disk and return in. Reads will be cached for performance
reasons.
The `parse_float` argument can be used to set the conversion method that
is used to parse floats. It must be a callable taking 1 argument. By
default it is `float()`, but certain schema's require `decimal.Decimal()`.
"""
warnings.warn(
"Depricated as of 0.8.1. Please use `ocpp.messages.get_validator()`."
)

if ocpp_version not in ["1.6", "2.0", "2.0.1"]:
raise ValueError

schemas_dir = 'v' + ocpp_version.replace('.', '')

schema_name = action
if message_type_id == MessageType.CallResult:
schema_name += 'Response'
elif message_type_id == MessageType.Call:
if ocpp_version in ["2.0", "2.0.1"]:
schema_name += 'Request'

if ocpp_version == "2.0":
schema_name += '_v1p0'

dir, _ = os.path.split(os.path.realpath(__file__))
relative_path = f'{schemas_dir}/schemas/{schema_name}.json'
path = os.path.join(dir, relative_path)

if relative_path in _schemas:
return _schemas[relative_path]

# The JSON schemas for OCPP 2.0 start with a byte order mark (BOM)
# character. If no encoding is given, reading the schema would fail with:
#
# Unexpected UTF-8 BOM (decode using utf-8-sig):
with open(path, 'r', encoding='utf-8-sig') as f:
data = f.read()
_schemas[relative_path] = json.loads(data, parse_float=parse_float)

return _schemas[relative_path]


def get_validator(
message_type_id: int,
action: str,
Expand Down
50 changes: 11 additions & 39 deletions tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
FormatViolationError,
PropertyConstraintViolationError,
UnknownCallErrorCodeError)
from ocpp.messages import (validate_payload, get_schema, _schemas,
get_validator, _validators, unpack, Call, CallError,
CallResult, MessageType, _DecimalEncoder)
from ocpp.messages import (validate_payload, get_validator, _validators,
unpack, Call, CallError, CallResult, MessageType,
_DecimalEncoder)


def test_unpack_with_invalid_json():
Expand Down Expand Up @@ -51,34 +51,6 @@ def test_unpack_with_invalid_message_type_id_in_json():
unpack(json.dumps([5, 1]))


def test_get_schema_with_valid_name():
"""
Test if correct schema is returned and if schema is added to cache.
"""
schema = get_schema(MessageType.Call, "Reset", ocpp_version="1.6")

assert schema == _schemas["v16/schemas/Reset.json"]
assert schema == {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "ResetRequest",
"type": "object",
"properties": {
"type": {
'additionalProperties': False,
"type": "string",
"enum": [
"Hard",
"Soft"
]
}
},
"additionalProperties": False,
"required": [
"type"
]
}


def test_get_validator_with_valid_name():
"""
Test if correct validator is returned and if validator is added to cache.
Expand Down Expand Up @@ -107,6 +79,14 @@ def test_get_validator_with_valid_name():
}


def test_get_validator_with_invalid_name():
"""
Test if OSError is raised when schema validation file cannnot be found.
"""
with pytest.raises(OSError):
get_validator(MessageType.Call, "non-existing", ocpp_version="1.6")


def test_validate_set_charging_profile_payload():
"""" Test if payloads with floats are validated correctly.
Expand Down Expand Up @@ -168,14 +148,6 @@ def test_validate_get_composite_profile_payload():
validate_payload(message, ocpp_version="1.6")


def test_get_schema_with_invalid_name():
"""
Test if OSError is raised when schema validation file cannnot be found.
"""
with pytest.raises(OSError):
get_schema(MessageType.Call, "non-existing", ocpp_version="1.6")


@pytest.mark.parametrize('ocpp_version', ['1.6', '2.0'])
def test_validate_payload_with_valid_payload(ocpp_version):
"""
Expand Down

0 comments on commit 8d1968a

Please sign in to comment.