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

View file

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

View file

@ -14,15 +14,16 @@ from pyheatpump.models.variable import Variable
async def get_variables(request):
return JSONResponse(dict([
(key, dict(
[
(add, var.__dict__)
def wo_type(d):
d.pop('type')
return d
return JSONResponse({
key: {
add: wo_type(var.__dict__)
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):

View file

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