Exemplo n.º 1
0
def test_read_write_matrix(dburl):
    with yenot.tests.server_running(dburl) as server:
        session = yclient.YenotSession(server.url)
        client = session.std_client()

        # do some reading and writing
        itable = rtlib.simple_table(["name"])
        with itable.adding_row() as row:
            row.name = "New"
        client.post("api/test/item-tag",
                    files={"tag": itable.as_http_post_file()})
        itable = rtlib.simple_table(["name"])
        with itable.adding_row() as row:
            row.name = "Used"
        client.post("api/test/item-tag",
                    files={"tag": itable.as_http_post_file()})

        payload = client.get("api/test/item-tag/list")
        tags = payload.main_table()

        # do some reading and writing
        itable = rtlib.simple_table(["name", "price", "tags"])
        with itable.adding_row() as row:
            row.name = "New Computer"
            row.price = 30
            row.tags = {"add": [t.id for t in tags.rows if t.name == "New"]}
        client.post(
            "api/test/item",
            files={"item": itable.as_http_post_file()},
        )

        data = client.get("api/test/items/list")
        items = data.main_table()

        newcomp = [row for row in items.rows if row.name == "New Computer"]

        payload = client.get("api/test/item/{}/record", newcomp[0].id)
        item = payload.main_table()
        assert item.rows[0].tags == ["New"], "Should have only tag New"

        row = item.rows[0]
        row.price = 32
        row.tags = {
            "add": [t.id for t in tags.rows if t.name == "Used"],
            "remove": [t.id for t in tags.rows if t.name == "New"],
        }
        client.put(
            "api/test/item/{}",
            newcomp[0].id,
            files={
                "item":
                item.as_http_post_file(inclusions=["price", "name", "tags"])
            },
        )

        payload = client.get("api/test/item/{}/record", newcomp[0].id)
        item = payload.main_table()
        assert item.rows[0].tags == ["Used"], "Should have only tag Used"
Exemplo n.º 2
0
    def changes_since(self, cancel, index):
        changes = rtlib.simple_table(["index", "payload"])

        wait_count = 10
        wait_length = 4

        for i in range(wait_count):
            self.last_check = time.time()

            for chrow in self.thislist:
                if chrow[1] > index:
                    with changes.adding_row() as r2:
                        r2.index = chrow[1]
                        r2.payload = chrow[2]
                    index = chrow[1]

            if len(changes.rows) > 0:
                break

            if i < wait_count - 1:
                self.event.wait(wait_length)
                if not self.event.is_set():
                    # give a chance for the cancel exception
                    cancel.wait(0.01)

        return changes
Exemplo n.º 3
0
def delete_api_test_item_record(itemid):
    with app.dbconn() as conn:
        item = rtlib.simple_table(["id"])
        with item.adding_row() as r2:
            r2.id = int(itemid)
        with api.writeblock(conn) as w:
            w.delete_rows("item", item)
        conn.commit()
    return api.Results().json_out()
Exemplo n.º 4
0
def post_api_test_receive_table():
    incoming = api.table_from_tab2("inbound", required=["id", "xint"])

    out = rtlib.simple_table(["id", "xint"])
    for row in incoming.rows:
        with out.adding_row() as r2:
            r2.id = row.id
            r2.xint = row.xint + 1

    results = api.Results()
    results.tables["outbound"] = out.as_tab2()
    return results.json_out()
Exemplo n.º 5
0
def test_read_write(dburl):
    with yenot.tests.server_running(dburl) as server:
        session = yclient.YenotSession(server.url)
        client = session.std_client()

        # do some reading and writing
        client.post("api/test/item-simple", name="Table", price=35)
        try:
            client.post("api/test/item-simple", name="Table", price=35)
        except Exception as e:
            assert str(e).find("duplicate key") > 0
        try:
            client.post("api/test/item-simple", name="Sofa")
        except Exception as e:
            assert str(e).find("non-empty and valid") > 0

        itable = rtlib.simple_table(["name", "price"])
        with itable.adding_row() as row:
            row.name = "Computer"
            row.price = 30
        client.post("api/test/item",
                    files={"item": itable.as_http_post_file()})

        data = client.get("api/test/items/list")
        items = data.main_table()

        for row in items.rows:
            if row.name == "Table":
                row.price = 81

        client.put(
            "api/test/update-items",
            files={
                "item":
                items.as_http_post_file(inclusions=["id", "name", "price"])
            },
        )

        for row in items.rows:
            row.client_class = "X"
        client.put(
            "api/test/update-item-extras",
            files={
                "item":
                items.as_http_post_file(
                    inclusions=["name", "price", "client_class"])
            },
        )
Exemplo n.º 6
0
def test_server_info(dburl):
    with yenot.tests.server_running(dburl) as server:
        session = yclient.YenotSession(server.url)

        client = session.raw_client()
        client.get("api/test/http-method-get")
        client.post("api/test/http-method-post")
        client.put("api/test/http-method-put")
        client.delete("api/test/http-method-delete")

        client = session.std_client()

        client.get(
            "api/test/parse-types",
            myfloat=3.1415926535,
            myint=64,
            mybool=True,
            mydate="2019-01-01",
        )

        something = client.get("api/test/prototype")
        assert something.keys["scalar"] == 23
        assert something.named_table("data").rows[0].name == "Fred"
        something = client.get("api/test/date-columns")
        something.named_table("data")
        something = client.get("api/test/modify-table")
        something.named_table("data")

        try:
            client.get("api/test/sql-exception01")
        except yclient.YenotServerError as e:
            assert str(e).find("with status code") > 0

        try:
            client.get("api/test/sql-exception02")
        except yclient.YenotServerError as e:
            assert str(e).find("with status code") > 0

        try:
            client.get("api/test/python-error")
        except yclient.YenotServerError as e:
            assert str(e).find("with status code") > 0

        try:
            client.get("api/test/user-error")
        except yclient.YenotError as e:
            assert str(e).find("human") > 0

        inbound = rtlib.simple_table(["id", "xint"])
        with inbound.adding_row() as row:
            row.id = "x1"
            row.xint = 1
        with inbound.adding_row() as row:
            row.id = "x2"
            row.xint = 2
        out = client.post("api/test/receive-table",
                          files={"inbound": inbound.as_http_post_file()})
        assert out.named_table("outbound").rows[0].xint == 2
        try:
            t = rtlib.simple_table(["id", "not_good"])
            out = client.post("api/test/receive-table",
                              files={"inbound": t.as_http_post_file()})
        except Exception as e:
            assert str(e).find("contains incorrect data")
        try:
            t = rtlib.simple_table(["id"])
            out = client.post("api/test/receive-table",
                              files={"inbound": t.as_http_post_file()})
        except Exception as e:
            assert str(e).find("fields not given")

        # for sake of coverage, quick call
        t1 = time.time()
        with futures.ThreadPoolExecutor(max_workers=1) as executor:
            fsleep = client.future(executor).get("api/request/sleep",
                                                 duration=4)
            time.sleep(0.5)  # give a bit of time for the server
            fsleep.cancel()
            try:
                fsleep.result()
            except Exception as e:
                assert str(e).find("cancelled request") > 0
        t2 = time.time()
        assert t2 - t1 < 1

        t1 = time.time()
        with futures.ThreadPoolExecutor(max_workers=2) as executor:
            fsleep1 = client.future(executor).get("api/request/sleep",
                                                  duration=0.4)
            fsleep2 = client.future(executor).get("api/request/sleep",
                                                  duration=0.4)
            sleeps = fsleep1.result(), fsleep2.result()
            assert sleeps[0].named_table("sleep") != None
            assert sleeps[1].named_table("sleep") != None
        t2 = time.time()
        assert 0.4 < t2 - t1 < 0.6, f"timed out -- {t2}, {t1}"

        client.get("api/sql/info")