From: Daniele Varrazzo Date: Wed, 28 Oct 2020 03:14:42 +0000 (+0100) Subject: Added sql adaptation from None to NULL X-Git-Tag: 3.0.dev0~424^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f4f44c951b08927ba073a484290c4a130147245a;p=thirdparty%2Fpsycopg.git Added sql adaptation from None to NULL This is a special case now, because None is no more a value to adapt, as it's not passed to PQexecParams like other values are. --- diff --git a/psycopg3/psycopg3/sql.py b/psycopg3/psycopg3/sql.py index 434f2ed2e..2dc0cb737 100644 --- a/psycopg3/psycopg3/sql.py +++ b/psycopg3/psycopg3/sql.py @@ -368,6 +368,9 @@ class Literal(Composable): """ def as_string(self, context: AdaptContext) -> str: + if self._obj is None: + return "NULL" + from .adapt import _connection_from_context, Transformer conn = _connection_from_context(context) diff --git a/tests/test_sql.py b/tests/test_sql.py index 6c95516b3..267bd4fd5 100755 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -10,6 +10,14 @@ import pytest from psycopg3 import sql, ProgrammingError +@pytest.mark.parametrize( + "obj, quoted", + [("hello", "'hello'"), (42, "'42'"), (True, "'t'"), (None, "NULL")], +) +def test_quote(obj, quoted): + assert sql.quote(obj) == quoted + + class TestSqlFormat: def test_pos(self, conn): s = sql.SQL("select {} from {}").format( @@ -234,9 +242,12 @@ class TestLiteral: assert isinstance(sql.Literal(42), sql.Literal) assert isinstance(sql.Literal(dt.date(2016, 12, 31)), sql.Literal) - def test_repr(self, conn): + def test_repr(self): assert repr(sql.Literal("foo")) == "Literal('foo')" assert str(sql.Literal("foo")) == "Literal('foo')" + + 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 (