test_client_registration.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from pyopenadr import OpenADRClient, OpenADRServer, enums
  2. from pyopenadr.utils import generate_id
  3. from pyopenadr.messaging import create_message, parse_message
  4. from datetime import datetime, timezone, timedelta
  5. import asyncio
  6. import sqlite3
  7. import pytest
  8. from aiohttp import web
  9. SERVER_PORT = 8001
  10. VEN_NAME = 'myven'
  11. VEN_ID = '1234abcd'
  12. VTN_ID = "TestVTN"
  13. async def _on_create_party_registration(payload):
  14. registration_id = generate_id()
  15. payload = {'response': {'response_code': 200,
  16. 'response_description': 'OK',
  17. 'request_id': payload['request_id']},
  18. 'ven_id': VEN_ID,
  19. 'registration_id': registration_id,
  20. 'profiles': [{'profile_name': '2.0b',
  21. 'transports': {'transport_name': 'simpleHttp'}}],
  22. 'requested_oadr_poll_freq': timedelta(seconds=10)}
  23. return 'oadrCreatedPartyRegistration', payload
  24. @pytest.fixture
  25. async def start_server():
  26. server = OpenADRServer(vtn_id=VTN_ID)
  27. server.add_handler('on_create_party_registration', _on_create_party_registration)
  28. runner = web.AppRunner(server.app)
  29. await runner.setup()
  30. site = web.TCPSite(runner, 'localhost', SERVER_PORT)
  31. await site.start()
  32. yield
  33. await runner.cleanup()
  34. @pytest.mark.asyncio
  35. async def test_query_party_registration(start_server):
  36. client = OpenADRClient(ven_name=VEN_NAME,
  37. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b")
  38. response_type, response_payload = await client.query_registration()
  39. assert response_type == 'oadrCreatedPartyRegistration'
  40. assert response_payload['vtn_id'] == VTN_ID
  41. @pytest.mark.asyncio
  42. async def test_create_party_registration(start_server):
  43. client = OpenADRClient(ven_name=VEN_NAME,
  44. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b")
  45. response_type, response_payload = await client.create_party_registration()
  46. assert response_type == 'oadrCreatedPartyRegistration'
  47. assert response_payload['ven_id'] == VEN_ID