Example #1
0
 def test_auto_literal(self, conn):
     s = sql.SQL("select {}, {}, {}").format(
         "he'lo", 10, dt.date(2020, 1, 1)
     )
     assert s.as_string(conn) == "select 'he''lo', 10, '2020-01-01'"
Example #2
0
 def test_eq(self):
     assert sql.Identifier("foo") == sql.Identifier("foo")
     assert sql.Identifier("foo", "bar") == sql.Identifier("foo", "bar")
     assert sql.Identifier("foo") != sql.Identifier("bar")
     assert sql.Identifier("foo") != "foo"
     assert sql.Identifier("foo") != sql.SQL("foo")
Example #3
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
Example #4
0
 def test_no_modifiers(self):
     with pytest.raises(ValueError):
         sql.SQL("select {a!r};").format(a=10)
     with pytest.raises(ValueError):
         sql.SQL("select {a:<};").format(a=10)
Example #5
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';"
Example #6
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)))
Example #7
0
 def select_stmt(self):
     fields = sql.SQL(", ").join(self.fields_names)
     return sql.SQL("select {} from {} order by id").format(
         fields, self.table_name)
Example #8
0
 def test_sum_inplace(self, conn):
     obj = sql.SQL("foo")
     obj += sql.SQL("bar")
     assert isinstance(obj, sql.Composed)
     assert obj.as_string(conn) == "foobar"
Example #9
0
 def test_compose_badnargs(self):
     with pytest.raises(IndexError):
         sql.SQL("select {0};").format()
Example #10
0
 def test_compose_bad_args_type(self):
     with pytest.raises(IndexError):
         sql.SQL("select {0};").format(a=10)
     with pytest.raises(KeyError):
         sql.SQL("select {x};").format(10)
Example #11
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}"
Example #12
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"
Example #13
0
 def test_compose_empty(self, conn):
     s = sql.SQL("select foo;").format()
     s1 = s.as_string(conn)
     assert s1 == "select foo;"
Example #14
0
 def test_repr(self, conn):
     assert repr(sql.SQL("foo")) == "SQL('foo')"
     assert str(sql.SQL("foo")) == "SQL('foo')"
     assert sql.SQL("foo").as_string(conn) == "foo"
Example #15
0
 def drop_stmt(self):
     return sql.SQL("drop table if exists {}").format(self.table_name)
Example #16
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")
Example #17
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
Example #18
0
 def test_multiply(self, conn):
     obj = sql.SQL("foo") * 3
     assert isinstance(obj, sql.Composed)
     assert obj.as_string(conn) == "foofoofoo"
Example #19
0
 def test_sum(self, conn):
     obj = sql.Composed([sql.SQL("foo ")])
     obj = obj + sql.Literal("bar")
     assert isinstance(obj, sql.Composed)
     assert noe(obj.as_string(conn)) == "foo 'bar'"