Kaynağa Gözat

closes #21 - error in the select fields

Maxime Alves LIRMM@home 4 yıl önce
ebeveyn
işleme
975d9043d2
2 değiştirilmiş dosya ile 43 ekleme ve 3 silme
  1. 7
    3
      pyheatpump/models/variable.py
  2. 36
    0
      tests/models/test_variable.py

+ 7
- 3
pyheatpump/models/variable.py Dosyayı Görüntüle

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

+ 36
- 0
tests/models/test_variable.py Dosyayı Görüntüle

@@ -0,0 +1,36 @@
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…
İptal
Kaydet