test_conformance_006.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. @pytest.mark.asyncio
  8. async def test_conformance_006():
  9. """
  10. The presence of any string except “false” in the oadrDistributeEvent
  11. testEvent element MUST be treated as a trigger for a test event.
  12. """
  13. # Monkey patch our own formatter to prevent an error being raised
  14. from pyopenadr.messaging import TEMPLATES
  15. def booleanformat_monkey(value):
  16. """
  17. Format a boolean value
  18. """
  19. if isinstance(value, bool):
  20. if value == True:
  21. return "true"
  22. elif value == False:
  23. return "false"
  24. else:
  25. return value
  26. booleanformat_original = TEMPLATES.filters['booleanformat']
  27. TEMPLATES.filters['booleanformat'] = booleanformat_monkey
  28. event_id = generate_id()
  29. event = {'event_descriptor':
  30. {'event_id': event_id,
  31. 'modification_number': 0,
  32. 'modification_date': datetime.now(),
  33. 'priority': 0,
  34. 'market_context': 'MarketContext001',
  35. 'created_date_time': datetime.now(),
  36. 'event_status': enums.EVENT_STATUS.FAR,
  37. 'test_event': "HelloThere",
  38. 'vtn_comment': 'No Comment'},
  39. 'active_period':
  40. {'dtstart': datetime.now(),
  41. 'duration': timedelta(minutes=30)},
  42. 'event_signals':
  43. [{'intervals': [{'duration': timedelta(minutes=10),
  44. 'signal_payload': 1},
  45. {'duration': timedelta(minutes=10),
  46. 'signal_payload': 2},
  47. {'duration': timedelta(minutes=10),
  48. 'signal_payload': 3}],
  49. 'signal_name': enums.SIGNAL_NAME.SIMPLE,
  50. 'signal_type': enums.SIGNAL_TYPE.DELTA,
  51. 'signal_id': generate_id()
  52. }]
  53. }
  54. # Create a message with this event
  55. msg = create_message('oadrDistributeEvent',
  56. response={'response_code': 200,
  57. 'response_description': 'OK',
  58. 'request_id': generate_id()},
  59. request_id=generate_id(),
  60. vtn_id=generate_id(),
  61. events=[event])
  62. parsed_type, parsed_message = parse_message(msg)
  63. assert parsed_type == 'oadrDistributeEvent'
  64. assert parsed_message['events'][0]['event_descriptor']['test_event'] == True
  65. # Restore the original booleanformat function
  66. TEMPLATES.filters['booleanformat'] = booleanformat_original