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")
    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)