Esempio n. 1
0
def test_array_dumper(conn, fmt_out):
    t = Transformer(conn)
    fmt_in = Format.from_pq(fmt_out)
    dint = t.get_dumper([0], fmt_in)
    assert dint.oid == builtins["int2"].array_oid
    assert dint.sub_dumper.oid == builtins["int2"].oid

    dstr = t.get_dumper([""], fmt_in)
    if fmt_in == Format.BINARY:
        assert dstr.oid == builtins["text"].array_oid
        assert dstr.sub_dumper.oid == builtins["text"].oid
    else:
        assert dstr.oid == 0
        assert dstr.sub_dumper.oid == 0

    assert dstr is not dint

    assert t.get_dumper([1], fmt_in) is dint
    assert t.get_dumper([None, [1]], fmt_in) is dint

    dempty = t.get_dumper([], fmt_in)
    assert t.get_dumper([None, [None]], fmt_in) is dempty
    assert dempty.oid == 0
    assert dempty.dump([]) == b"{}"

    L = []
    L.append(L)
    with pytest.raises(psycopg3.DataError):
        assert t.get_dumper(L, fmt_in)
Esempio n. 2
0
async def test_copy_to_leaks(dsn, faker, fmt, method):
    faker.format = PgFormat.from_pq(fmt)
    faker.choose_schema(ncols=20)
    faker.make_records(20)

    n = []
    for i in range(3):
        async with await psycopg3.AsyncConnection.connect(dsn) as conn:
            async with conn.cursor(binary=fmt) as cur:
                await cur.execute(faker.drop_stmt)
                await cur.execute(faker.create_stmt)
                await cur.executemany(faker.insert_stmt, faker.records)

                stmt = sql.SQL(
                    "copy (select {} from {} order by id) to stdout (format {})"
                ).format(
                    sql.SQL(", ").join(faker.fields_names),
                    faker.table_name,
                    sql.SQL(fmt.name),
                )

                async with cur.copy(stmt) as copy:
                    types = [
                        t.as_string(conn).replace('"', "")
                        for t in faker.types_names
                    ]
                    copy.set_types(types)

                    if method == "read":
                        while 1:
                            tmp = await copy.read()
                            if not tmp:
                                break
                    elif method == "iter":
                        async for x in copy:
                            pass
                    elif method == "row":
                        while 1:
                            tmp = await copy.read_row()
                            if tmp is None:
                                break
                    elif method == "rows":
                        async for x in copy.rows():
                            pass

                    tmp = None

        del cur, conn
        gc.collect()
        gc.collect()
        n.append(len(gc.get_objects()))

    assert (
        n[0] == n[1] == n[2]
    ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
Esempio n. 3
0
    def get_supported_types(self):
        dumpers = self.conn.adapters._dumpers[Format.as_pq(self.format)]
        rv = set()
        for cls in dumpers.keys():
            if isinstance(cls, str):
                cls = deep_import(cls)
            rv.add(cls)

        # check all the types are handled
        for cls in rv:
            self.get_maker(cls)

        return rv
Esempio n. 4
0
def test_random(conn, faker, fmt):
    faker.format = fmt
    faker.choose_schema(ncols=20)
    faker.make_records(50)

    with conn.cursor(binary=Format.as_pq(fmt)) as cur:
        cur.execute(faker.drop_stmt)
        cur.execute(faker.create_stmt)
        cur.executemany(faker.insert_stmt, faker.records)
        cur.execute(faker.select_stmt)
        recs = cur.fetchall()

    for got, want in zip(recs, faker.records):
        faker.assert_record(got, want)
Esempio n. 5
0
def test_leak(dsn, faker, fmt, fetch, row_factory):
    faker.format = fmt
    faker.choose_schema(ncols=5)
    faker.make_records(10)
    row_factory = getattr(rows, row_factory)

    n = []
    for i in range(3):
        with psycopg3.connect(dsn) as conn:
            with conn.cursor(binary=Format.as_pq(fmt),
                             row_factory=row_factory) as cur:
                cur.execute(faker.drop_stmt)
                cur.execute(faker.create_stmt)
                cur.executemany(faker.insert_stmt, faker.records)
                cur.execute(faker.select_stmt)

                if fetch == "one":
                    while 1:
                        tmp = cur.fetchone()
                        if tmp is None:
                            break
                elif fetch == "many":
                    while 1:
                        tmp = cur.fetchmany(3)
                        if not tmp:
                            break
                elif fetch == "all":
                    cur.fetchall()
                elif fetch == "iter":
                    for rec in cur:
                        pass

                tmp = None

        del cur, conn
        gc.collect()
        gc.collect()
        n.append(len(gc.get_objects()))

    assert (n[0] == n[1] ==
            n[2]), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
Esempio n. 6
0
async def test_copy_from_leaks(dsn, faker, fmt):
    faker.format = PgFormat.from_pq(fmt)
    faker.choose_schema(ncols=20)
    faker.make_records(20)

    n = []
    for i in range(3):
        async with await psycopg3.AsyncConnection.connect(dsn) as conn:
            async with conn.cursor(binary=fmt) as cur:
                await cur.execute(faker.drop_stmt)
                await cur.execute(faker.create_stmt)

                stmt = sql.SQL("copy {} ({}) from stdin (format {})").format(
                    faker.table_name,
                    sql.SQL(", ").join(faker.fields_names),
                    sql.SQL(fmt.name),
                )
                async with cur.copy(stmt) as copy:
                    for row in faker.records:
                        await copy.write_row(row)

                await cur.execute(faker.select_stmt)
                recs = await cur.fetchall()

                for got, want in zip(recs, faker.records):
                    faker.assert_record(got, want)

                del recs

        del cur, conn
        gc.collect()
        gc.collect()
        n.append(len(gc.get_objects()))

    assert (
        n[0] == n[1] == n[2]
    ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"