Exemplo n.º 1
0
def test_quote_int(conn, val, expr):
    tx = Transformer()
    assert tx.get_dumper(val, 0).quote(val) == expr

    cur = conn.cursor()
    cur.execute(sql.SQL("select {v}, -{v}").format(v=sql.Literal(val)))
    assert cur.fetchone() == (val, -val)
Exemplo n.º 2
0
def test_quote_none(conn):

    tx = Transformer()
    assert tx.get_dumper(None, 0).quote(None) == b"NULL"

    cur = conn.cursor()
    cur.execute(sql.SQL("select {v}").format(v=sql.Literal(None)))
    assert cur.fetchone()[0] is None
Exemplo n.º 3
0
def test_quote_bool(conn, val):

    tx = Transformer()
    assert tx.get_dumper(val, 0).quote(val) == str(val).lower().encode("ascii")

    cur = conn.cursor()
    cur.execute(sql.SQL("select {v}").format(v=sql.Literal(val)))
    assert cur.fetchone()[0] is val
Exemplo n.º 4
0
def test_dump(data, format, result, type):
    t = Transformer()
    dumper = t.get_dumper(data, format)
    assert dumper.dump(data) == result
    if type == "text" and format != Format.BINARY:
        assert dumper.oid == 0
    else:
        assert dumper.oid == builtins[type].oid
Exemplo n.º 5
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)
Exemplo n.º 6
0
def test_quote_numeric(conn, val, expr):
    val = Decimal(val)
    tx = Transformer()
    assert tx.get_dumper(val, 0).quote(val) == expr

    cur = conn.cursor()
    cur.execute(sql.SQL("select {v}, -{v}").format(v=sql.Literal(val)))
    r = cur.fetchone()

    if val.is_nan():
        assert isnan(r[0]) and isnan(r[1])
    else:
        assert r == (val, -val)
Exemplo n.º 7
0
def test_quote_float(conn, val, expr):
    tx = Transformer()
    assert tx.get_dumper(val, 0).quote(val) == expr

    cur = conn.cursor()
    cur.execute(sql.SQL("select {v}, -{v}").format(v=sql.Literal(val)))
    r = cur.fetchone()
    if isnan(val):
        assert isnan(r[0]) and isnan(r[1])
    else:
        if isinstance(r[0], Decimal):
            r = tuple(map(float, r))

        assert r == (val, -val)
Exemplo n.º 8
0
def test_dump_numeric_binary():
    # TODO: numeric binary adaptation
    tx = Transformer()
    n = Decimal(1)
    tx.get_dumper(n, Format.BINARY).dump(n)
Exemplo n.º 9
0
def test_bad_binary_array(input):
    tx = Transformer()
    with pytest.raises(psycopg3.DataError):
        tx.get_dumper(input, Format.BINARY).dump(input)
Exemplo n.º 10
0
def test_array_mixed_numbers(array, type):
    # TODO: must use the type accommodating the largest/highest precision
    tx = Transformer()
    dumper = tx.get_dumper(array, Format.BINARY)
    dumper.dump(array)
    assert dumper.oid == builtins[type].array_oid
Exemplo n.º 11
0
def test_quote(data, result):
    t = Transformer()
    dumper = t.get_dumper(data, Format.TEXT)
    assert dumper.quote(data) == result
Exemplo n.º 12
0
def test_dump(data, format, result, type):
    t = Transformer()
    dumper = t.get_dumper(data, format)
    assert dumper.dump(data) == result
    assert dumper.oid == 0 if type == "text" else builtins[type].oid
Exemplo n.º 13
0
def test_array_mixed_numbers(array, type):
    tx = Transformer()
    dumper = tx.get_dumper(array, Format.BINARY)
    dumper.dump(array)
    assert dumper.oid == builtins[type].array_oid
Exemplo n.º 14
0
def test_dump_int_binary():
    # TODO: int binary adaptation (must choose the fitting int2,4,8)
    tx = Transformer()
    n = 1
    tx.get_dumper(n, Format.BINARY).dump(n)
Exemplo n.º 15
0
def test_dump_float_binary():
    # TODO: float binary adaptation
    tx = Transformer()
    n = 1.0
    tx.get_dumper(n, Format.BINARY).dump(n)