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_value.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 datetime import datetime
  14. from pyheatpump.config import config
  15. from pyheatpump.db import initialize, connect
  16. from pyheatpump.variable_types import app, get_variable_types, set_variable_types, ROUTES
  17. from pyheatpump.models import *
  18. @pytest.fixture()
  19. def set_test_db():
  20. _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
  21. print(f'Will store database in {tmpdb}')
  22. config['heatpump']['database'] = tmpdb
  23. if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
  24. sys.exit(-1)
  25. if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
  26. sys.exit(-1)
  27. if not initialize(os.path.join(os.getcwd(), 'db/test_variable_values.sql')):
  28. sys.exit(-1)
  29. yield
  30. os.unlink(tmpdb)
  31. @pytest.fixture
  32. def var_types():
  33. return VariableType.getall()
  34. def test_last_update(set_test_db, var_types):
  35. for _, var_type in var_types.items():
  36. for _, variable in var_type.get_variables().items():
  37. try:
  38. if not variable.last_update:
  39. continue
  40. time = datetime.fromtimestamp(variable.last_update)
  41. val = VariableValue.get(variable.type,
  42. variable.address,
  43. time)
  44. assert val is not None
  45. assert variable.last_update == val.time
  46. except StopIteration:
  47. assert False
  48. def test_var_value_getval(set_test_db, var_types):
  49. var_type = var_types['Analog']
  50. for _, variable in var_type.get_variables().items():
  51. try:
  52. if not variable.last_update:
  53. continue
  54. time = datetime.fromtimestamp(variable.last_update)
  55. value = VariableValue.get(variable.type,
  56. variable.address,
  57. time).get_value()
  58. assert type(value) == float
  59. except StopIteration:
  60. assert False
  61. var_type = var_types['Integer']
  62. for _, variable in var_type.get_variables().items():
  63. try:
  64. if not variable.last_update:
  65. continue
  66. time = datetime.fromtimestamp(variable.last_update)
  67. value = VariableValue.get(variable.type,
  68. variable.address,
  69. time).get_value()
  70. assert type(value) == int
  71. except StopIteration:
  72. assert False
  73. var_type = var_types['Digital']
  74. for _, variable in var_type.get_variables().items():
  75. try:
  76. if not variable.last_update:
  77. continue
  78. time = datetime.fromtimestamp(variable.last_update)
  79. value = VariableValue.get(variable.type,
  80. variable.address,
  81. time).get_value()
  82. assert type(value) == bool
  83. except StopIteration:
  84. assert False