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
 
6
 
7
 from pyheatpump.cli import cli
7
 from pyheatpump.cli import cli
8
 from pyheatpump.config import config
8
 from pyheatpump.config import config
9
-from pyheatpump.db import initialize, connect
9
+from pyheatpump.db import DB
10
 from pyheatpump.models import VariableValue
10
 from pyheatpump.models import VariableValue
11
 
11
 
12
 @pytest.fixture(scope='module')
12
 @pytest.fixture(scope='module')
14
     _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
14
     _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
15
     print(f'Will store database in {tmpdb}')
15
     print(f'Will store database in {tmpdb}')
16
     config['heatpump']['database'] = tmpdb
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
         sys.exit(-1)
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
         sys.exit(-1)
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
         sys.exit(-1)
24
         sys.exit(-1)
25
 
25
 
26
     yield
26
     yield

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

7
 
7
 
8
 from pyheatpump.cli import cli
8
 from pyheatpump.cli import cli
9
 from pyheatpump.config import config
9
 from pyheatpump.config import config
10
-from pyheatpump.db import initialize, connect
10
+from pyheatpump.db import DB
11
 from pyheatpump.models import VariableValue
11
 from pyheatpump.models import VariableValue
12
 
12
 
13
 @pytest.fixture(scope='module')
13
 @pytest.fixture(scope='module')
15
     _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
15
     _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
16
     print(f'Will store database in {tmpdb}')
16
     print(f'Will store database in {tmpdb}')
17
     config['heatpump']['database'] = tmpdb
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
         sys.exit(-1)
19
         sys.exit(-1)
20
 
20
 
21
     yield
21
     yield

+ 30
- 0
tests/conftest.py View File

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
 pytest_plugins = "pytester"
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
 #!/usr/bin/env python3
1
 #!/usr/bin/env python3
2
 import pytest
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
 from datetime import datetime
3
 from datetime import datetime
14
 
4
 
15
-from pyheatpump.config import config
16
-from pyheatpump.db import initialize, connect
17
 from pyheatpump.variable_types import app, get_variable_types, set_variable_types, ROUTES
5
 from pyheatpump.variable_types import app, get_variable_types, set_variable_types, ROUTES
18
 from pyheatpump.models import *
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
 def test_last_update(set_test_db, var_types):
9
 def test_last_update(set_test_db, var_types):
47
     for _, var_type in var_types.items():
10
     for _, var_type in var_types.items():
48
         for _, variable in var_type.get_variables().items():
11
         for _, variable in var_type.get_variables().items():

+ 1
- 22
tests/test_heatpump.py View File

1
 #!/usr/bin/env python3
1
 #!/usr/bin/env python3
2
 import pytest
2
 import pytest
3
-from starlette.authentication import UnauthenticatedUser
4
 from starlette.testclient import TestClient
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
 import json
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
 def test_get_(set_test_db):
9
 def test_get_(set_test_db):
31
     c = TestClient(app)
10
     c = TestClient(app)

+ 0
- 36
tests/test_modbus.py View File

2
 import pytest
2
 import pytest
3
 from unittest.mock import patch, MagicMock
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
 from serial import Serial
5
 from serial import Serial
14
 import umodbus
6
 import umodbus
15
 from umodbus.client.serial import rtu
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
 from pyheatpump import modbus
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
 @patch('umodbus.client.serial.rtu.send_message')
12
 @patch('umodbus.client.serial.rtu.send_message')
46
 @patch('umodbus.client.serial.rtu.read_coils')
13
 @patch('umodbus.client.serial.rtu.read_coils')
82
     assert type(r) == list
49
     assert type(r) == list
83
     assert len(r) == 150
50
     assert len(r) == 150
84
 
51
 
85
-@pytest.fixture
86
-def var_types():
87
-    return VariableType.getall()
88
 
52
 
89
 def test_get_analog(set_test_db, serial_conn, var_types):
53
 def test_get_analog(set_test_db, serial_conn, var_types):
90
     analog = var_types['Analog']
54
     analog = var_types['Analog']

+ 1
- 25
tests/test_variable.py View File

1
 #!/usr/bin/env python3
1
 #!/usr/bin/env python3
2
 import pytest
2
 import pytest
3
-from starlette.authentication import UnauthenticatedUser
4
 from starlette.testclient import TestClient
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
 from pyheatpump.variables import app, get_variables, set_variables, ROUTES
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
 def test_get_(set_test_db):
8
 def test_get_(set_test_db):
34
     c = TestClient(app)
9
     c = TestClient(app)
38
     d_resp = r.json()
13
     d_resp = r.json()
39
     assert type(d_resp) is dict
14
     assert type(d_resp) is dict
40
     assert 'A' in d_resp.keys()
15
     assert 'A' in d_resp.keys()
16
+    print(d_resp)
41
     assert '10' in d_resp['A'].keys()
17
     assert '10' in d_resp['A'].keys()
42
     assert len(d_resp['A'].keys()) == 4
18
     assert len(d_resp['A'].keys()) == 4
43
     assert len(d_resp['I'].keys()) == 4
19
     assert len(d_resp['I'].keys()) == 4

+ 0
- 21
tests/test_variable_types.py View File

1
 #!/usr/bin/env python3
1
 #!/usr/bin/env python3
2
 import pytest
2
 import pytest
3
-from starlette.authentication import UnauthenticatedUser
4
 from starlette.testclient import TestClient
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 unittest.mock import patch, MagicMock
4
 from unittest.mock import patch, MagicMock
7
-from pprint import pprint
8
 import json
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
 from pyheatpump.variable_types import app, get_variable_types, set_variable_types, ROUTES
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
 def test_get_(set_test_db):
10
 def test_get_(set_test_db):
32
     c = TestClient(app)
11
     c = TestClient(app)

+ 0
- 29
tests/test_variable_values.py View File

1
 #!/usr/bin/env python3
1
 #!/usr/bin/env python3
2
 import pytest
2
 import pytest
3
-from starlette.authentication import UnauthenticatedUser
4
 from starlette.exceptions import HTTPException 
3
 from starlette.exceptions import HTTPException 
5
 from starlette.testclient import TestClient
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
 from datetime import datetime, timedelta
5
 from datetime import datetime, timedelta
14
 
6
 
15
-from pyheatpump.config import config
16
-from pyheatpump.db import initialize, connect
17
 from pyheatpump.variables import app, get_variables, set_variables, ROUTES
7
 from pyheatpump.variables import app, get_variables, set_variables, ROUTES
18
 from pyheatpump.variable_values import app, get_variable_value, set_variable_value, ROUTES
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
 def test_get_type_address(set_test_db):
11
 def test_get_type_address(set_test_db):

Loading…
Cancel
Save