Browse Source

[tests] fit tests to new DB class

Maxime Alves LIRMM@home 3 years ago
parent
commit
9560e989f4

+ 4
- 4
tests/cli/test_fetch.py View File

@@ -6,7 +6,7 @@ from tempfile import mkstemp
6 6
 
7 7
 from pyheatpump.cli import cli
8 8
 from pyheatpump.config import config
9
-from pyheatpump.db import initialize, connect
9
+from pyheatpump.db import DB
10 10
 from pyheatpump.models import VariableValue
11 11
 
12 12
 @pytest.fixture(scope='module')
@@ -14,13 +14,13 @@ def set_test_db():
14 14
     _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
15 15
     print(f'Will store database in {tmpdb}')
16 16
     config['heatpump']['database'] = tmpdb
17
-    if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
17
+    if not DB.initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
18 18
         sys.exit(-1)
19 19
 
20
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
20
+    if not DB.initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
21 21
         sys.exit(-1)
22 22
 
23
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variable_values.sql')):
23
+    if not DB.initialize(os.path.join(os.getcwd(), 'db/test_variable_values.sql')):
24 24
         sys.exit(-1)
25 25
 
26 26
     yield

+ 2
- 2
tests/cli/test_supervise.py View File

@@ -7,7 +7,7 @@ import requests
7 7
 
8 8
 from pyheatpump.cli import cli
9 9
 from pyheatpump.config import config
10
-from pyheatpump.db import initialize, connect
10
+from pyheatpump.db import DB
11 11
 from pyheatpump.models import VariableValue
12 12
 
13 13
 @pytest.fixture(scope='module')
@@ -15,7 +15,7 @@ def set_test_db():
15 15
     _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
16 16
     print(f'Will store database in {tmpdb}')
17 17
     config['heatpump']['database'] = tmpdb
18
-    if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.test.sql')):
18
+    if not DB.initialize(os.path.join(os.getcwd(), 'db/pyheatpump.test.sql')):
19 19
         sys.exit(-1)
20 20
 
21 21
     yield

+ 30
- 0
tests/conftest.py View File

@@ -1 +1,31 @@
1
+import os
2
+import pytest
3
+import sys
4
+from tempfile import mkstemp
5
+
6
+from pyheatpump import DB
7
+from pyheatpump.models import *
8
+from pyheatpump.config import config
9
+
1 10
 pytest_plugins = "pytester"
11
+
12
+@pytest.fixture(scope="module")
13
+def set_test_db():
14
+    _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
15
+    print(f'Will store database in {tmpdb}')
16
+    config['heatpump']['database'] = tmpdb
17
+    assert DB.initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql'))
18
+
19
+    DB.sqlf(os.path.join(os.getcwd(), 'db/test_variables.sql'))
20
+
21
+    DB.sqlf(os.path.join(os.getcwd(), 'db/test_variable_values.sql'))
22
+
23
+    DB.sqlf(os.path.join(os.getcwd(), 'db/test_variable_values_wo_time.sql'))
24
+
25
+    yield
26
+
27
+
28
+
29
+@pytest.fixture
30
+def var_types():
31
+    return VariableType.getall()

+ 0
- 37
tests/models/test_variable_value.py View File

@@ -1,48 +1,11 @@
1 1
 #!/usr/bin/env python3
2 2
 import pytest
3
-from starlette.authentication import UnauthenticatedUser
4
-from starlette.testclient import TestClient
5
-#from pyheatpump.config import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES
6
-from unittest.mock import patch, MagicMock
7
-from pprint import pprint
8
-import json
9
-from tempfile import mkstemp
10
-from configparser import ConfigParser
11
-import os
12
-import sys
13 3
 from datetime import datetime
14 4
 
15
-from pyheatpump.config import config
16
-from pyheatpump.db import initialize, connect
17 5
 from pyheatpump.variable_types import app, get_variable_types, set_variable_types, ROUTES
18 6
 from pyheatpump.models import *
19 7
 
20 8
 
21
-@pytest.fixture()
22
-def set_test_db():
23
-    _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
24
-    print(f'Will store database in {tmpdb}')
25
-    config['heatpump']['database'] = tmpdb
26
-    if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
27
-        sys.exit(-1)
28
-
29
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
30
-        sys.exit(-1)
31
-
32
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variable_values.sql')):
33
-        sys.exit(-1)
34
-
35
-    yield
36
-
37
-
38
-    os.unlink(tmpdb)
39
-
40
-
41
-@pytest.fixture
42
-def var_types():
43
-    return VariableType.getall()
44
-
45
-
46 9
 def test_last_update(set_test_db, var_types):
47 10
     for _, var_type in var_types.items():
48 11
         for _, variable in var_type.get_variables().items():

+ 1
- 22
tests/test_heatpump.py View File

@@ -1,31 +1,10 @@
1 1
 #!/usr/bin/env python3
2 2
 import pytest
3
-from starlette.authentication import UnauthenticatedUser
4 3
 from starlette.testclient import TestClient
5
-from pyheatpump.config import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES
6
-from pyheatpump.db import initialize, connect
7
-from unittest.mock import patch, MagicMock
8
-from pprint import pprint
9 4
 import json
10
-from tempfile import mkstemp
11
-from configparser import ConfigParser
12
-import os
13 5
 
14
-@pytest.fixture(scope='module')
15
-def set_test_db():
16
-    _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
17
-    print(f'Will store database in {tmpdb}')
18
-    config['heatpump']['database'] = tmpdb
19
-    if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
20
-        sys.exit(-1)
6
+from pyheatpump.heatpump import app
21 7
 
22
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
23
-        sys.exit(-1)
24
-
25
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variable_values.sql')):
26
-        sys.exit(-1)
27
-
28
-    yield
29 8
 
30 9
 def test_get_(set_test_db):
31 10
     c = TestClient(app)

+ 0
- 36
tests/test_modbus.py View File

@@ -2,45 +2,12 @@
2 2
 import pytest
3 3
 from unittest.mock import patch, MagicMock
4 4
 
5
-from starlette.authentication import UnauthenticatedUser
6
-from starlette.testclient import TestClient
7
-from pprint import pprint
8
-import json
9
-from tempfile import mkstemp
10
-from configparser import ConfigParser
11
-import os
12
-
13 5
 from serial import Serial
14 6
 import umodbus
15 7
 from umodbus.client.serial import rtu
16 8
 
17
-
18
-from pyheatpump.config import config
19
-from pyheatpump.db import initialize
20
-from pyheatpump.models.variable_type import VariableType
21 9
 from pyheatpump import modbus
22 10
 
23
-@pytest.fixture(scope='module')
24
-def set_test_db():
25
-    _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
26
-    print(f'Will store database in {tmpdb}')
27
-    config['heatpump']['database'] = tmpdb
28
-    if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
29
-        sys.exit(-1)
30
-
31
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
32
-        sys.exit(-1)
33
-
34
-    yield
35
-
36
-    os.unlink(tmpdb)
37
-
38
-@pytest.fixture(scope='module')
39
-def serial_conn():
40
-    s = modbus.connect()
41
-    assert type(s) == Serial
42
-    return s
43
-
44 11
 
45 12
 @patch('umodbus.client.serial.rtu.send_message')
46 13
 @patch('umodbus.client.serial.rtu.read_coils')
@@ -82,9 +49,6 @@ def test_rtu_call_holding_registers(serial_conn):
82 49
     assert type(r) == list
83 50
     assert len(r) == 150
84 51
 
85
-@pytest.fixture
86
-def var_types():
87
-    return VariableType.getall()
88 52
 
89 53
 def test_get_analog(set_test_db, serial_conn, var_types):
90 54
     analog = var_types['Analog']

+ 1
- 25
tests/test_variable.py View File

@@ -1,34 +1,9 @@
1 1
 #!/usr/bin/env python3
2 2
 import pytest
3
-from starlette.authentication import UnauthenticatedUser
4 3
 from starlette.testclient import TestClient
5
-from unittest.mock import patch, MagicMock
6
-from pprint import pprint
7
-import json
8
-from tempfile import mkstemp
9
-from configparser import ConfigParser
10
-import os
11
-import sys
12 4
 
13
-from pyheatpump.config import config
14
-from pyheatpump.db import initialize, connect
15 5
 from pyheatpump.variables import app, get_variables, set_variables, ROUTES
16 6
 
17
-@pytest.fixture(scope='module')
18
-def set_test_db():
19
-    _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
20
-    print(f'Will store database in {tmpdb}')
21
-    config['heatpump']['database'] = tmpdb
22
-    if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
23
-        sys.exit(-1)
24
-
25
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
26
-        sys.exit(-1)
27
-
28
-    yield
29
-
30
-    os.unlink(tmpdb)
31
-
32 7
 
33 8
 def test_get_(set_test_db):
34 9
     c = TestClient(app)
@@ -38,6 +13,7 @@ def test_get_(set_test_db):
38 13
     d_resp = r.json()
39 14
     assert type(d_resp) is dict
40 15
     assert 'A' in d_resp.keys()
16
+    print(d_resp)
41 17
     assert '10' in d_resp['A'].keys()
42 18
     assert len(d_resp['A'].keys()) == 4
43 19
     assert len(d_resp['I'].keys()) == 4

+ 0
- 21
tests/test_variable_types.py View File

@@ -1,32 +1,11 @@
1 1
 #!/usr/bin/env python3
2 2
 import pytest
3
-from starlette.authentication import UnauthenticatedUser
4 3
 from starlette.testclient import TestClient
5
-#from pyheatpump.config import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES
6 4
 from unittest.mock import patch, MagicMock
7
-from pprint import pprint
8 5
 import json
9
-from tempfile import mkstemp
10
-from configparser import ConfigParser
11
-import os
12
-import sys
13 6
 
14
-from pyheatpump.config import config
15
-from pyheatpump.db import initialize, connect
16 7
 from pyheatpump.variable_types import app, get_variable_types, set_variable_types, ROUTES
17 8
 
18
-@pytest.fixture(scope='module')
19
-def set_test_db():
20
-    _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
21
-    print(f'Will store database in {tmpdb}')
22
-    config['heatpump']['database'] = tmpdb
23
-    if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
24
-        sys.exit(-1)
25
-
26
-    yield
27
-
28
-    os.unlink(tmpdb)
29
-
30 9
 
31 10
 def test_get_(set_test_db):
32 11
     c = TestClient(app)

+ 0
- 29
tests/test_variable_values.py View File

@@ -1,40 +1,11 @@
1 1
 #!/usr/bin/env python3
2 2
 import pytest
3
-from starlette.authentication import UnauthenticatedUser
4 3
 from starlette.exceptions import HTTPException 
5 4
 from starlette.testclient import TestClient
6
-from unittest.mock import patch, MagicMock
7
-from pprint import pprint
8
-import json
9
-from tempfile import mkstemp
10
-from configparser import ConfigParser
11
-import os
12
-import sys
13 5
 from datetime import datetime, timedelta
14 6
 
15
-from pyheatpump.config import config
16
-from pyheatpump.db import initialize, connect
17 7
 from pyheatpump.variables import app, get_variables, set_variables, ROUTES
18 8
 from pyheatpump.variable_values import app, get_variable_value, set_variable_value, ROUTES
19
-from pyheatpump.models.variable_value import VariableValue
20
-
21
-@pytest.fixture(scope='module')
22
-def set_test_db():
23
-    _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
24
-    print(f'Will store database in {tmpdb}')
25
-    config['heatpump']['database'] = tmpdb
26
-    if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
27
-        sys.exit(-1)
28
-
29
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variables.sql')):
30
-        sys.exit(-1)
31
-
32
-    if not initialize(os.path.join(os.getcwd(), 'db/test_variable_values.sql')):
33
-        sys.exit(-1)
34
-
35
-    yield
36
-
37
-    os.unlink(tmpdb)
38 9
 
39 10
 
40 11
 def test_get_type_address(set_test_db):

Loading…
Cancel
Save