[cli] added tests for **fetch**
This commit is contained in:
parent
d9e6686667
commit
a33196bd03
2 changed files with 74 additions and 12 deletions
|
|
@ -8,8 +8,11 @@ import re
|
|||
import importlib
|
||||
from pprint import pprint
|
||||
|
||||
from pyheatpump.logger import logger_init
|
||||
from pyheatpump.models import *
|
||||
|
||||
CONTEXT_SETTINGS={
|
||||
'default_map':{'run': {}}
|
||||
'default_map':{'run': {}}
|
||||
}
|
||||
|
||||
@click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
|
||||
|
|
@ -20,14 +23,13 @@ def cli(ctx, version):
|
|||
from pyheatpump import version
|
||||
return click.echo(pyheatpump.version())
|
||||
|
||||
if ctx.invoked_subcommand is None:
|
||||
return run()
|
||||
|
||||
|
||||
@click.option('--host', default=None)
|
||||
@click.option('--port', default=None)
|
||||
@cli.command()
|
||||
def run(host, port):
|
||||
logger = logger_init()
|
||||
|
||||
from pyheatpump.conf import (API_HOST, API_PORT)
|
||||
|
||||
if not host:
|
||||
|
|
@ -49,33 +51,50 @@ def run(host, port):
|
|||
|
||||
@cli.command()
|
||||
def fetch():
|
||||
from pyheatpump.models import *
|
||||
logger = logger_init()
|
||||
|
||||
from pyheatpump import modbus
|
||||
|
||||
|
||||
var_types = VariableType.getall()
|
||||
|
||||
# Analog - float
|
||||
analog = var_types['Analog']
|
||||
logger.info('Read analog variables in registers [{}, {}]'.format(
|
||||
analog.start_address, analog.end_address
|
||||
))
|
||||
res = modbus.read_holding_registers(analog.start_address, analog.end_address)
|
||||
|
||||
for r in range(len(res)):
|
||||
val = VariableValue(analog, r + analog.start_address, res[r])
|
||||
val = VariableValue(**{
|
||||
'type': analog,
|
||||
'address': r + analog.start_address,
|
||||
'value': res[r]})
|
||||
val.insert()
|
||||
|
||||
|
||||
# Integer - int
|
||||
integer = var_types['Integer']
|
||||
logger.info('Read integer variables in registers [{}, {}]'.format(
|
||||
integer.start_address, integer.end_address
|
||||
))
|
||||
res = modbus.read_holding_registers(integer.start_address, integer.end_address)
|
||||
|
||||
for r in res:
|
||||
val = VariableValue(integer, r + integer.start_address, res[r])
|
||||
for r in range(len(res)):
|
||||
val = VariableValue(**{
|
||||
'type': integer,
|
||||
'address': r + integer.start_address,
|
||||
'value': res[r]})
|
||||
val.insert()
|
||||
|
||||
# Digital - bool
|
||||
digital = var_types['Digital']
|
||||
logger.info('Read digital variables in coils [{}, {}]'.format(
|
||||
digital.start_address, digital.end_address
|
||||
))
|
||||
res = modbus.read_coils(digital.start_address, digital.end_address)
|
||||
|
||||
for r in res:
|
||||
val = VariableValue(integer), r + integer.start_address, res[r])
|
||||
for r in range(len(res)):
|
||||
val = VariableValue(**{
|
||||
'type': digital,
|
||||
'address': r + digital.start_address,
|
||||
'value': res[r]})
|
||||
val.insert()
|
||||
|
|
|
|||
43
tests/cli/test_fetch.py
Normal file
43
tests/cli/test_fetch.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import os
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
from unittest.mock import patch
|
||||
from tempfile import mkstemp
|
||||
|
||||
from pyheatpump.cli import cli
|
||||
from pyheatpump.conf import config
|
||||
from pyheatpump.db import initialize, connect
|
||||
from pyheatpump.models import VariableValue
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def set_test_db():
|
||||
_, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
|
||||
print(f'Will store database in {tmpdb}')
|
||||
config['heatpump']['database'] = tmpdb
|
||||
if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
|
||||
sys.exit(-1)
|
||||
|
||||
if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
|
||||
sys.exit(-1)
|
||||
|
||||
if not initialize(os.path.join(os.getcwd(), 'db/test_variable_values.sql')):
|
||||
sys.exit(-1)
|
||||
|
||||
yield
|
||||
|
||||
os.unlink(tmpdb)
|
||||
|
||||
@pytest.fixture
|
||||
def runner():
|
||||
return CliRunner()
|
||||
|
||||
@patch('pyheatpump.models.VariableValue.insert')
|
||||
def test_mock_variable_value(VariableValueInsert, set_test_db, runner):
|
||||
res = runner.invoke(cli, 'fetch')
|
||||
try:
|
||||
assert res.exit_code == 0
|
||||
VariableValueInsert.assert_called()
|
||||
|
||||
except Exception as e:
|
||||
print(res.output)
|
||||
raise e
|
||||
Loading…
Add table
Add a link
Reference in a new issue