Browse Source

closes #21 - error in the select fields

Maxime Alves LIRMM@home 4 years ago
parent
commit
975d9043d2
2 changed files with 43 additions and 3 deletions
  1. 7
    3
      pyheatpump/models/variable.py
  2. 36
    0
      tests/models/test_variable.py

+ 7
- 3
pyheatpump/models/variable.py View File

22
     def insert(self):
22
     def insert(self):
23
 
23
 
24
         try:
24
         try:
25
+            self.type_slabel = self.type.slabel
26
+
25
             DB.sql(
27
             DB.sql(
26
             """INSERT INTO variable
28
             """INSERT INTO variable
27
             (type, address)
29
             (type, address)
28
             VALUES
30
             VALUES
29
-            (:type, :address)
31
+            (:type_slabel, :address)
30
             """, self.__dict__)
32
             """, self.__dict__)
31
             return True
33
             return True
32
         except Exception as e:
34
         except Exception as e:
36
 
38
 
37
     def exists(self):
39
     def exists(self):
38
         try:
40
         try:
41
+            self.type_slabel = self.type.slabel
42
+
39
             return bool(next(DB.sql(
43
             return bool(next(DB.sql(
40
             """SELECT 1 FROM variable
44
             """SELECT 1 FROM variable
41
-            WHERE type=:type AND address=:address
45
+            WHERE type=:type_slabel AND address=:address
42
             """, self.__dict__)))
46
             """, self.__dict__)))
43
         except StopIteration:
47
         except StopIteration:
44
             return False
48
             return False
78
         return {
82
         return {
79
             row['address']: cast_fct(row['value'])
83
             row['address']: cast_fct(row['value'])
80
             for row in DB.sql(
84
             for row in DB.sql(
81
-            """SELECT var.address as address, val.value
85
+            """SELECT var.address as address, val.value as value
82
             FROM variable var
86
             FROM variable var
83
             LEFT JOIN var_value val ON
87
             LEFT JOIN var_value val ON
84
                 var.type = val.type
88
                 var.type = val.type

+ 36
- 0
tests/models/test_variable.py View File

1
+#!/usr/bin/env python3
2
+import pytest
3
+from datetime import datetime
4
+from random import choice
5
+
6
+from pyheatpump.models.variable import Variable
7
+
8
+
9
+@pytest.fixture
10
+def rand_vars(set_test_db, var_types):
11
+    r = []
12
+    for _, var_type in var_types.items():
13
+        r.append( Variable(type=var_type,
14
+            address=choice(range(var_type.start_address, var_type.end_address))))
15
+    return r
16
+
17
+def test_insert(set_test_db, var_types, rand_vars):
18
+    for var in rand_vars:
19
+        var.insert()
20
+
21
+        assert var.exists()
22
+
23
+def test_getall_of_type(set_test_db, var_types):
24
+    for _, var_type in var_types.items():
25
+        assert isinstance(Variable.getall_of_type(var_type), dict)
26
+
27
+def test_getall(set_test_db):
28
+    assert isinstance(Variable.getall(), dict)
29
+
30
+def test_getall_values_of_type_since(set_test_db, var_types):
31
+    for _, var_type in var_types.items():
32
+        assert isinstance(Variable.getall_values_of_type_since(var_type, 0), dict)
33
+
34
+def test_getall_values_of_type(set_test_db, var_types):
35
+    for _, var_type in var_types.items():
36
+        assert isinstance(Variable.getall_values_of_type_since(var_type, 0), dict)

Loading…
Cancel
Save