test_conformance_008.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. import warnings
  19. @pytest.mark.asyncio
  20. async def test_conformance_008_autocorrect():
  21. """
  22. oadrDistributeEvent eventSignal interval durations for a given event MUST
  23. add up to eiEvent eiActivePeriod duration.
  24. """
  25. event_id = generate_id()
  26. event = {'event_descriptor':
  27. {'event_id': event_id,
  28. 'modification_number': 0,
  29. 'modification_date': datetime.now(),
  30. 'priority': 0,
  31. 'market_context': 'MarketContext001',
  32. 'created_date_time': datetime.now(),
  33. 'event_status': enums.EVENT_STATUS.FAR,
  34. 'test_event': False,
  35. 'vtn_comment': 'No Comment'},
  36. 'active_period':
  37. {'dtstart': datetime.now(),
  38. 'duration': timedelta(minutes=5)},
  39. 'event_signals':
  40. [{'intervals': [{'duration': timedelta(minutes=10),
  41. 'signal_payload': 1},
  42. {'duration': timedelta(minutes=10),
  43. 'signal_payload': 2},
  44. {'duration': timedelta(minutes=10),
  45. 'signal_payload': 3}],
  46. 'signal_name': enums.SIGNAL_NAME.SIMPLE,
  47. 'signal_type': enums.SIGNAL_TYPE.DELTA,
  48. 'signal_id': generate_id()
  49. }]
  50. }
  51. # Create a message with this event
  52. with pytest.warns(UserWarning):
  53. msg = create_message('oadrDistributeEvent',
  54. response={'response_code': 200,
  55. 'response_description': 'OK',
  56. 'request_id': generate_id()},
  57. request_id=generate_id(),
  58. vtn_id=generate_id(),
  59. events=[event])
  60. parsed_type, parsed_msg = parse_message(msg)
  61. assert parsed_type == 'oadrDistributeEvent'
  62. total_time = sum([i['duration'] for i in parsed_msg['events'][0]['event_signals'][0]['intervals']],
  63. timedelta(seconds=0))
  64. assert parsed_msg['events'][0]['active_period']['duration'] == total_time
  65. @pytest.mark.asyncio
  66. async def test_conformance_008_raise():
  67. """
  68. oadrDistributeEvent eventSignal interval durations for a given event MUST
  69. add up to eiEvent eiActivePeriod duration.
  70. """
  71. event_id = generate_id()
  72. event = {'event_descriptor':
  73. {'event_id': event_id,
  74. 'modification_number': 0,
  75. 'modification_date': datetime.now(),
  76. 'priority': 0,
  77. 'market_context': 'MarketContext001',
  78. 'created_date_time': datetime.now(),
  79. 'event_status': enums.EVENT_STATUS.FAR,
  80. 'test_event': False,
  81. 'vtn_comment': 'No Comment'},
  82. 'active_period':
  83. {'dtstart': datetime.now(),
  84. 'duration': timedelta(minutes=5)},
  85. 'event_signals':
  86. [{'intervals': [{'duration': timedelta(minutes=10),
  87. 'signal_payload': 1},
  88. {'duration': timedelta(minutes=10),
  89. 'signal_payload': 2},
  90. {'duration': timedelta(minutes=10),
  91. 'signal_payload': 3}],
  92. 'signal_name': enums.SIGNAL_NAME.SIMPLE,
  93. 'signal_type': enums.SIGNAL_TYPE.DELTA,
  94. 'signal_id': generate_id()
  95. },
  96. {'intervals': [{'duration': timedelta(minutes=1),
  97. 'signal_payload': 1},
  98. {'duration': timedelta(minutes=2),
  99. 'signal_payload': 2},
  100. {'duration': timedelta(minutes=2),
  101. 'signal_payload': 3}],
  102. 'signal_name': enums.SIGNAL_NAME.SIMPLE,
  103. 'signal_type': enums.SIGNAL_TYPE.DELTA,
  104. 'signal_id': generate_id()
  105. }]
  106. }
  107. with pytest.raises(ValueError):
  108. msg = create_message('oadrDistributeEvent',
  109. response={'response_code': 200,
  110. 'response_description': 'OK',
  111. 'request_id': generate_id()},
  112. request_id=generate_id(),
  113. vtn_id=generate_id(),
  114. events=[event])