test_conformance_008.py 4.7 KB

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