summaryrefslogtreecommitdiffstats
path: root/tests/test_sql.py
blob: 42b6c63ce5c7e7c0234eeb8509f0c505376ca3f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# test_sql.py - tests for the psycopg2.sql module

# Copyright (C) 2020 The Psycopg Team

import re
import datetime as dt

import pytest

from psycopg import pq, sql, ProgrammingError
from psycopg.adapt import PyFormat
from psycopg._encodings import py2pgenc
from psycopg.types import TypeInfo
from psycopg.types.string import StrDumper

from .utils import eur
from .fix_crdb import crdb_encoding, crdb_scs_off


@pytest.mark.parametrize(
    "obj, quoted",
    [
        ("foo\\bar", " E'foo\\\\bar'"),
        ("hello", "'hello'"),
        (42, "42"),
        (True, "true"),
        (None, "NULL"),
    ],
)
def test_quote(obj, quoted):
    assert sql.quote(obj) == quoted


@pytest.mark.parametrize("scs", ["on", crdb_scs_off("off")])
def test_quote_roundtrip(conn, scs):
    messages = []
    conn.add_notice_handler(lambda msg: messages.append(msg.message_primary))
    conn.execute(f"set standard_conforming_strings to {scs}")

    for i in range(1, 256):
        want = chr(i)
        quoted = sql.quote(want)
        got = conn.execute(f"select {quoted}::text").fetchone()[0]
        assert want == got

        # No "nonstandard use of \\ in a string literal" warning
        assert not messages, f"error with {want!r}"


@pytest.mark.parametrize("dummy", [crdb_scs_off("off")])
def test_quote_stable_despite_deranged_libpq(conn, dummy):
    # Verify the libpq behaviour of PQescapeString using the last setting seen.
    # Check that we are not affected by it.
    good_str = " E'\\\\'"
    good_bytes = " E'\\\\000'::bytea"
    conn.execute("set standard_conforming_strings to on")
    assert pq.Escaping().escape_string(b"\\") == b"\\"
    assert sql.quote("\\") == good_str
    assert pq.Escaping().escape_bytea(b"\x00") == b"\\000"
    assert sql.quote(b"\x00") == good_bytes

    conn.execute("set standard_conforming_strings to off")
    assert pq.Escaping().escape_string(b"\\") == b"\\\\"
    assert sql.quote("\\") == good_str
    assert pq.Escaping().escape_bytea(b"\x00") == b"\\\\000"
    assert sql.quote(b"\x00") == good_bytes

    # Verify that the good values are actually good
    messages = []
    conn.add_notice_handler(lambda msg: messages.append(msg.message_primary))
    conn.execute("set escape_string_warning to on")
    for scs in ("on", "off"):
        conn.execute(f"set standard_conforming_strings to {scs}")
        cur = conn.execute(f"select {good_str}, {good_bytes}::bytea")
        assert cur.fetchone() == ("\\", b"\x00")

    # No "nonstandard use of \\ in a string literal" warning
    assert not messages


class TestSqlFormat:
    def test_pos(self, conn):
        s = sql.SQL("select {} from {}").format(
            sql.Identifier("field"), sql.Identifier("table")
        )
        s1 = s.as_string(conn)
        assert isinstance(s1, str)
        assert s1 == 'select "field" from "table"'

    def test_pos_spec(self, conn):
        s = sql.SQL("select {0} from {1}").format(
            sql.Identifier("field"), sql.Identifier("table")
        )
        s1 = s.as_string(conn)
        assert isinstance(s1, str)
        assert s1 == 'select "field" from "table"'

        s = sql.SQL("select {1} from {0}").format(
            sql.Identifier("table"), sql.Identifier("field")
        )
        s1 = s.as_string(conn)
        assert isinstance(s1, str)
        assert s1 == 'select "field" from "table"'

    def test_dict(self, conn):
        s = sql.SQL("select {f} from {t}").format(
            f=sql.Identifier("field"), t=sql.Identifier("table")
        )
        s1 = s.as_string(conn)
        assert isinstance(s1, str)
        assert s1 == 'select "field" from "table"'

    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'::date;"

    def test_compose_empty(self, conn):
        s = sql.SQL("select foo;").format()
        s1 = s.as_string(conn)
        assert s1 == "select foo;"

    def test_percent_escape(self, conn):
        s = sql.SQL("42 % {0}").format(sql.Literal(7))
        s1 = s.as_string(conn)
        assert s1 == "42 % 7"

    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}"

    def test_compose_badnargs(self):
        with pytest.raises(IndexError):
            sql.SQL("select {0};").format()

    def test_compose_badnargs_auto(self):
        with pytest.raises(IndexError):
            sql.SQL("select {};").format()
        with pytest.raises(ValueError):
            sql.SQL("select {} {1};").format(10, 20)
        with pytest.raises(ValueError):
            sql.SQL("select {0} {};").format(10, 20)

    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)

    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)

    def test_must_be_adaptable(self, conn):
        class Foo:
            pass

        s = sql.SQL("select {0};").format(sql.Literal(Foo()))
        with pytest.raises(ProgrammingError):
            s.as_string(conn)

    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'::date"

    def test_execute(self, conn):
        cur = conn.cursor()
        cur.execute(
            """
            create table test_compose (
                id serial primary key,
                foo text, bar text, "ba'z" text)
            """
        )
        cur.execute(
            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"),
        )

        cur.execute("select * from test_compose")
        assert cur.fetchall() == [(10, "a", "b", "c")]

    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")]

    @pytest.mark.crdb_skip("copy")
    def test_copy(self, conn):
        cur = conn.cursor()
        cur.execute(
            """
            create table test_compose (
                id serial primary key,
                foo text, bar text, "ba'z" text)
            """
        )

        with cur.copy(
            sql.SQL("copy {t} (id, foo, bar, {f}) from stdin").format(
                t=sql.Identifier("test_compose"), f=sql.Identifier("ba'z")
            ),
        ) as copy:
            copy.write_row((10, "a", "b", "c"))
            copy.write_row((20, "d", "e", "f"))

        with cur.copy(
            sql.SQL("copy (select {f} from {t} order by id) to stdout").format(
                t=sql.Identifier("test_compose"), f=sql.Identifier("ba'z")
            )
        ) as copy:
            assert list(copy) == [b"c\n", b"f\n"]


class TestIdentifier:
    def test_class(self):
        assert issubclass(sql.Identifier, sql.Composable)

    def test_init(self):
        assert isinstance(sql.Identifier("foo"), sql.Identifier)
        assert isinstance(sql.Identifier("foo"), sql.Identifier)
        assert isinstance(sql.Identifier("foo", "bar", "baz"), sql.Identifier)
        with pytest.raises(TypeError):
            sql.Identifier()
        with pytest.raises(TypeError):
            sql.Identifier(10)  # type: ignore[arg-type]
        with pytest.raises(TypeError):
            sql.Identifier(dt.date(2016, 12, 31))  # type: ignore[arg-type]

    def test_repr(self):
        obj = sql.Identifier("fo'o")
        assert repr(obj) == 'Identifier("fo\'o")'
        assert repr(obj) == str(obj)

        obj = sql.Identifier("fo'o", 'ba"r')
        assert repr(obj) == "Identifier(\"fo'o\", 'ba\"r')"
        assert repr(obj) == str(obj)

    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")

    @pytest.mark.parametrize(
        "args, want",
        [
            (("foo",), '"foo"'),
            (("foo", "bar"), '"foo"."bar"'),
            (("fo'o", 'ba"r'), '"fo\'o"."ba""r"'),
        ],
    )
    def test_as_string(self, conn, args, want):
        assert sql.Identifier(*args).as_string(conn) == want

    @pytest.mark.parametrize(
        "args, want, enc",
        [
            crdb_encoding(("foo",), '"foo"', "ascii"),
            crdb_encoding(("foo", "bar"), '"foo"."bar"', "ascii"),
            crdb_encoding(("fo'o", 'ba"r'), '"fo\'o"."ba""r"', "ascii"),
            (("foo", eur), f'"foo"."{eur}"', "utf8"),
            crdb_encoding(("foo", eur), f'"foo"."{eur}"', "latin9"),
        ],
    )
    def test_as_bytes(self, conn, args, want, enc):
        want = want.encode(enc)
        conn.execute(f"set client_encoding to {py2pgenc(enc).decode()}")
        assert sql.Identifier(*args).as_bytes(conn) == want

    def test_join(self):
        assert not hasattr(sql.Identifier("foo"), "join")


class TestLiteral:
    def test_class(self):
        assert issubclass(sql.Literal, sql.Composable)

    def test_init(self):
        assert isinstance(sql.Literal("foo"), sql.Literal)
        assert isinstance(sql.Literal("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)

    def test_repr(self):
        assert repr(sql.Literal("foo")) == "Literal('foo')"
        assert str(sql.Literal("foo")) == "Literal('foo')"

    def test_as_string(self, conn):
        assert sql.Literal(None).as_string(conn) == "NULL"
        assert no_e(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'::date"

    def test_as_bytes(self, conn):
        assert sql.Literal(None).as_bytes(conn) == b"NULL"
        assert no_e(sql.Literal("foo").as_bytes(conn)) == b"'foo'"
        assert sql.Literal(42).as_bytes(conn) == b"42"
        assert sql.Literal(dt.date(2017, 1, 1)).as_bytes(conn) == b"'2017-01-01'::date"

    @pytest.mark.parametrize("encoding", ["utf8", crdb_encoding("latin9")])
    def test_as_bytes_encoding(self, conn, encoding):
        conn.execute(f"set client_encoding to {encoding}")
        assert sql.Literal(eur).as_bytes(conn) == f"'{eur}'".encode(encoding)

    def test_eq(self):
        assert sql.Literal("foo") == sql.Literal("foo")
        assert sql.Literal("foo") != sql.Literal("bar")
        assert sql.Literal("foo") != "foo"
        assert sql.Literal("foo") != sql.SQL("foo")

    def test_must_be_adaptable(self, conn):
        class Foo:
            pass

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

    def test_array(self, conn):
        assert (
            sql.Literal([dt.date(2000, 1, 1)]).as_string(conn)
            == "'{2000-01-01}'::date[]"
        )

    def test_short_name_builtin(self, conn):
        assert sql.Literal(dt.time(0, 0)).as_string(conn) == "'00:00:00'::time"
        assert (
            sql.Literal(dt.datetime(2000, 1, 1)).as_string(conn)
            == "'2000-01-01 00:00:00'::timestamp"
        )
        assert (
            sql.Literal([dt.datetime(2000, 1, 1)]).as_string(conn)
            == "'{\"2000-01-01 00:00:00\"}'::timestamp[]"
        )

    def test_text_literal(self, conn):
        conn.adapters.register_dumper(str, StrDumper)
        assert sql.Literal("foo").as_string(conn) == "'foo'"

    @pytest.mark.crdb_skip("composite")  # create type, actually
    @pytest.mark.parametrize("name", ["a-b", f"{eur}", "order", "foo bar"])
    def test_invalid_name(self, conn, name):
        conn.execute(
            f"""
            set client_encoding to utf8;
            create type "{name}";
            create function invin(cstring) returns "{name}"
                language internal immutable strict as 'textin';
            create function invout("{name}") returns cstring
                language internal immutable strict as 'textout';
            create type "{name}" (input=invin, output=invout, like=text);
            """
        )
        info = TypeInfo.fetch(conn, f'"{name}"')

        class InvDumper(StrDumper):
            oid = info.oid

            def dump(self, obj):
                rv = super().dump(obj)
                return b"%s-inv" % rv

        info.register(conn)
        conn.adapters.register_dumper(str, InvDumper)

        assert sql.Literal("hello").as_string(conn) == f"'hello-inv'::\"{name}\""
        cur = conn.execute(sql.SQL("select {}").format("hello"))
        assert cur.fetchone()[0] == "hello-inv"

        assert (
            sql.Literal(["hello"]).as_string(conn) == f"'{{hello-inv}}'::\"{name}\"[]"
        )
        cur = conn.execute(sql.SQL("select {}").format(["hello"]))
        assert cur.fetchone()[0] == ["hello-inv"]


class TestSQL:
    def test_class(self):
        assert issubclass(sql.SQL, sql.Composable)

    def test_init(self):
        assert isinstance(sql.SQL("foo"), sql.SQL)
        assert isinstance(sql.SQL("foo"), sql.SQL)
        with pytest.raises(TypeError):
            sql.SQL(10)  # type: ignore[arg-type]
        with pytest.raises(TypeError):
            sql.SQL(dt.date(2016, 12, 31))  # type: ignore[arg-type]

    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"

    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")

    def test_sum(self, conn):
        obj = sql.SQL("foo") + sql.SQL("bar")
        assert isinstance(obj, sql.Composed)
        assert obj.as_string(conn) == "foobar"

    def test_sum_inplace(self, conn):
        obj = sql.SQL("f") + sql.SQL("oo")
        obj += sql.SQL("bar")
        assert isinstance(obj, sql.Composed)
        assert obj.as_string(conn) == "foobar"

    def test_multiply(self, conn):
        obj = sql.SQL("foo") * 3
        assert isinstance(obj, sql.Composed)
        assert obj.as_string(conn) == "foofoofoo"

    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([])

    def test_as_string(self, conn):
        assert sql.SQL("foo").as_string(conn) == "foo"

    @pytest.mark.parametrize("encoding", ["utf8", crdb_encoding("latin9")])
    def test_as_bytes(self, conn, encoding):
        if encoding:
            conn.execute(f"set client_encoding to {encoding}")

        assert sql.SQL(eur).as_bytes(conn) == eur.encode(encoding)


class TestComposed:
    def test_class(self):
        assert issubclass(sql.Composed, sql.Composable)

    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)

    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)

    def test_join(self, conn):
        obj = sql.Composed([sql.Literal("foo"), sql.Identifier("b'ar")])
        obj = obj.join(", ")
        assert isinstance(obj, sql.Composed)
        assert no_e(obj.as_string(conn)) == "'foo', \"b'ar\""

    def test_auto_literal(self, conn):
        obj = sql.Composed(["fo'o", dt.date(2020, 1, 1)])
        obj = obj.join(", ")
        assert isinstance(obj, sql.Composed)
        assert no_e(obj.as_string(conn)) == "'fo''o', '2020-01-01'::date"

    def test_sum(self, conn):
        obj = sql.Composed([sql.SQL("foo ")])
        obj = obj + sql.Literal("bar")
        assert isinstance(obj, sql.Composed)
        assert no_e(obj.as_string(conn)) == "foo 'bar'"

    def test_sum_inplace(self, conn):
        obj = sql.Composed([sql.SQL("foo ")])
        obj += sql.Literal("bar")
        assert isinstance(obj, sql.Composed)
        assert no_e(obj.as_string(conn)) == "foo 'bar'"

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

    def test_iter(self):
        obj = sql.Composed([sql.SQL("foo"), sql.SQL("bar")])
        it = iter(obj)
        i = next(it)
        assert i == sql.SQL("foo")
        i = next(it)
        assert i == sql.SQL("bar")
        with pytest.raises(StopIteration):
            next(it)

    def test_as_string(self, conn):
        obj = sql.Composed([sql.SQL("foo"), sql.SQL("bar")])
        assert obj.as_string(conn) == "foobar"

    def test_as_bytes(self, conn):
        obj = sql.Composed([sql.SQL("foo"), sql.SQL("bar")])
        assert obj.as_bytes(conn) == b"foobar"

    @pytest.mark.parametrize("encoding", ["utf8", crdb_encoding("latin9")])
    def test_as_bytes_encoding(self, conn, encoding):
        obj = sql.Composed([sql.SQL("foo"), sql.SQL(eur)])
        conn.execute(f"set client_encoding to {encoding}")
        assert obj.as_bytes(conn) == ("foo" + eur).encode(encoding)


class TestPlaceholder:
    def test_class(self):
        assert issubclass(sql.Placeholder, sql.Composable)

    @pytest.mark.parametrize("format", PyFormat)
    def test_repr_format(self, conn, format):
        ph = sql.Placeholder(format=format)
        add = f"format={format.name}" if format != PyFormat.AUTO else ""
        assert str(ph) == repr(ph) == f"Placeholder({add})"

    @pytest.mark.parametrize("format", PyFormat)
    def test_repr_name_format(self, conn, format):
        ph = sql.Placeholder("foo", format=format)
        add = f", format={format.name}" if format != PyFormat.AUTO else ""
        assert str(ph) == repr(ph) == f"Placeholder('foo'{add})"

    def test_bad_name(self):
        with pytest.raises(ValueError):
            sql.Placeholder(")")

    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")

    @pytest.mark.parametrize("format", PyFormat)
    def test_as_string(self, conn, format):
        ph = sql.Placeholder(format=format)
        assert ph.as_string(conn) == f"%{format.value}"

        ph = sql.Placeholder(name="foo", format=format)
        assert ph.as_string(conn) == f"%(foo){format.value}"

    @pytest.mark.parametrize("format", PyFormat)
    def test_as_bytes(self, conn, format):
        ph = sql.Placeholder(format=format)
        assert ph.as_bytes(conn) == f"%{format.value}".encode("ascii")

        ph = sql.Placeholder(name="foo", format=format)
        assert ph.as_bytes(conn) == f"%(foo){format.value}".encode("ascii")


class TestValues:
    def test_null(self, conn):
        assert isinstance(sql.NULL, sql.SQL)
        assert sql.NULL.as_string(conn) == "NULL"

    def test_default(self, conn):
        assert isinstance(sql.DEFAULT, sql.SQL)
        assert sql.DEFAULT.as_string(conn) == "DEFAULT"


def no_e(s):
    """Drop an eventual E from E'' quotes"""
    if isinstance(s, memoryview):
        s = bytes(s)

    if isinstance(s, str):
        return re.sub(r"\bE'", "'", s)
    elif isinstance(s, bytes):
        return re.sub(rb"\bE'", b"'", s)
    else:
        raise TypeError(f"not dealing with {type(s).__name__}: {s}")