def test_quote_int(conn, val, expr): tx = Transformer() assert tx.get_dumper(val, Format.TEXT).quote(val) == expr cur = conn.cursor() cur.execute(sql.SQL("select {v}, -{v}").format(v=sql.Literal(val))) assert cur.fetchone() == (val, -val)
def test_array_dumper(conn, fmt_out): t = Transformer(conn) fmt_in = PyFormat.from_pq(fmt_out) dint = t.get_dumper([0], fmt_in) if fmt_out == pq.Format.BINARY: assert dint.oid == builtins["int2"].array_oid assert dint.sub_dumper.oid == builtins["int2"].oid else: assert dint.oid == builtins["numeric"].array_oid assert dint.sub_dumper is None dstr = t.get_dumper([""], fmt_in) if fmt_in == PyFormat.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(psycopg.DataError): assert t.get_dumper(L, fmt_in)
def test_quote_none(conn): tx = Transformer() assert tx.get_dumper(None, Format.TEXT).quote(None) == b"NULL" cur = conn.cursor() cur.execute(sql.SQL("select {v}").format(v=sql.Literal(None))) assert cur.fetchone()[0] is None
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 != PyFormat.BINARY: assert dumper.oid == 0 else: assert dumper.oid == builtins[type].oid
def test_quote_bool(conn, val): tx = Transformer() assert tx.get_dumper( val, Format.TEXT).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
def test_quote_numeric(conn, val, expr): val = Decimal(val) tx = Transformer() assert tx.get_dumper(val, Format.TEXT).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)
def test_quote_float(conn, val, expr): tx = Transformer() assert tx.get_dumper(val, Format.TEXT).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)
def test_bad_binary_array(input): tx = Transformer() with pytest.raises(psycopg.DataError): tx.get_dumper(input, PyFormat.BINARY).dump(input)
def test_array_mixed_numbers(array, type): tx = Transformer() dumper = tx.get_dumper(array, PyFormat.BINARY) dumper.dump(array) assert dumper.oid == builtins[type].array_oid
def test_pg_query_seq(query, params, want, wformats, wparams): pq = PostgresQuery(Transformer()) pq.convert(query, params) assert pq.query == want assert pq.formats == wformats assert pq.params == wparams
def test_pq_query_badprog(query, params): pq = PostgresQuery(Transformer()) with pytest.raises(psycopg.ProgrammingError): pq.convert(query, params)
def test_pq_query_badtype(query, params): pq = PostgresQuery(Transformer()) with pytest.raises(TypeError): pq.convert(query, params)
def test_quote(data, result): t = Transformer() dumper = t.get_dumper(data, PyFormat.TEXT) assert dumper.quote(data) == result
def test_cast(data, format, type, result): t = Transformer() rv = t.get_loader(builtins[type].oid, format).load(data) assert rv == result