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_variable_types.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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
  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. import sys
  13. from pyheatpump.config import config
  14. from pyheatpump.db import initialize, connect
  15. from pyheatpump.variable_types import app, get_variable_types, set_variable_types, ROUTES
  16. @pytest.fixture(scope='module')
  17. def set_test_db():
  18. _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
  19. print(f'Will store database in {tmpdb}')
  20. config['heatpump']['database'] = tmpdb
  21. if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
  22. sys.exit(-1)
  23. yield
  24. os.unlink(tmpdb)
  25. def test_get_(set_test_db):
  26. c = TestClient(app)
  27. r = c.get('/')
  28. assert r.status_code == 200
  29. class RequestMock(MagicMock):
  30. def __get__(self, key):
  31. if key == 'method':
  32. return 'GET'
  33. @pytest.mark.asyncio
  34. async def test_get_variable_types(set_test_db):
  35. resp = await get_variable_types(RequestMock())
  36. assert resp.status_code == 200
  37. d_resp = json.loads(resp.body.decode())
  38. assert 'Analog' in d_resp.keys()
  39. assert type(d_resp['Analog']) == dict
  40. assert 'Integer' in d_resp.keys()
  41. assert type(d_resp['Integer']) == dict
  42. assert 'Digital' in d_resp.keys()
  43. assert type(d_resp['Digital']) == dict
  44. @pytest.mark.skip
  45. def test_set_variable_types(set_test_db):
  46. c = TestClient(app)
  47. r = c.post('/', json=json.dumps({
  48. 'Analog': {
  49. 'start_address': 42,
  50. 'end_address': 420,
  51. }
  52. }))
  53. assert r.status_code == 200
  54. r = c.get('/')
  55. d_resp = json.loads(r.content.decode())
  56. assert d_resp['Analog']['start_address'] == 42
  57. assert d_resp['Analog']['end_address'] == 420