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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, \
  6. CONFIG_FILES, get_config, get_config_dict, set_config, config_route, ROUTES, \
  7. mac_address_init, save_config, get_last_update, set_last_update, \
  8. last_update_route
  9. from unittest.mock import patch, MagicMock
  10. from pprint import pprint
  11. import json
  12. from tempfile import mkstemp
  13. from configparser import ConfigParser
  14. import os
  15. from requests import Request
  16. from uuid import uuid4
  17. from datetime import datetime
  18. @pytest.fixture
  19. def tmpconf(testdir):
  20. tmp_conf_filepath = os.path.join(testdir.tmpdir, 'pyheatpump.ini')
  21. config = ConfigParser(
  22. default_section='heatpump')
  23. with open(tmp_conf_filepath, 'w') as f:
  24. f.write("[heatpump]\nmac_address = 'None'")
  25. #CONFIG_FILES = [tmp_conf_filepath]
  26. config.read(filenames=CONFIG_FILES)
  27. return config
  28. def test_get_(tmpconf):
  29. c = TestClient(app)
  30. r = c.get('/')
  31. assert r.status_code == 200
  32. class RequestMock(MagicMock):
  33. def __get__(self, key):
  34. if key == 'method':
  35. return 'GET'
  36. def test_get_config(tmpconf):
  37. c = TestClient(app)
  38. resp = c.get('/')
  39. assert resp.status_code == 200
  40. d_resp = resp.json()
  41. assert type(d_resp) == dict
  42. """
  43. for sect in default_config.keys():
  44. assert sect in d_resp.keys()
  45. for item in default_config[sect].keys():
  46. assert item in d_resp[sect].keys()
  47. """
  48. def test_set_config(tmpconf):
  49. c = TestClient(app)
  50. _, tmpconf = mkstemp()
  51. CONFIG_FILES.append(tmpconf)
  52. rand = str(uuid4())
  53. r = c.post('/', json=json.dumps({
  54. 'supervisor': {
  55. 'scheme':rand
  56. }
  57. }))
  58. config = ConfigParser()
  59. config.read(filenames=[tmpconf])
  60. assert r.status_code == 200
  61. assert config.get('supervisor', 'scheme') == rand
  62. assert type(get_config_dict()) == dict
  63. d_config =get_config_dict()
  64. assert 'supervisor' in d_config.keys()
  65. assert 'scheme' in d_config['supervisor'].keys()
  66. assert d_config['supervisor']['scheme'] == rand
  67. os.unlink(tmpconf)
  68. def test_mac_address(testdir, tmpconf):
  69. c = TestClient(app)
  70. _, tmpconf = mkstemp()
  71. CONFIG_FILES.append(tmpconf)
  72. mac_address = mac_address_init()
  73. assert type(get_config_dict()) == dict
  74. d_config = get_config_dict()
  75. assert d_config['heatpump']['mac_address'] == mac_address
  76. resp = c.get('/mac')
  77. assert resp.status_code == 200
  78. mac_address = resp.content.decode()
  79. assert d_config['heatpump']['mac_address'] == mac_address
  80. def test_last_update(testdir, tmpconf):
  81. c = TestClient(app)
  82. resp = c.get('/last_update')
  83. last_update_0 = int(resp.content.decode())
  84. print(f'update0 {last_update_0}')
  85. resp = c.post('/last_update', str(datetime.now().strftime('%s')))
  86. last_update_1 = int(resp.content.decode())
  87. print(f'update1 {last_update_1}')
  88. assert last_update_0 == last_update_1