test_client_misc.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import pytest
  2. from openleadr import OpenADRClient
  3. from openleadr import enums
  4. def test_trailing_slash_on_vtn_url():
  5. client = OpenADRClient(ven_name='myven', vtn_url='http://localhost/')
  6. assert client.vtn_url == 'http://localhost'
  7. def test_wrong_handler_supplied(caplog):
  8. client = OpenADRClient(ven_name='myven', vtn_url='http://localhost')
  9. client.add_handler('non_existant', print)
  10. assert ("'handler' must be either on_event or on_update_event") in [rec.message for rec in caplog.records]
  11. def test_invalid_report_name(caplog):
  12. client = OpenADRClient(ven_name='myven', vtn_url='http://localhost')
  13. with pytest.raises(ValueError):
  14. client.add_report(callback=print,
  15. resource_id='myresource',
  16. measurement='voltage',
  17. report_name='non_existant')
  18. # assert (f"non_existant is not a valid report_name. Valid options are "
  19. # f"{', '.join(enums.REPORT_NAME.values)}",
  20. # " or any name starting with 'x-'.") in [rec.message for rec in caplog.records]
  21. def test_invalid_reading_type():
  22. client = OpenADRClient(ven_name='myven', vtn_url='http://localhost')
  23. with pytest.raises(ValueError):
  24. client.add_report(callback=print,
  25. resource_id='myresource',
  26. measurement='voltage',
  27. reading_type='non_existant')
  28. # assert (f"non_existant is not a valid reading_type. Valid options are "
  29. # f"{', '.join(enums.READING_TYPE.values)}",
  30. # " or any name starting with 'x-'.") in [rec.message for rec in caplog.records]
  31. def test_invalid_report_type():
  32. client = OpenADRClient(ven_name='myven', vtn_url='http://localhost')
  33. with pytest.raises(ValueError):
  34. client.add_report(callback=print,
  35. resource_id='myresource',
  36. measurement='voltage',
  37. report_type='non_existant')
  38. # assert (f"non_existant is not a valid report_type. Valid options are "
  39. # f"{', '.join(enums.REPORT_TYPE.values)}",
  40. # " or any name starting with 'x-'.") in [rec.message for rec in caplog.records]
  41. def test_invalid_data_collection_mode():
  42. client = OpenADRClient(ven_name='myven', vtn_url='http://localhost')
  43. with pytest.raises(ValueError):
  44. client.add_report(callback=print,
  45. resource_id='myresource',
  46. measurement='voltage',
  47. data_collection_mode='non_existant')
  48. # assert ("The data_collection_mode should be 'incremental' or 'full'.") in [rec.message for rec in caplog.records]
  49. def test_invalid_scale():
  50. client = OpenADRClient(ven_name='myven', vtn_url='http://localhost')
  51. with pytest.raises(ValueError):
  52. client.add_report(callback=print,
  53. resource_id='myresource',
  54. measurement='voltage',
  55. scale='non_existant')
  56. def test_add_report_without_specifier_id():
  57. client = OpenADRClient(ven_name='myven', vtn_url='http://localhost')
  58. client.add_report(callback=print,
  59. resource_id='myresource1',
  60. measurement='voltage')
  61. client.add_report(callback=print,
  62. resource_id='myresource2',
  63. measurement='voltage')
  64. assert len(client.reports) == 1
  65. async def wrong_sig(param1):
  66. pass
  67. def test_add_report_with_invalid_callback_signature():
  68. client = OpenADRClient(ven_name='myven', vtn_url='http://localhost')
  69. with pytest.raises(TypeError):
  70. client.add_report(callback=wrong_sig,
  71. data_collection_mode='full',
  72. resource_id='myresource1',
  73. measurement='voltage')