Browse Source

[cli] added tests for **fetch**

Maxime Alves LIRMM@home 3 years ago
parent
commit
a33196bd03
2 changed files with 74 additions and 12 deletions
  1. 31
    12
      pyheatpump/cli.py
  2. 43
    0
      tests/cli/test_fetch.py

+ 31
- 12
pyheatpump/cli.py View File

@@ -8,8 +8,11 @@ import re
8 8
 import importlib
9 9
 from pprint import pprint
10 10
 
11
+from pyheatpump.logger import logger_init
12
+from pyheatpump.models import *
13
+
11 14
 CONTEXT_SETTINGS={
12
-    'default_map':{'run': {}} 
15
+    'default_map':{'run': {}}
13 16
 }
14 17
 
15 18
 @click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
@@ -20,14 +23,13 @@ def cli(ctx, version):
20 23
         from pyheatpump import version
21 24
         return click.echo(pyheatpump.version())
22 25
 
23
-    if ctx.invoked_subcommand is None: 
24
-        return run()
25
-
26 26
 
27 27
 @click.option('--host', default=None)
28 28
 @click.option('--port', default=None)
29 29
 @cli.command()
30 30
 def run(host, port):
31
+    logger = logger_init()
32
+
31 33
     from pyheatpump.conf import (API_HOST, API_PORT)
32 34
 
33 35
     if not host:
@@ -49,33 +51,50 @@ def run(host, port):
49 51
 
50 52
 @cli.command()
51 53
 def fetch():
52
-    from pyheatpump.models import *
53
-    from pyheatpump import modbus
54
-
54
+    logger = logger_init()
55 55
 
56
+    from pyheatpump import modbus
56 57
     var_types = VariableType.getall()
57 58
 
58 59
     # Analog - float
59 60
     analog = var_types['Analog']
61
+    logger.info('Read analog variables in registers [{}, {}]'.format(
62
+        analog.start_address, analog.end_address
63
+    ))
60 64
     res = modbus.read_holding_registers(analog.start_address, analog.end_address)
61 65
 
62 66
     for r in range(len(res)):
63
-        val = VariableValue(analog, r + analog.start_address, res[r])
67
+        val = VariableValue(**{
68
+            'type': analog,
69
+            'address': r + analog.start_address,
70
+            'value': res[r]})
64 71
         val.insert()
65 72
 
66 73
 
67 74
     # Integer - int
68 75
     integer = var_types['Integer']
76
+    logger.info('Read integer variables in registers [{}, {}]'.format(
77
+        integer.start_address, integer.end_address
78
+    ))
69 79
     res = modbus.read_holding_registers(integer.start_address, integer.end_address)
70 80
 
71
-    for r in res:
72
-        val = VariableValue(integer, r + integer.start_address, res[r])
81
+    for r in range(len(res)):
82
+        val = VariableValue(**{
83
+            'type': integer,
84
+            'address': r + integer.start_address,
85
+            'value': res[r]})
73 86
         val.insert()
74 87
 
75 88
     # Digital - bool
76 89
     digital = var_types['Digital']
90
+    logger.info('Read digital variables in coils [{}, {}]'.format(
91
+        digital.start_address, digital.end_address
92
+    ))
77 93
     res = modbus.read_coils(digital.start_address, digital.end_address)
78 94
 
79
-    for r in res:
80
-        val = VariableValue(integer), r + integer.start_address, res[r])
95
+    for r in range(len(res)):
96
+        val = VariableValue(**{
97
+            'type': digital,
98
+            'address': r + digital.start_address,
99
+            'value': res[r]})
81 100
         val.insert()

+ 43
- 0
tests/cli/test_fetch.py View File

@@ -0,0 +1,43 @@
1
+import os
2
+import pytest
3
+from click.testing import CliRunner
4
+from unittest.mock import patch
5
+from tempfile import mkstemp
6
+
7
+from pyheatpump.cli import cli
8
+from pyheatpump.conf import config
9
+from pyheatpump.db import initialize, connect
10
+from pyheatpump.models import VariableValue
11
+
12
+@pytest.fixture(scope='module')
13
+def set_test_db():
14
+    _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
15
+    print(f'Will store database in {tmpdb}')
16
+    config['heatpump']['database'] = tmpdb
17
+    if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
18
+        sys.exit(-1)
19
+
20
+    if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
21
+        sys.exit(-1)
22
+
23
+    if not initialize(os.path.join(os.getcwd(), 'db/test_variable_values.sql')):
24
+        sys.exit(-1)
25
+
26
+    yield
27
+
28
+    os.unlink(tmpdb)
29
+
30
+@pytest.fixture
31
+def runner():
32
+    return CliRunner()
33
+
34
+@patch('pyheatpump.models.VariableValue.insert')
35
+def test_mock_variable_value(VariableValueInsert, set_test_db, runner):
36
+    res = runner.invoke(cli, 'fetch')
37
+    try:
38
+        assert res.exit_code == 0
39
+        VariableValueInsert.assert_called()
40
+
41
+    except Exception as e:
42
+        print(res.output)
43
+        raise e

Loading…
Cancel
Save