1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #!/usr/bin/env python3
- import pytest
- from starlette.authentication import UnauthenticatedUser
- from starlette.testclient import TestClient
- from pyheatpump.config import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES, mac_address_init, save_config
- from unittest.mock import patch, MagicMock
- from pprint import pprint
- import json
- from tempfile import mkstemp
- from configparser import ConfigParser
- import os
-
- @pytest.fixture
- def tmpconf(testdir):
- tmp_conf_filepath = os.path.join(testdir.tmpdir, 'pyheatpump.ini')
- config = ConfigParser(
- default_section='heatpump')
- config.read_dict(default_config)
-
-
- with open(tmp_conf_filepath, 'w') as f:
- f.write("[heatpump]\nmac_address = 'None'")
- CONFIG_FILES = [tmp_conf_filepath]
- config.read(filenames=CONFIG_FILES)
-
- return config
-
- def test_get_():
- 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_config():
- resp = await get_config(RequestMock())
- assert resp.status_code == 200
- d_resp = json.loads(resp.body.decode())
- for sect in default_config.keys():
- assert sect in d_resp.keys()
- for item in default_config[sect].keys():
- assert item in d_resp[sect].keys()
-
-
- def test_set_config():
- c = TestClient(app)
- _, tmpconf = mkstemp()
- CONFIG_FILES.insert(0, tmpconf)
- r = c.post('/', json=json.dumps({
- 'supervisor': {
- 'scheme':'test'
- }
- }))
-
- config = ConfigParser()
- config.read(filenames=[tmpconf])
-
- assert r.status_code == 200
- assert config.get('supervisor', 'scheme') == 'test'
-
- os.unlink(tmpconf)
-
-
- def test_mac_address(testdir, tmpconf):
- try:
- assert tmpconf.get('heatpump', 'mac_address') is 'None'
- mac_address = mac_address_init()
- config_mac_address = tmpconf.get('heatpump', 'mac_address')
- assert config_mac_address == mac_address
- except Exception as e:
- raise e
|