From 8d1968ad2f666017047bb31355b3f2a7758895b1 Mon Sep 17 00:00:00 2001 From: Patrick Roelke Date: Thu, 16 Sep 2021 09:09:04 +0200 Subject: [PATCH] Remove depreciated get_schema code (#249) --- ocpp/messages.py | 48 ---------------------------------------- tests/test_messages.py | 50 ++++++++++-------------------------------- 2 files changed, 11 insertions(+), 87 deletions(-) diff --git a/ocpp/messages.py b/ocpp/messages.py index 658c92690..bfa8d7535 100644 --- a/ocpp/messages.py +++ b/ocpp/messages.py @@ -3,7 +3,6 @@ import os import json import decimal -import warnings from typing import Callable, Dict from dataclasses import asdict, is_dataclass @@ -15,7 +14,6 @@ ProtocolError, ValidationError, UnknownCallErrorCodeError) -_schemas: Dict[str, Dict] = {} _validators: Dict[str, Draft4Validator] = {} @@ -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, diff --git a/tests/test_messages.py b/tests/test_messages.py index 08d968574..cb1ae9e4a 100644 --- a/tests/test_messages.py +++ b/tests/test_messages.py @@ -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(): @@ -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. @@ -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. @@ -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): """