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_values.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. import pytest
  3. from starlette.authentication import UnauthenticatedUser
  4. from starlette.exceptions import HTTPException
  5. from starlette.testclient import TestClient
  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 datetime import datetime, timedelta
  14. from pyheatpump.conf import config
  15. from pyheatpump.db import initialize, connect
  16. from pyheatpump.variables import app, get_variables, set_variables, ROUTES
  17. from pyheatpump.variable_values import app, get_variable_value, set_variable_value, ROUTES
  18. from pyheatpump.models.variable_value import VariableValue
  19. @pytest.fixture(scope='module')
  20. def set_test_db():
  21. _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
  22. print(f'Will store database in {tmpdb}')
  23. config['heatpump']['database'] = tmpdb
  24. if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
  25. sys.exit(-1)
  26. if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
  27. sys.exit(-1)
  28. if not initialize(os.path.join(os.getcwd(), 'db/test_variable_values.sql')):
  29. sys.exit(-1)
  30. yield
  31. os.unlink(tmpdb)
  32. def test_get_type_address(set_test_db):
  33. c = TestClient(app)
  34. r = c.get('/A/10')
  35. assert r.status_code == 200
  36. d_resp = r.content.decode()
  37. assert int(d_resp) == 42
  38. try:
  39. r = c.get('/X/10')
  40. except HTTPException as e:
  41. assert e.status_code == 404
  42. r = c.get('/A/10/{}'.format(
  43. (datetime.now() - timedelta(days=1)).isoformat()
  44. ))
  45. d_resp = r.content.decode()
  46. assert int(d_resp) == 20
  47. r = c.get('/A/10/{}'.format(
  48. (datetime.now() - timedelta(days=2)).isoformat()
  49. ))
  50. d_resp = r.content.decode()
  51. assert int(d_resp) == 0
  52. r = c.get('/I/5010')
  53. d_resp = r.content.decode()
  54. assert int(d_resp) == 42
  55. r = c.get('/I/5011')
  56. d_resp = r.content.decode()
  57. assert int(d_resp) == 24
  58. r = c.get('/I/5010/{}'.format(
  59. (datetime.now() - timedelta(hours=1)).isoformat()
  60. ))
  61. d_resp = r.content.decode()
  62. assert int(d_resp) == 20
  63. r = c.get('/I/5010/{}'.format(
  64. (datetime.now() - timedelta(hours=2)).isoformat()
  65. ))
  66. d_resp = r.content.decode()
  67. assert int(d_resp) == 0
  68. @pytest.mark.skip
  69. def test_set_(set_test_db):
  70. c = TestClient(app)
  71. r = c.post('/')
  72. assert r.status_code == 200