[tests][variables] fixed some loops and routes

This commit is contained in:
Maxime Alves LIRMM@home 2020-09-16 00:21:01 +02:00
commit e4b9b43ddd
4 changed files with 21 additions and 19 deletions

View file

@ -55,15 +55,15 @@ class Variable(RowClass):
@staticmethod @staticmethod
def getall_of_type(type: VariableType) -> dict: def getall_of_type(type: VariableType) -> dict:
return dict([ return {
(row['address'], Variable(**dict(row))) row['address']: Variable(**dict(row))
for row in sql( for row in sql(
"""SELECT * FROM variable """SELECT * FROM variable
WHERE type = '{}' WHERE type = '{}'
AND address >= {} AND address >= {}
AND address <= {}""".format( AND address <= {}""".format(
type, type.start_address, type.end_address)) type, type.start_address, type.end_address))
]) }
@staticmethod @staticmethod
@ -77,8 +77,8 @@ class Variable(RowClass):
cast_fct = floatcast if type.slabel == 'A' else lambda x: x cast_fct = floatcast if type.slabel == 'A' else lambda x: x
#offset = 10000 if (type.slabel == 'D') else 0 #offset = 10000 if (type.slabel == 'D') else 0
return dict([ return {
(row['address'], cast_fct(row['value'])) row['address']: cast_fct(row['value'])
for row in sql( for row in sql(
"""SELECT var.address as address, val.value """SELECT var.address as address, val.value
FROM variable var FROM variable var
@ -92,7 +92,7 @@ class Variable(RowClass):
AND var.address <= {} AND var.address <= {}
AND var.last_update > {}""".format( AND var.last_update > {}""".format(
type, type.start_address, type.end_address, since)) type, type.start_address, type.end_address, since))
]) }

View file

@ -106,9 +106,10 @@ class VariableType(RowClass):
@staticmethod @staticmethod
def getall(): def getall():
return dict([ return {
(row['label'], VariableType(**dict(row))) row['label']: VariableType(**dict(row))
for row in sql('SELECT * FROM var_type') ]) for row in sql('SELECT * FROM var_type')
}
@staticmethod @staticmethod
def get(slabel: str): def get(slabel: str):

View file

@ -14,15 +14,16 @@ from pyheatpump.models.variable import Variable
async def get_variables(request): async def get_variables(request):
return JSONResponse(dict([ def wo_type(d):
(key, dict( d.pop('type')
[ return d
(add, var.__dict__)
return JSONResponse({
key: {
add: wo_type(var.__dict__)
for add, var in var_type.items() for add, var in var_type.items()
]) } for key, var_type in Variable.getall().items()
) })
for key, var_type in Variable.getall().items()
]))
async def set_variables(request): async def set_variables(request):

View file

@ -35,8 +35,8 @@ def test_get_(set_test_db):
r = c.get('/') r = c.get('/')
assert r.status_code == 200 assert r.status_code == 200
d_resp = json.loads(r.content.decode()) d_resp = r.json()
pprint(d_resp) assert type(d_resp) is dict
assert 'A' in d_resp.keys() assert 'A' in d_resp.keys()
assert '10' in d_resp['A'].keys() assert '10' in d_resp['A'].keys()
assert len(d_resp['A'].keys()) == 4 assert len(d_resp['A'].keys()) == 4