Projet de remplacement du "RPiPasserelle" d'Otec.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_config.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. import pytest
  3. from starlette.authentication import UnauthenticatedUser
  4. from starlette.testclient import TestClient
  5. from pyheatpump.config import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES, mac_address_init, save_config
  6. from unittest.mock import patch, MagicMock
  7. from pprint import pprint
  8. import json
  9. from tempfile import mkstemp
  10. from configparser import ConfigParser
  11. import os
  12. @pytest.fixture
  13. def tmpconf(testdir):
  14. tmp_conf_filepath = os.path.join(testdir.tmpdir, 'pyheatpump.ini')
  15. config = ConfigParser(
  16. default_section='heatpump')
  17. config.read_dict(default_config)
  18. with open(tmp_conf_filepath, 'w') as f:
  19. f.write("[heatpump]\nmac_address = 'None'")
  20. CONFIG_FILES = [tmp_conf_filepath]
  21. config.read(filenames=CONFIG_FILES)
  22. return config
  23. def test_get_():
  24. c = TestClient(app)
  25. r = c.get('/')
  26. assert r.status_code == 200
  27. class RequestMock(MagicMock):
  28. def __get__(self, key):
  29. if key == 'method':
  30. return 'GET'
  31. @pytest.mark.asyncio
  32. async def test_get_config():
  33. resp = await get_config(RequestMock())
  34. assert resp.status_code == 200
  35. d_resp = json.loads(resp.body.decode())
  36. for sect in default_config.keys():
  37. assert sect in d_resp.keys()
  38. for item in default_config[sect].keys():
  39. assert item in d_resp[sect].keys()
  40. def test_set_config():
  41. c = TestClient(app)
  42. _, tmpconf = mkstemp()
  43. CONFIG_FILES.insert(0, tmpconf)
  44. r = c.post('/', json=json.dumps({
  45. 'supervisor': {
  46. 'scheme':'test'
  47. }
  48. }))
  49. config = ConfigParser()
  50. config.read(filenames=[tmpconf])
  51. assert r.status_code == 200
  52. assert config.get('supervisor', 'scheme') == 'test'
  53. os.unlink(tmpconf)
  54. def test_mac_address(testdir, tmpconf):
  55. try:
  56. assert tmpconf.get('heatpump', 'mac_address') is 'None'
  57. mac_address = mac_address_init()
  58. config_mac_address = tmpconf.get('heatpump', 'mac_address')
  59. assert config_mac_address == mac_address
  60. except Exception as e:
  61. raise e