craftbeerpi4-pione/tests/test_step.py

100 lines
2.8 KiB
Python
Raw Normal View History

2019-01-02 21:20:44 +01:00
import asyncio
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
2019-01-05 20:43:48 +01:00
from cbpi.craftbeerpi import CraftBeerPi
2019-01-02 21:20:44 +01:00
class StepTestCase(AioHTTPTestCase):
async def get_application(self):
self.cbpi = CraftBeerPi()
await self.cbpi.init_serivces()
return self.cbpi.app
@unittest_run_loop
async def test_get(self):
resp = await self.client.request("GET", "/step")
2019-07-27 21:08:19 +02:00
print(resp)
2019-01-02 21:20:44 +01:00
assert resp.status == 200
resp = await self.client.request("GET", "/step/types")
2019-07-27 21:08:19 +02:00
print(resp)
2019-01-02 21:20:44 +01:00
assert resp.status == 200
@unittest_run_loop
async def test_crud(self):
data = {
"name": "Test",
"type": "CustomStepCBPi",
2019-07-27 21:08:19 +02:00
"config": {}
2019-01-02 21:20:44 +01:00
}
2019-07-27 21:08:19 +02:00
# Add new step
2019-01-02 21:20:44 +01:00
resp = await self.client.post(path="/step/", json=data)
assert resp.status == 200
m = await resp.json()
2019-07-27 21:08:19 +02:00
print("Step", m)
2019-01-02 21:20:44 +01:00
sensor_id = m["id"]
# Get sensor
resp = await self.client.get(path="/step/%s" % sensor_id)
assert resp.status == 200
m2 = await resp.json()
sensor_id = m2["id"]
# Update Sensor
resp = await self.client.put(path="/step/%s" % sensor_id, json=m)
assert resp.status == 200
# # Delete Sensor
resp = await self.client.delete(path="/step/%s" % sensor_id)
assert resp.status == 204
2019-01-04 09:29:09 +01:00
def create_wait_callback(self, topic):
future = self.cbpi.app.loop.create_future()
async def test(**kwargs):
print("GOON")
future.set_result("OK")
self.cbpi.bus.register(topic, test, once=True)
return future
async def wait(self, future):
done, pending = await asyncio.wait({future})
if future in done:
pass
2019-01-02 21:20:44 +01:00
@unittest_run_loop
async def test_process(self):
2019-07-27 21:08:19 +02:00
step_ctlr = self.cbpi.step
2019-01-02 21:20:44 +01:00
2019-07-27 21:08:19 +02:00
await step_ctlr.clear_all()
await step_ctlr.add(**{"name": "Kettle1", "type": "CustomStepCBPi", "config": {"name1": "1", "temp": 99}})
await step_ctlr.add(**{"name": "Kettle1", "type": "CustomStepCBPi", "config": {"name1": "1", "temp": 99}})
await step_ctlr.add(**{"name": "Kettle1", "type": "CustomStepCBPi", "config": {"name1": "1", "temp": 99}})
2019-01-02 21:20:44 +01:00
2019-07-27 21:08:19 +02:00
await step_ctlr.stop_all()
future = self.create_wait_callback("step/+/started")
await step_ctlr.start()
await self.wait(future)
2019-01-02 21:20:44 +01:00
2019-07-27 21:08:19 +02:00
for i in range(len(step_ctlr.cache)-1):
2019-01-04 09:29:09 +01:00
future = self.create_wait_callback("step/+/started")
2019-07-27 21:08:19 +02:00
await step_ctlr.next()
2019-01-04 09:29:09 +01:00
await self.wait(future)
2019-01-02 21:20:44 +01:00
2019-07-27 21:08:19 +02:00
await self.print_steps()
async def print_steps(self):
2019-01-02 21:20:44 +01:00
2019-07-27 21:08:19 +02:00
s = await self.cbpi.step.get_all()
print(s)
for k, v in s.items():
2019-07-31 07:58:54 +02:00
print(k, v.to_json())