test_conformance_006.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # SPDX-License-Identifier: Apache-2.0
  2. # Copyright 2020 Contributors to OpenLEADR
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. import pytest
  13. from pyopenadr import OpenADRClient, OpenADRServer, enums
  14. from pyopenadr.utils import generate_id
  15. from pyopenadr.messaging import create_message, parse_message
  16. from datetime import datetime, timezone, timedelta
  17. from pprint import pprint
  18. @pytest.mark.asyncio
  19. async def test_conformance_006():
  20. """
  21. The presence of any string except “false” in the oadrDistributeEvent
  22. testEvent element MUST be treated as a trigger for a test event.
  23. """
  24. # Monkey patch our own formatter to prevent an error being raised
  25. from pyopenadr.messaging import TEMPLATES
  26. def booleanformat_monkey(value):
  27. """
  28. Format a boolean value
  29. """
  30. if isinstance(value, bool):
  31. if value == True:
  32. return "true"
  33. elif value == False:
  34. return "false"
  35. else:
  36. return value
  37. booleanformat_original = TEMPLATES.filters['booleanformat']
  38. TEMPLATES.filters['booleanformat'] = booleanformat_monkey
  39. event_id = generate_id()
  40. event = {'event_descriptor':
  41. {'event_id': event_id,
  42. 'modification_number': 0,
  43. 'modification_date': datetime.now(),
  44. 'priority': 0,
  45. 'market_context': 'MarketContext001',
  46. 'created_date_time': datetime.now(),
  47. 'event_status': enums.EVENT_STATUS.FAR,
  48. 'test_event': "HelloThere",
  49. 'vtn_comment': 'No Comment'},
  50. 'active_period':
  51. {'dtstart': datetime.now(),
  52. 'duration': timedelta(minutes=30)},
  53. 'event_signals':
  54. [{'intervals': [{'duration': timedelta(minutes=10),
  55. 'signal_payload': 1},
  56. {'duration': timedelta(minutes=10),
  57. 'signal_payload': 2},
  58. {'duration': timedelta(minutes=10),
  59. 'signal_payload': 3}],
  60. 'signal_name': enums.SIGNAL_NAME.SIMPLE,
  61. 'signal_type': enums.SIGNAL_TYPE.DELTA,
  62. 'signal_id': generate_id()
  63. }]
  64. }
  65. # Create a message with this event
  66. msg = create_message('oadrDistributeEvent',
  67. response={'response_code': 200,
  68. 'response_description': 'OK',
  69. 'request_id': generate_id()},
  70. request_id=generate_id(),
  71. vtn_id=generate_id(),
  72. events=[event])
  73. parsed_type, parsed_message = parse_message(msg)
  74. assert parsed_type == 'oadrDistributeEvent'
  75. assert parsed_message['events'][0]['event_descriptor']['test_event'] == True
  76. # Restore the original booleanformat function
  77. TEMPLATES.filters['booleanformat'] = booleanformat_original