12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #!/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
-
- 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)
|