test_poll_responses.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from openleadr import OpenADRClient, OpenADRServer, objects, utils
  2. from functools import partial
  3. from dataclasses import asdict
  4. from datetime import datetime, timezone, timedelta
  5. import pytest
  6. def on_create_party_registration(registration_info):
  7. return 'ven123', 'reg123'
  8. def poll_responder(ven_id, message_type, message_payload):
  9. return message_type, message_payload
  10. event = objects.Event(event_descriptor=objects.EventDescriptor(event_id='event123',
  11. event_status='far',
  12. modification_number='1',
  13. market_context='http://marketcontext01'),
  14. event_signals=[objects.EventSignal(signal_name='simple',
  15. signal_type='level',
  16. signal_id=utils.generate_id(),
  17. intervals=[objects.Interval(dtstart=datetime.now(timezone.utc),
  18. duration=timedelta(minutes=10),
  19. signal_payload=1)])],
  20. targets=[{'ven_id': 'ven123'}])
  21. poll_responses = [('oadrResponse', {}),
  22. ('oadrDistributeEvent', {'events': [asdict(event)]}),
  23. ('oadrCreateReport', {'report_requests': [{'report_request_id': 'req123',
  24. 'report_specifier': {'report_specifier_id': 'rsi123',
  25. 'granularity': timedelta(seconds=10),
  26. 'report_back_duration': timedelta(seconds=10),
  27. 'specifier_payloads': [{'r_id': 'rid123',
  28. 'reading_type': 'Direct Read'}]}}]}),
  29. ('oadrCancelReport', {'report_request_id': 'report123',
  30. 'report_to_follow': False,
  31. 'request_id': 'request123'}),
  32. ('oadrRegisterReport', {'ven_id': 'ven123', 'reports': []}),
  33. ('oadrUpdateReport', {'ven_id': 'ven123'}),
  34. ('oadrCancelPartyRegistration', {'registration_id': 'reg123',
  35. 'ven_id': 'ven123'}),
  36. ('oadrRequestReregistration', {'ven_id': 'ven123'})]
  37. @pytest.mark.parametrize('message_type,message_payload', poll_responses)
  38. @pytest.mark.asyncio
  39. async def test_message(message_type, message_payload):
  40. server = OpenADRServer(vtn_id='myvtn')
  41. server.add_handler('on_create_party_registration', on_create_party_registration)
  42. server.add_handler('on_poll', partial(poll_responder, message_type=message_type, message_payload=message_payload))
  43. client = OpenADRClient(ven_name='myven',
  44. vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b')
  45. await server.run_async()
  46. await client.create_party_registration()
  47. response_type, response_payload = await client.poll()
  48. await server.stop()
  49. assert response_type == message_type