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_supervise.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import os
  2. import pytest
  3. from click.testing import CliRunner
  4. from unittest.mock import patch
  5. from tempfile import mkstemp
  6. import requests
  7. from pyheatpump.cli import cli
  8. from pyheatpump.config import config
  9. from pyheatpump.db import DB
  10. from pyheatpump.models import VariableValue
  11. @pytest.fixture(scope='module')
  12. def set_test_db():
  13. _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
  14. print(f'Will store database in {tmpdb}')
  15. config['heatpump']['database'] = tmpdb
  16. if not DB.initialize(os.path.join(os.getcwd(), 'db/pyheatpump.test.sql')):
  17. sys.exit(-1)
  18. yield
  19. os.unlink(tmpdb)
  20. @pytest.fixture
  21. def runner():
  22. return CliRunner()
  23. @pytest.fixture
  24. def fetch(set_test_db, runner):
  25. try:
  26. runner.invoke(cli, 'fetch')
  27. except ValueError:
  28. pass
  29. @pytest.fixture
  30. def default_config():
  31. config['supervisor']['scheme'] = 'http'
  32. config['supervisor']['host'] = '127.0.0.1'
  33. config['supervisor']['port'] = '8080'
  34. config['supervisor']['post_path'] = '/post'
  35. config['supervisor']['get_path'] = '/get'
  36. config['heatpump']['mac_address'] = '00:11:22:33:44:55'
  37. @pytest.mark.skip
  38. @patch('pyheatpump.cli.requests')
  39. def test_supervise(requestsMock, fetch, runner, default_config):
  40. data = {'macAddress':'001122334455'}
  41. data_analog = { **data, **{'Analog':{} } }
  42. data_integer = { **data, **{'Integer':{} } }
  43. data_digital = { **data, **{'Digital':{} } }
  44. try:
  45. r = runner.invoke(cli, 'supervise')
  46. except ValueError:
  47. pass
  48. requestsMock.post.assert_called()
  49. requestsMock.post.assert_called_with(
  50. url='http://127.0.0.1:8080/post',
  51. json=data_integer,
  52. verify=False
  53. )
  54. requestsMock.get.assert_called()
  55. requestsMock.get.assert_called_with(
  56. url=''.join('http://127.0.0.1:8080/get/001122334455'),
  57. verify=False)
  58. @patch('pyheatpump.cli.requests')
  59. def test_supervise_values(requestsMock, fetch, runner, default_config, set_db_values):
  60. data = {'macAddress':'001122334455'}
  61. data_analog = { **data, **{'Analog':{} } }
  62. data_integer = { **data, **{'Integer':{} } }
  63. data_digital = { **data, **{'Digital':{} } }
  64. try:
  65. r = runner.invoke(cli, 'supervise')
  66. except ValueError:
  67. pass
  68. requestsMock.post.assert_called()
  69. requestsMock.post.assert_called_with(
  70. url='http://127.0.0.1:8080/post',
  71. json=data_integer,
  72. verify=False
  73. )
  74. requestsMock.get.assert_called()
  75. requestsMock.get.assert_called_with(
  76. url=''.join('http://127.0.0.1:8080/get/001122334455'),
  77. verify=False)