Projet de remplacement du "RPiPasserelle" d'Otec.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

test_config.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. @pytest.mark.skip
  49. def test_set_config(tmpconf):
  50. c = TestClient(app)
  51. _, tmpconf = mkstemp()
  52. CONFIG_FILES.append(tmpconf)
  53. rand = str(uuid4())
  54. r = c.post('/', json=json.dumps({
  55. 'supervisor': {
  56. 'scheme':rand
  57. }
  58. }))
  59. config = ConfigParser()
  60. config.read(filenames=[tmpconf])
  61. assert r.status_code == 200
  62. assert config.get('supervisor', 'scheme') == rand
  63. assert type(get_config_dict()) == dict
  64. d_config =get_config_dict()
  65. assert 'supervisor' in d_config.keys()
  66. assert 'scheme' in d_config['supervisor'].keys()
  67. assert d_config['supervisor']['scheme'] == rand
  68. os.unlink(tmpconf)
  69. def test_mac_address(testdir, tmpconf):
  70. c = TestClient(app)
  71. _, tmpconf = mkstemp()
  72. CONFIG_FILES.append(tmpconf)
  73. mac_address = mac_address_init()
  74. assert type(get_config_dict()) == dict
  75. d_config = get_config_dict()
  76. assert d_config['heatpump']['mac_address'] == mac_address
  77. resp = c.get('/mac')
  78. assert resp.status_code == 200
  79. mac_address = resp.content.decode()
  80. assert d_config['heatpump']['mac_address'] == mac_address
  81. @pytest.mark.skip
  82. def test_last_update(testdir, tmpconf):
  83. c = TestClient(app)
  84. resp = c.get('/last_update')
  85. last_update_0 = int(resp.content.decode())
  86. print(f'update0 {last_update_0}')
  87. resp = c.post('/last_update', str(datetime.now().strftime('%s')))
  88. last_update_1 = int(resp.content.decode())
  89. print(f'update1 {last_update_1}')
  90. assert last_update_0 == last_update_1