Projet de remplacement du "RPiPasserelle" d'Otec.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

test_config.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, \
  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. config.read_dict(default_config)
  24. with open(tmp_conf_filepath, 'w') as f:
  25. f.write("[heatpump]\nmac_address = 'None'")
  26. #CONFIG_FILES = [tmp_conf_filepath]
  27. config.read(filenames=CONFIG_FILES)
  28. return config
  29. def test_get_(tmpconf):
  30. c = TestClient(app)
  31. r = c.get('/')
  32. assert r.status_code == 200
  33. class RequestMock(MagicMock):
  34. def __get__(self, key):
  35. if key == 'method':
  36. return 'GET'
  37. def test_get_config(tmpconf):
  38. c = TestClient(app)
  39. resp = c.get('/')
  40. assert resp.status_code == 200
  41. d_resp = resp.json()
  42. assert type(d_resp) == dict
  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. def test_set_config(tmpconf):
  48. c = TestClient(app)
  49. _, tmpconf = mkstemp()
  50. CONFIG_FILES.append(tmpconf)
  51. rand = str(uuid4())
  52. r = c.post('/', json=json.dumps({
  53. 'supervisor': {
  54. 'scheme':rand
  55. }
  56. }))
  57. config = ConfigParser()
  58. config.read(filenames=[tmpconf])
  59. assert r.status_code == 200
  60. assert config.get('supervisor', 'scheme') == rand
  61. assert type(get_config_dict()) == dict
  62. d_config =get_config_dict()
  63. assert 'supervisor' in d_config.keys()
  64. assert 'scheme' in d_config['supervisor'].keys()
  65. assert d_config['supervisor']['scheme'] == rand
  66. os.unlink(tmpconf)
  67. def test_mac_address(testdir, tmpconf):
  68. c = TestClient(app)
  69. _, tmpconf = mkstemp()
  70. CONFIG_FILES.append(tmpconf)
  71. mac_address = mac_address_init()
  72. assert type(get_config_dict()) == dict
  73. d_config = get_config_dict()
  74. assert d_config['heatpump']['mac_address'] == mac_address
  75. resp = c.get('/mac')
  76. assert resp.status_code == 200
  77. mac_address = resp.content.decode()
  78. assert d_config['heatpump']['mac_address'] == mac_address
  79. def test_last_update(testdir, tmpconf):
  80. c = TestClient(app)
  81. resp = c.get('/last_update')
  82. last_update_0 = int(resp.content.decode())
  83. print(f'update0 {last_update_0}')
  84. resp = c.post('/last_update', str(datetime.now().strftime('%s')))
  85. last_update_1 = int(resp.content.decode())
  86. print(f'update1 {last_update_1}')
  87. assert last_update_0 == last_update_1