craftbeerpi4-pione/tests/test_config.py

61 lines
1.7 KiB
Python
Raw Normal View History

2018-12-29 00:27:19 +01:00
import time
import aiosqlite
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
2019-01-05 20:43:48 +01:00
from cbpi.api.config import ConfigType
2018-12-29 00:27:19 +01:00
2019-01-05 20:43:48 +01:00
from cbpi.craftbeerpi import CraftBeerPi
2019-01-01 15:35:35 +01:00
2018-12-29 00:27:19 +01:00
class ConfigTestCase(AioHTTPTestCase):
async def get_application(self):
self.cbpi = CraftBeerPi()
2019-01-01 15:35:35 +01:00
await self.cbpi.init_serivces()
2018-12-29 00:27:19 +01:00
return self.cbpi.app
@unittest_run_loop
async def test_get(self):
2019-01-01 15:35:35 +01:00
assert self.cbpi.config.get("CBPI_TEST_1", 1) == "22"
2018-12-29 00:27:19 +01:00
@unittest_run_loop
async def test_set_get(self):
value = str(time.time())
2019-01-01 15:35:35 +01:00
await self.cbpi.config.set("CBPI_TEST_2", value)
2018-12-29 00:27:19 +01:00
2019-01-01 15:35:35 +01:00
assert self.cbpi.config.get("CBPI_TEST_2", 1) == value
2018-12-29 00:27:19 +01:00
@unittest_run_loop
async def test_add(self):
value = str(time.time())
key = "CBPI_TEST_3"
async with aiosqlite.connect("./craftbeerpi.db") as db:
await db.execute("DELETE FROM config WHERE name = ? ", (key,))
await db.commit()
2019-01-01 15:35:35 +01:00
await self.cbpi.config.add(key, value, type=ConfigType.STRING, description="test")
2018-12-29 00:27:19 +01:00
@unittest_run_loop
async def test_http_set(self):
value = str(time.time())
key = "CBPI_TEST_3"
2019-01-01 15:35:35 +01:00
await self.cbpi.config.set(key, value)
assert self.cbpi.config.get(key, 1) == value
2018-12-29 00:27:19 +01:00
2019-01-28 22:21:31 +01:00
resp = await self.client.request("PUT", "/config/%s/" % key, json={'value': '1'})
2018-12-29 00:27:19 +01:00
assert resp.status == 204
2019-01-01 15:35:35 +01:00
assert self.cbpi.config.get(key, -1) == "1"
2018-12-29 00:27:19 +01:00
@unittest_run_loop
async def test_http_get(self):
resp = await self.client.request("GET", "/config/")
assert resp.status == 200
2019-01-02 00:48:36 +01:00
2019-01-02 21:20:44 +01:00
@unittest_run_loop
async def test_get_default(self):
value = self.cbpi.config.get("HELLO_WORLD", None)
assert value == None