Esempio n. 1
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")
Esempio n. 2
0
 def insert_stmt(self):
     phs = [
         sql.Placeholder(format=self.format)
         for i in range(len(self.schema))
     ]
     return sql.SQL("insert into {} ({}) values ({})").format(
         self.table_name,
         sql.SQL(", ").join(self.fields_names),
         sql.SQL(", ").join(phs),
     )
Esempio n. 3
0
    def test_executemany(self, conn):
        cur = conn.cursor()
        cur.execute(
            """
            create table test_compose (
                id serial primary key,
                foo text, bar text, "ba'z" text)
            """
        )
        cur.executemany(
            sql.SQL("insert into {0} (id, {1}) values (%s, {2})").format(
                sql.Identifier("test_compose"),
                sql.SQL(", ").join(
                    map(sql.Identifier, ["foo", "bar", "ba'z"])
                ),
                (sql.Placeholder() * 3).join(", "),
            ),
            [(10, "a", "b", "c"), (20, "d", "e", "f")],
        )

        cur.execute("select * from test_compose")
        assert cur.fetchall() == [(10, "a", "b", "c"), (20, "d", "e", "f")]
Esempio n. 4
0
 def test_bad_name(self):
     with pytest.raises(ValueError):
         sql.Placeholder(")")
Esempio n. 5
0
 def test_repr_name_binary(self, conn):
     ph = sql.Placeholder("foo", format=Format.BINARY)
     assert str(ph) == repr(ph) == "Placeholder('foo', format=BINARY)"
     assert ph.as_string(conn) == "%(foo)b"
Esempio n. 6
0
 def test_repr_name(self, conn):
     ph = sql.Placeholder("foo")
     assert str(ph) == repr(ph) == "Placeholder('foo')"
     assert ph.as_string(conn) == "%(foo)s"
Esempio n. 7
0
 def test_repr_binary(self, conn):
     ph = sql.Placeholder(format=Format.BINARY)
     assert str(ph) == repr(ph) == "Placeholder(format=BINARY)"
     assert ph.as_string(conn) == "%b"
Esempio n. 8
0
 def test_repr(self, conn):
     ph = sql.Placeholder()
     assert str(ph) == repr(ph) == "Placeholder()"
     assert ph.as_string(conn) == "%s"