test_conformance_008.py 4.7 KB

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