|
@@ -0,0 +1,60 @@
|
|
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
|
+
|
|
8
|
+from pyheatpump.cli import cli
|
|
9
|
+from pyheatpump.config import config
|
|
10
|
+from pyheatpump.db import initialize, connect
|
|
11
|
+from pyheatpump.models import VariableValue
|
|
12
|
+
|
|
13
|
+@pytest.fixture(scope='module')
|
|
14
|
+def set_test_db():
|
|
15
|
+ _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
|
|
16
|
+ print(f'Will store database in {tmpdb}')
|
|
17
|
+ config['heatpump']['database'] = tmpdb
|
|
18
|
+ if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.test.sql')):
|
|
19
|
+ sys.exit(-1)
|
|
20
|
+
|
|
21
|
+ yield
|
|
22
|
+
|
|
23
|
+ os.unlink(tmpdb)
|
|
24
|
+
|
|
25
|
+@pytest.fixture
|
|
26
|
+def runner():
|
|
27
|
+ return CliRunner()
|
|
28
|
+
|
|
29
|
+@pytest.fixture
|
|
30
|
+def fetch(set_test_db, runner):
|
|
31
|
+ try:
|
|
32
|
+ runner.invoke(cli, 'fetch')
|
|
33
|
+ except ValueError:
|
|
34
|
+ pass
|
|
35
|
+
|
|
36
|
+@patch('pyheatpump.cli.requests')
|
|
37
|
+def test_supervise(requestsMock, fetch, runner):
|
|
38
|
+ config['supervisor']['scheme'] = 'http'
|
|
39
|
+ config['supervisor']['host'] = '127.0.0.1'
|
|
40
|
+ config['supervisor']['port'] = '8080'
|
|
41
|
+ config['supervisor']['post_path'] = '/post'
|
|
42
|
+ config['supervisor']['get_path'] = '/get'
|
|
43
|
+ config['heatpump']['mac_address'] = '00:11:22:33:44:55'
|
|
44
|
+
|
|
45
|
+ data = {'macAddress':config['heatpump']['mac_address']}
|
|
46
|
+
|
|
47
|
+ try:
|
|
48
|
+ r = runner.invoke(cli, 'supervise')
|
|
49
|
+ except ValueError:
|
|
50
|
+ pass
|
|
51
|
+
|
|
52
|
+ requestsMock.post.assert_called()
|
|
53
|
+ requestsMock.post.assert_called_with(
|
|
54
|
+ url='http://127.0.0.1:8080/post',
|
|
55
|
+ json=data
|
|
56
|
+ )
|
|
57
|
+
|
|
58
|
+ requestsMock.get.assert_called()
|
|
59
|
+ requestsMock.get.assert_called_with(
|
|
60
|
+ url=''.join('http://127.0.0.1:8080/get/001122334455'))
|