123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #!/usr/bin/env python3
- import pytest
- from starlette.authentication import UnauthenticatedUser
- from starlette.testclient import TestClient
- from pyheatpump.config import app, config, \
- 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
- from pprint import pprint
- import json
- 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):
- tmp_conf_filepath = os.path.join(testdir.tmpdir, 'pyheatpump.ini')
- config = ConfigParser(
- default_section='heatpump')
-
-
- 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_(tmpconf):
- c = TestClient(app)
- r = c.get('/')
- assert r.status_code == 200
-
-
- class RequestMock(MagicMock):
- def __get__(self, key):
- if key == 'method':
- return 'GET'
-
-
- def test_get_config(tmpconf):
- c = TestClient(app)
- resp = c.get('/')
- assert resp.status_code == 200
- 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(tmpconf):
- c = TestClient(app)
- _, tmpconf = mkstemp()
- CONFIG_FILES.append(tmpconf)
- rand = str(uuid4())
- r = c.post('/', json=json.dumps({
- 'supervisor': {
- 'scheme':rand
- }
- }))
-
- config = ConfigParser()
- config.read(filenames=[tmpconf])
-
- assert r.status_code == 200
- 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):
- c = TestClient(app)
- _, tmpconf = mkstemp()
- CONFIG_FILES.append(tmpconf)
-
- mac_address = mac_address_init()
- assert type(get_config_dict()) == dict
- d_config = get_config_dict()
-
- assert d_config['heatpump']['mac_address'] == mac_address
-
- resp = c.get('/mac')
- assert resp.status_code == 200
- mac_address = resp.content.decode()
-
- assert d_config['heatpump']['mac_address'] == mac_address
-
-
-
-
- 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}')
-
- 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
|