12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #!/usr/bin/env python3
- import pytest
- from starlette.authentication import UnauthenticatedUser
- from starlette.testclient import TestClient
- #from pyheatpump.conf import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES
- from unittest.mock import patch, MagicMock
- from pprint import pprint
- import json
- from tempfile import mkstemp
- from configparser import ConfigParser
- import os
- import sys
-
- from pyheatpump.conf import config
- from pyheatpump.db import initialize, connect
- from pyheatpump.variable_types import app, get_variable_types, set_variable_types, ROUTES
-
- @pytest.fixture(scope='module')
- def set_test_db():
- _, tmpdb = mkstemp(suffix='.db', dir=os.getcwd(), )
- print(f'Will store database in {tmpdb}')
- config['heatpump']['database'] = tmpdb
- if not initialize(os.path.join(os.getcwd(), 'db/pyheatpump.sql')):
- sys.exit(-1)
-
- yield
-
- os.unlink(tmpdb)
-
-
- def test_get_(set_test_db):
- c = TestClient(app)
- r = c.get('/')
- assert r.status_code == 200
-
- class RequestMock(MagicMock):
- def __get__(self, key):
- if key == 'method':
- return 'GET'
-
- @pytest.mark.asyncio
- async def test_get_variable_types(set_test_db):
- resp = await get_variable_types(RequestMock())
- assert resp.status_code == 200
- d_resp = json.loads(resp.body.decode())
- assert 'Analog' in d_resp.keys()
- assert type(d_resp['Analog']) == dict
- assert 'Integer' in d_resp.keys()
- assert type(d_resp['Integer']) == dict
- assert 'Digital' in d_resp.keys()
- assert type(d_resp['Digital']) == dict
-
- def test_set_variable_types(set_test_db):
- c = TestClient(app)
- r = c.post('/', json=json.dumps({
- 'Analog': {
- 'start_address': 42,
- 'end_address': 420,
- }
- }))
-
- assert r.status_code == 200
-
- r = c.get('/')
- d_resp = json.loads(r.content.decode())
- assert d_resp['Analog']['start_address'] == 42
- assert d_resp['Analog']['end_address'] == 420
|