[get/post] /config/last_update | [get/post] /config/mac_address

This commit is contained in:
Maxime Alves LIRMM@home 2020-09-15 23:25:47 +02:00
commit 5d677464d9
2 changed files with 52 additions and 44 deletions

View file

@ -3,7 +3,7 @@ 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, \
CONFIG_FILES, get_config, get_config_dict, set_config, config_route, ROUTES, \
mac_address_init, save_config, get_last_update, set_last_update, \
last_update_route
from unittest.mock import patch, MagicMock
@ -13,6 +13,8 @@ from tempfile import mkstemp
from configparser import ConfigParser
import os
from requests import Request
from uuid import uuid4
from datetime import datetime
@pytest.fixture
def tmpconf(testdir):
@ -24,12 +26,12 @@ def tmpconf(testdir):
with open(tmp_conf_filepath, 'w') as f:
f.write("[heatpump]\nmac_address = 'None'")
CONFIG_FILES = [tmp_conf_filepath]
#CONFIG_FILES = [tmp_conf_filepath]
config.read(filenames=CONFIG_FILES)
return config
def test_get_():
def test_get_(tmpconf):
c = TestClient(app)
r = c.get('/')
assert r.status_code == 200
@ -41,24 +43,26 @@ class RequestMock(MagicMock):
return 'GET'
@pytest.mark.asyncio
async def test_get_config():
resp = await get_config(RequestMock())
def test_get_config(tmpconf):
c = TestClient(app)
resp = c.get('/')
assert resp.status_code == 200
d_resp = json.loads(resp.body.decode())
d_resp = resp.json()
assert type(d_resp) == dict
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():
def test_set_config(tmpconf):
c = TestClient(app)
_, tmpconf = mkstemp()
CONFIG_FILES.insert(0, tmpconf)
CONFIG_FILES.append(tmpconf)
rand = str(uuid4())
r = c.post('/', json=json.dumps({
'supervisor': {
'scheme':'test'
'scheme':rand
}
}))
@ -66,28 +70,31 @@ def test_set_config():
config.read(filenames=[tmpconf])
assert r.status_code == 200
assert config.get('supervisor', 'scheme') == 'test'
assert config.get('supervisor', 'scheme') == rand
assert type(get_config_dict()) == dict
d_config =get_config_dict()
assert 'supervisor' in d_config.keys()
assert 'scheme' in d_config['supervisor'].keys()
assert d_config['supervisor']['scheme'] == rand
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
mac_address = mac_address_init()
assert type(get_config_dict()) == dict
d_config =get_config_dict()
assert d_config['heatpump']['mac_address'] == mac_address
@pytest.mark.asyncio
async def test_last_update(testdir, tmpconf):
req = Request()
req.method = 'POST'
res = await last_update_route(req)
last_update_0 = int(res.body)
def test_last_update(testdir, tmpconf):
c = TestClient(app)
resp = c.get('/last_update')
last_update_0 = int(resp.content.decode())
print(f'update0 {last_update_0}')
res = await get_last_update(req)
last_update_1 = int(res.body)
resp = c.post('/last_update', str(datetime.now().strftime('%s')))
last_update_1 = int(resp.content.decode())
print(f'update1 {last_update_1}')
assert last_update_0 == last_update_1