test_conformance_008.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 openleadr import OpenADRClient, OpenADRServer, enums
  14. from openleadr.utils import generate_id
  15. from openleadr.messaging import create_message, parse_message
  16. from datetime import datetime, timezone, timedelta
  17. from pprint import pprint
  18. import logging
  19. @pytest.mark.asyncio
  20. async def test_conformance_008_autocorrect(caplog):
  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. 'targets': [{'ven_id': '123'}]
  51. }
  52. # Create a message with this event
  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. assert caplog.record_tuples == [("openleadr", logging.WARNING, f"The active_period duration for event {event_id} (0:05:00) differs from the sum of the interval's durations (0:30:00). The active_period duration has been adjusted to (0:30:00).")]
  61. parsed_type, parsed_msg = parse_message(msg)
  62. assert parsed_type == 'oadrDistributeEvent'
  63. total_time = sum([i['duration'] for i in parsed_msg['events'][0]['event_signals'][0]['intervals']],
  64. timedelta(seconds=0))
  65. assert parsed_msg['events'][0]['active_period']['duration'] == total_time
  66. @pytest.mark.asyncio
  67. async def test_conformance_008_raise():
  68. """
  69. oadrDistributeEvent eventSignal interval durations for a given event MUST
  70. add up to eiEvent eiActivePeriod duration.
  71. """
  72. event_id = generate_id()
  73. event = {'event_descriptor':
  74. {'event_id': event_id,
  75. 'modification_number': 0,
  76. 'modification_date': datetime.now(),
  77. 'priority': 0,
  78. 'market_context': 'MarketContext001',
  79. 'created_date_time': datetime.now(),
  80. 'event_status': enums.EVENT_STATUS.FAR,
  81. 'test_event': False,
  82. 'vtn_comment': 'No Comment'},
  83. 'active_period':
  84. {'dtstart': datetime.now(),
  85. 'duration': timedelta(minutes=5)},
  86. 'event_signals':
  87. [{'intervals': [{'duration': timedelta(minutes=10),
  88. 'signal_payload': 1},
  89. {'duration': timedelta(minutes=10),
  90. 'signal_payload': 2},
  91. {'duration': timedelta(minutes=10),
  92. 'signal_payload': 3}],
  93. 'signal_name': enums.SIGNAL_NAME.SIMPLE,
  94. 'signal_type': enums.SIGNAL_TYPE.DELTA,
  95. 'signal_id': generate_id()
  96. },
  97. {'intervals': [{'duration': timedelta(minutes=1),
  98. 'signal_payload': 1},
  99. {'duration': timedelta(minutes=2),
  100. 'signal_payload': 2},
  101. {'duration': timedelta(minutes=2),
  102. 'signal_payload': 3}],
  103. 'signal_name': enums.SIGNAL_NAME.SIMPLE,
  104. 'signal_type': enums.SIGNAL_TYPE.DELTA,
  105. 'signal_id': generate_id()
  106. }]
  107. }
  108. with pytest.raises(ValueError):
  109. msg = create_message('oadrDistributeEvent',
  110. response={'response_code': 200,
  111. 'response_description': 'OK',
  112. 'request_id': generate_id()},
  113. request_id=generate_id(),
  114. vtn_id=generate_id(),
  115. events=[event])