Exemplo n.º 1
0
    async def test_stream_create(self):
        db: Session = self.app["db"]
        new_stream = DataStream(name="test", datatype=DataStream.DATATYPE.FLOAT32)
        new_stream.elements = [Element(name="e%d" % j, index=j,
                                       display_type=Element.DISPLAYTYPE.CONTINUOUS) for j in range(3)]
        payload = {
            "dest_path": "/deep/new folder",
            "stream": new_stream.to_json()
        }
        resp = await self.client.post("/stream.json", json=payload)

        self.assertEqual(resp.status, 200)
        # check the stream was created correctly
        created_stream: DataStream = db.query(DataStream).filter_by(name="test").one()
        self.assertEqual(len(created_stream.elements), len(new_stream.elements))
        self.assertEqual(created_stream.folder.name, "new folder")

        # can create by dest_id as well
        folder1: Folder = db.query(Folder).filter_by(name="folder1").one()
        new_stream.name = "test2"
        payload = {
            "dest_id": folder1.id,
            "stream": new_stream.to_json()
        }
        resp = await self.client.post("/stream.json", json=payload)

        self.assertEqual(resp.status, 200)
        # check the stream was created correctly
        created_stream: DataStream = db.query(DataStream).filter_by(name="test2").one()
        self.assertEqual(len(created_stream.elements), len(new_stream.elements))
        self.assertEqual(created_stream.folder.name, "folder1")
Exemplo n.º 2
0
async def _create_nilmdb_stream(server: str, path: str, template: DataStream):
    click.echo("\tcreating destination stream")
    dest_stream = data_stream.from_json(template.to_json())
    dest_stream.keep_us = DataStream.KEEP_ALL
    dest_stream.name = path.split("/")[-1]

    url = "{server}/stream/create".format(server=server)
    data = {"path": path, "layout": dest_stream.layout}
    async with aiohttp.ClientSession() as session:
        async with session.post(url, data=data) as resp:
            if not resp.status == 200:
                raise click.ClickException("cannot create [%s] on [%s]" %
                                           (path, server))

        # add the metadata
        url = "{server}/stream/set_metadata".format(server=server)
        data = {
            "path":
            path,
            "data":
            json.dumps(
                {"config_key__": json.dumps(dest_stream.to_nilmdb_metadata())})
        }
        async with session.post(url, data=data) as resp:
            if not resp.status == 200:
                raise click.ClickException(
                    "cannot create metadata for [%s] on [%s]" % (path, server))
    return dest_stream
Exemplo n.º 3
0
async def _create_joule_stream(node: BaseNode, path: str,
                               template: DataStream) -> DataStream:
    click.echo("\tcreating destination stream")
    # split the destination into the path and stream name
    dest_stream = data_stream.from_json(template.to_json())
    dest_stream.keep_us = DataStream.KEEP_ALL
    dest_stream.name = path.split("/")[-1]
    folder = "/".join(path.split("/")[:-1])
    return await node.data_stream_create(dest_stream, folder)
    async def test_stream_create(self):
        # must be json
        resp = await self.client.post("/stream.json", data={"bad_values": "not_json"})
        self.assertEqual(resp.status, 400)
        self.assertIn("json", await resp.text())

        # must specify a stream
        resp = await self.client.post("/stream.json", json={"dest_path": "/folder2/deeper"})
        self.assertEqual(resp.status, 400)
        # invalid dest_path
        resp = await self.client.post("/stream.json", json={"dest_path": "notapath"})
        self.assertEqual(resp.status, 400)
        # must specify a path
        new_stream = DataStream(name="test", datatype=DataStream.DATATYPE.FLOAT32)
        new_stream.elements = [Element(name="e%d" % j, index=j,
                                       display_type=Element.DISPLAYTYPE.CONTINUOUS) for j in range(3)]
        resp = await self.client.post("/stream.json", json={"stream": new_stream.to_json()})
        self.assertEqual(resp.status, 400)
        # element names must be unique
        e1_name = new_stream.elements[1].name
        new_stream.elements[1].name = new_stream.elements[2].name
        resp = await self.client.post("/stream.json", json={
            "dest_path": "/folder2/deeper",
            "stream": new_stream.to_json()})
        self.assertEqual(resp.status, 400)
        self.assertIn("names must be unique", await resp.text())
        new_stream.elements[1].name = e1_name  # restore the original name

        # stream must have a unique name in the folder
        new_stream.name = "stream1"
        resp = await self.client.post("/stream.json", json={"stream": new_stream.to_json(),
                                                            "dest_path": "/folder1"})
        self.assertEqual(resp.status, 400)
        self.assertIn("same name", await resp.text())
        # invalid dest_path
        resp = await self.client.post("/stream.json", json={"stream": new_stream.to_json(),
                                                            "dest_path": "notapath"})
        self.assertEqual(resp.status, 400)
        # stream must have a name
        new_stream.name = ""
        resp = await self.client.post("/stream.json", json={"stream": new_stream.to_json(),
                                                            "dest_path": "/a/valid/path"})
        self.assertEqual(resp.status, 400)
        self.assertIn("name", await resp.text())
        new_stream.name = "test"
        # stream must have at least one element
        new_stream.elements = []
        resp = await self.client.post("/stream.json", json={"stream": new_stream.to_json(),
                                                            "dest_path": "/a/valid/path"})
        self.assertEqual(resp.status, 400)
        self.assertIn("element", await resp.text())

        # invalid stream json (test all different exception paths)
        resp = await self.client.post("/stream.json", json={"dest_path": "/path/to",
                                                            "stream": 'notjson'})
        self.assertEqual(resp.status, 400)
        self.assertIn("JSON", await resp.text())

        json = new_stream.to_json()
        json["datatype"] = "invalid"
        resp = await self.client.post("/stream.json", json={"dest_path": "/path/to",
                                                            "stream": json})
        self.assertEqual(resp.status, 400)
        self.assertIn("specification", await resp.text())
        self.assertIn("datatype", await resp.text())

        del json["datatype"]
        resp = await self.client.post("/stream.json", json={"dest_path": "/path/to",
                                                            "stream": json})
        self.assertEqual(resp.status, 400)
        self.assertIn("datatype", await resp.text())

        # incorrect stream format
        resp = await self.client.post("/stream.json", json={"path": "/path/to",
                                                            "stream": '{"invalid": 2}'})
        self.assertEqual(resp.status, 400)