示例#1
0
 def test_as_str(self, conn):
     assert sql.Literal(None).as_string(conn) == "NULL"
     assert noe(sql.Literal("foo").as_string(conn)) == "'foo'"
     assert sql.Literal(42).as_string(conn) == "42"
     assert (
         sql.Literal(dt.date(2017, 1, 1)).as_string(conn) == "'2017-01-01'"
     )
示例#2
0
    def test_sum_inplace(self, conn):
        obj = sql.Composed([sql.SQL("foo ")])
        obj += sql.Literal("bar")
        assert isinstance(obj, sql.Composed)
        assert noe(obj.as_string(conn)) == "foo 'bar'"

        obj = sql.Composed([sql.SQL("foo ")])
        obj += sql.Composed([sql.Literal("bar")])
        assert isinstance(obj, sql.Composed)
        assert noe(obj.as_string(conn)) == "foo 'bar'"
示例#3
0
def test_quote_percent(conn):
    cur = conn.cursor()
    cur.execute(sql.SQL("select {ch}").format(ch=sql.Literal("%")))
    assert cur.fetchone()[0] == "%"

    cur.execute(
        sql.SQL("select {ch} = chr(%s::int)").format(ch=sql.Literal("%")),
        (ord("%"),),
    )
    assert cur.fetchone()[0] is True
示例#4
0
 def test_eq(self):
     assert sql.Placeholder("foo") == sql.Placeholder("foo")
     assert sql.Placeholder("foo") != sql.Placeholder("bar")
     assert sql.Placeholder("foo") != "foo"
     assert sql.Placeholder() == sql.Placeholder()
     assert sql.Placeholder("foo") != sql.Placeholder()
     assert sql.Placeholder("foo") != sql.Literal("foo")
示例#5
0
    def test_must_be_adaptable(self, conn):
        class Foo(object):
            pass

        s = sql.SQL("select {0};").format(sql.Literal(Foo()))
        with pytest.raises(ProgrammingError):
            s.as_string(conn)
示例#6
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)
示例#7
0
def test_quote_1byte(conn):
    cur = conn.cursor()
    query = sql.SQL("select {ch} = %s::bytea")
    for i in range(0, 256):
        cur.execute(query.format(ch=sql.Literal(bytes([i]))),
                    (fr"\x{i:02x}", ))
        assert cur.fetchone()[0] is True, i
示例#8
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
示例#9
0
def test_quote_1char(conn):
    cur = conn.cursor()
    query = sql.SQL("select {ch} = chr(%s::int)")
    for i in range(1, 256):
        if chr(i) == "%":
            continue
        cur.execute(query.format(ch=sql.Literal(chr(i))), (i,))
        assert cur.fetchone()[0] is True, chr(i)
示例#10
0
    def test_join(self, conn):
        obj = sql.SQL(", ").join(
            [sql.Identifier("foo"), sql.SQL("bar"), sql.Literal(42)]
        )
        assert isinstance(obj, sql.Composed)
        assert obj.as_string(conn) == '"foo", bar, 42'

        obj = sql.SQL(", ").join(
            sql.Composed(
                [sql.Identifier("foo"), sql.SQL("bar"), sql.Literal(42)]
            )
        )
        assert isinstance(obj, sql.Composed)
        assert obj.as_string(conn) == '"foo", bar, 42'

        obj = sql.SQL(", ").join([])
        assert obj == sql.Composed([])
示例#11
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
示例#12
0
def test_dump_date(conn, val, expr):
    val = as_date(val)
    cur = conn.cursor()
    cur.execute(f"select '{expr}'::date = %s", (val,))
    assert cur.fetchone()[0] is True

    cur.execute(
        sql.SQL("select {val}::date = %s").format(val=sql.Literal(val)), (val,)
    )
    assert cur.fetchone()[0] is True
示例#13
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)
示例#14
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)
示例#15
0
def test_quote_zero(conn):
    cur = conn.cursor()
    s = "foo\x00bar"
    with pytest.raises(psycopg3.DataError):
        cur.execute(sql.SQL("select {}").format(sql.Literal(s)))
示例#16
0
def test_minus_minus_quote(conn, pgtype):
    cur = conn.cursor()
    cast = f"::{pgtype}" if pgtype is not None else ""
    cur.execute(sql.SQL("select -{}{}").format(sql.Literal(-1), sql.SQL(cast)))
    result = cur.fetchone()[0]
    assert result == 1
示例#17
0
 def test_braces_escape(self, conn):
     s = sql.SQL("{{{0}}}").format(sql.Literal(7))
     assert s.as_string(conn) == "{7}"
     s = sql.SQL("{{1,{0}}}").format(sql.Literal(7))
     assert s.as_string(conn) == "{1,7}"
示例#18
0
 def test_eq(self):
     assert sql.SQL("foo") == sql.SQL("foo")
     assert sql.SQL("foo") != sql.SQL("bar")
     assert sql.SQL("foo") != "foo"
     assert sql.SQL("foo") != sql.Literal("foo")
示例#19
0
 def test_compose_literal(self, conn):
     s = sql.SQL("select {0};").format(sql.Literal(dt.date(2016, 12, 31)))
     s1 = s.as_string(conn)
     assert s1 == "select '2016-12-31';"
示例#20
0
 def test_init(self):
     assert isinstance(sql.Literal("foo"), sql.Literal)
     assert isinstance(sql.Literal(u"foo"), sql.Literal)
     assert isinstance(sql.Literal(b"foo"), sql.Literal)
     assert isinstance(sql.Literal(42), sql.Literal)
     assert isinstance(sql.Literal(dt.date(2016, 12, 31)), sql.Literal)
示例#21
0
 def test_repr(self):
     assert repr(sql.Literal("foo")) == "Literal('foo')"
     assert str(sql.Literal("foo")) == "Literal('foo')"
示例#22
0
 def test_eq(self):
     L = [sql.Literal("foo"), sql.Identifier("b'ar")]
     l2 = [sql.Literal("foo"), sql.Literal("b'ar")]
     assert sql.Composed(L) == sql.Composed(list(L))
     assert sql.Composed(L) != L
     assert sql.Composed(L) != sql.Composed(l2)
示例#23
0
 def test_repr(self):
     obj = sql.Composed([sql.Literal("foo"), sql.Identifier("b'ar")])
     assert (
         repr(obj) == """Composed([Literal('foo'), Identifier("b'ar")])"""
     )
     assert str(obj) == repr(obj)
示例#24
0
 def test_percent_escape(self, conn):
     s = sql.SQL("42 % {0}").format(sql.Literal(7))
     s1 = s.as_string(conn)
     assert s1 == "42 % 7"
示例#25
0
 def test_join(self, conn):
     obj = sql.Composed([sql.Literal("foo"), sql.Identifier("b'ar")])
     obj = obj.join(", ")
     assert isinstance(obj, sql.Composed)
     assert noe(obj.as_string(conn)) == "'foo', \"b'ar\""
示例#26
0
    def test_must_be_adaptable(self, conn):
        class Foo(object):
            pass

        with pytest.raises(ProgrammingError):
            sql.Literal(Foo()).as_string(conn)