test_objects.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. from openleadr import objects, enums
  2. from datetime import datetime, timedelta, timezone
  3. from openleadr.utils import ensure_bytes
  4. from openleadr.messaging import create_message, parse_message, validate_xml_schema
  5. from pprint import pprint
  6. import pytest
  7. def test_oadr_event():
  8. event = objects.Event(
  9. event_descriptor=objects.EventDescriptor(
  10. event_id=1,
  11. modification_number=0,
  12. market_context='MarketContext1',
  13. event_status=enums.EVENT_STATUS.NEAR),
  14. active_period=objects.ActivePeriod(
  15. dtstart=datetime.now(),
  16. duration=timedelta(minutes=10)),
  17. event_signals=[objects.EventSignal(
  18. intervals=[
  19. objects.Interval(
  20. dtstart=datetime.now(),
  21. duration=timedelta(minutes=5),
  22. uid=0,
  23. signal_payload=1),
  24. objects.Interval(
  25. dtstart=datetime.now(),
  26. duration=timedelta(minutes=5),
  27. uid=1,
  28. signal_payload=2)],
  29. targets=[objects.Target(
  30. ven_id='1234'
  31. )],
  32. signal_name=enums.SIGNAL_NAME.LOAD_CONTROL,
  33. signal_type=enums.SIGNAL_TYPE.LEVEL,
  34. signal_id=1,
  35. current_value=0
  36. )],
  37. targets=[objects.Target(
  38. ven_id='1234'
  39. )]
  40. )
  41. response = objects.Response(response_code=200,
  42. response_description='OK',
  43. request_id='1234')
  44. msg = create_message('oadrDistributeEvent', response=response, events=[event])
  45. validate_xml_schema(ensure_bytes(msg))
  46. message_type, message_payload = parse_message(msg)
  47. def test_oadr_event_targets_by_type():
  48. event = objects.Event(
  49. event_descriptor=objects.EventDescriptor(
  50. event_id=1,
  51. modification_number=0,
  52. market_context='MarketContext1',
  53. event_status=enums.EVENT_STATUS.NEAR),
  54. active_period=objects.ActivePeriod(
  55. dtstart=datetime.now(),
  56. duration=timedelta(minutes=10)),
  57. event_signals=[objects.EventSignal(
  58. intervals=[
  59. objects.Interval(
  60. dtstart=datetime.now(),
  61. duration=timedelta(minutes=5),
  62. uid=0,
  63. signal_payload=1),
  64. objects.Interval(
  65. dtstart=datetime.now(),
  66. duration=timedelta(minutes=5),
  67. uid=1,
  68. signal_payload=2)],
  69. targets=[objects.Target(
  70. ven_id='1234'
  71. )],
  72. signal_name=enums.SIGNAL_NAME.LOAD_CONTROL,
  73. signal_type=enums.SIGNAL_TYPE.LEVEL,
  74. signal_id=1,
  75. current_value=0
  76. )],
  77. targets_by_type={'ven_id': ['ven123']}
  78. )
  79. msg = create_message('oadrDistributeEvent', events=[event])
  80. validate_xml_schema(ensure_bytes(msg))
  81. message_type, message_payload = parse_message(msg)
  82. def test_oadr_event_targets_and_targets_by_type():
  83. event = objects.Event(
  84. event_descriptor=objects.EventDescriptor(
  85. event_id=1,
  86. modification_number=0,
  87. market_context='MarketContext1',
  88. event_status=enums.EVENT_STATUS.NEAR),
  89. active_period=objects.ActivePeriod(
  90. dtstart=datetime.now(),
  91. duration=timedelta(minutes=10)),
  92. event_signals=[objects.EventSignal(
  93. intervals=[
  94. objects.Interval(
  95. dtstart=datetime.now(),
  96. duration=timedelta(minutes=5),
  97. uid=0,
  98. signal_payload=1),
  99. objects.Interval(
  100. dtstart=datetime.now(),
  101. duration=timedelta(minutes=5),
  102. uid=1,
  103. signal_payload=2)],
  104. targets=[objects.Target(
  105. ven_id='1234'
  106. )],
  107. signal_name=enums.SIGNAL_NAME.LOAD_CONTROL,
  108. signal_type=enums.SIGNAL_TYPE.LEVEL,
  109. signal_id=1,
  110. current_value=0
  111. )],
  112. targets=[{'ven_id': 'ven123'}],
  113. targets_by_type={'ven_id': ['ven123']}
  114. )
  115. msg = create_message('oadrDistributeEvent', events=[event])
  116. validate_xml_schema(ensure_bytes(msg))
  117. message_type, message_payload = parse_message(msg)
  118. def test_oadr_event_targets_and_targets_by_type_invalid():
  119. with pytest.raises(ValueError):
  120. event = objects.Event(
  121. event_descriptor=objects.EventDescriptor(
  122. event_id=1,
  123. modification_number=0,
  124. market_context='MarketContext1',
  125. event_status=enums.EVENT_STATUS.NEAR),
  126. active_period=objects.ActivePeriod(
  127. dtstart=datetime.now(),
  128. duration=timedelta(minutes=10)),
  129. event_signals=[objects.EventSignal(
  130. intervals=[
  131. objects.Interval(
  132. dtstart=datetime.now(),
  133. duration=timedelta(minutes=5),
  134. uid=0,
  135. signal_payload=1),
  136. objects.Interval(
  137. dtstart=datetime.now(),
  138. duration=timedelta(minutes=5),
  139. uid=1,
  140. signal_payload=2)],
  141. targets=[objects.Target(
  142. ven_id='1234'
  143. )],
  144. signal_name=enums.SIGNAL_NAME.LOAD_CONTROL,
  145. signal_type=enums.SIGNAL_TYPE.LEVEL,
  146. signal_id=1,
  147. current_value=0
  148. )],
  149. targets=[objects.Target(ven_id='ven456')],
  150. targets_by_type={'ven_id': ['ven123']}
  151. )
  152. msg = create_message('oadrDistributeEvent', events=[event])
  153. validate_xml_schema(ensure_bytes(msg))
  154. message_type, message_payload = parse_message(msg)
  155. def test_oadr_event_no_targets():
  156. with pytest.raises(ValueError):
  157. event = objects.Event(
  158. event_descriptor=objects.EventDescriptor(
  159. event_id=1,
  160. modification_number=0,
  161. market_context='MarketContext1',
  162. event_status=enums.EVENT_STATUS.NEAR),
  163. active_period=objects.ActivePeriod(
  164. dtstart=datetime.now(),
  165. duration=timedelta(minutes=10)),
  166. event_signals=[objects.EventSignal(
  167. intervals=[
  168. objects.Interval(
  169. dtstart=datetime.now(),
  170. duration=timedelta(minutes=5),
  171. uid=0,
  172. signal_payload=1),
  173. objects.Interval(
  174. dtstart=datetime.now(),
  175. duration=timedelta(minutes=5),
  176. uid=1,
  177. signal_payload=2)],
  178. targets=[objects.Target(
  179. ven_id='1234'
  180. )],
  181. signal_name=enums.SIGNAL_NAME.LOAD_CONTROL,
  182. signal_type=enums.SIGNAL_TYPE.LEVEL,
  183. signal_id=1,
  184. current_value=0
  185. )]
  186. )
  187. def test_event_signal_with_grouped_targets():
  188. event_signal = objects.EventSignal(intervals=[objects.Interval(dtstart=datetime.now(timezone.utc),
  189. duration=timedelta(minutes=10),
  190. signal_payload=1)],
  191. signal_name='simple',
  192. signal_type='level',
  193. signal_id='signal123',
  194. targets_by_type={'ven_id': ['ven123', 'ven456']})
  195. assert event_signal.targets == [objects.Target(ven_id='ven123'), objects.Target(ven_id='ven456')]
  196. def test_event_signal_with_incongruent_targets():
  197. with pytest.raises(ValueError):
  198. event_signal = objects.EventSignal(intervals=[objects.Interval(dtstart=datetime.now(timezone.utc),
  199. duration=timedelta(minutes=10),
  200. signal_payload=1)],
  201. signal_name='simple',
  202. signal_type='level',
  203. signal_id='signal123',
  204. targets=[objects.Target(ven_id='ven123')],
  205. targets_by_type={'ven_id': ['ven123', 'ven456']})
  206. def test_event_descriptor_modification_number():
  207. event_descriptor = objects.EventDescriptor(event_id='event123',
  208. modification_number=None,
  209. market_context='http://marketcontext01',
  210. event_status='near')
  211. assert event_descriptor.modification_number == 0