closes #21 - error in the select fields
This commit is contained in:
parent
53bd4ebf07
commit
975d9043d2
2 changed files with 43 additions and 3 deletions
|
|
@ -22,11 +22,13 @@ class Variable(RowClass):
|
|||
def insert(self):
|
||||
|
||||
try:
|
||||
self.type_slabel = self.type.slabel
|
||||
|
||||
DB.sql(
|
||||
"""INSERT INTO variable
|
||||
(type, address)
|
||||
VALUES
|
||||
(:type, :address)
|
||||
(:type_slabel, :address)
|
||||
""", self.__dict__)
|
||||
return True
|
||||
except Exception as e:
|
||||
|
|
@ -36,9 +38,11 @@ class Variable(RowClass):
|
|||
|
||||
def exists(self):
|
||||
try:
|
||||
self.type_slabel = self.type.slabel
|
||||
|
||||
return bool(next(DB.sql(
|
||||
"""SELECT 1 FROM variable
|
||||
WHERE type=:type AND address=:address
|
||||
WHERE type=:type_slabel AND address=:address
|
||||
""", self.__dict__)))
|
||||
except StopIteration:
|
||||
return False
|
||||
|
|
@ -78,7 +82,7 @@ class Variable(RowClass):
|
|||
return {
|
||||
row['address']: cast_fct(row['value'])
|
||||
for row in DB.sql(
|
||||
"""SELECT var.address as address, val.value
|
||||
"""SELECT var.address as address, val.value as value
|
||||
FROM variable var
|
||||
LEFT JOIN var_value val ON
|
||||
var.type = val.type
|
||||
|
|
|
|||
36
tests/models/test_variable.py
Normal file
36
tests/models/test_variable.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env python3
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
from random import choice
|
||||
|
||||
from pyheatpump.models.variable import Variable
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def rand_vars(set_test_db, var_types):
|
||||
r = []
|
||||
for _, var_type in var_types.items():
|
||||
r.append( Variable(type=var_type,
|
||||
address=choice(range(var_type.start_address, var_type.end_address))))
|
||||
return r
|
||||
|
||||
def test_insert(set_test_db, var_types, rand_vars):
|
||||
for var in rand_vars:
|
||||
var.insert()
|
||||
|
||||
assert var.exists()
|
||||
|
||||
def test_getall_of_type(set_test_db, var_types):
|
||||
for _, var_type in var_types.items():
|
||||
assert isinstance(Variable.getall_of_type(var_type), dict)
|
||||
|
||||
def test_getall(set_test_db):
|
||||
assert isinstance(Variable.getall(), dict)
|
||||
|
||||
def test_getall_values_of_type_since(set_test_db, var_types):
|
||||
for _, var_type in var_types.items():
|
||||
assert isinstance(Variable.getall_values_of_type_since(var_type, 0), dict)
|
||||
|
||||
def test_getall_values_of_type(set_test_db, var_types):
|
||||
for _, var_type in var_types.items():
|
||||
assert isinstance(Variable.getall_values_of_type_since(var_type, 0), dict)
|
||||
Loading…
Add table
Add a link
Reference in a new issue