File: test_tstring.py

package info (click to toggle)
psycopg3 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,836 kB
  • sloc: python: 46,657; sh: 403; ansic: 149; makefile: 73
file content (264 lines) | stat: -rw-r--r-- 8,643 bytes parent folder | download
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
from random import random

import pytest

import psycopg
from psycopg import sql
from psycopg.pq import Format

from .acompat import alist
from .test_sql import no_e
from .test_adapt import make_dumper

vstr = "hello"
vint = 16


async def test_connection_no_params(aconn):
    with pytest.raises(TypeError):
        await aconn.execute(t"select 1", [])    # noqa: F542


async def test_cursor_no_params(aconn):
    cur = aconn.cursor()
    with pytest.raises(TypeError):
        await cur.execute(t"select 1", [])  # noqa: F542


async def test_connection_execute(aconn):
    cur = await aconn.execute(t"select {vstr}")
    assert await cur.fetchone() == ("hello",)
    assert cur._query.query == b"select $1"
    assert cur._query.params == [b"hello"]
    assert cur._query.types == (0,)


@pytest.mark.parametrize(
    "t", [t"select {vstr!a}", t"select {vstr!r}", t"select {vstr!s}"]
)
async def test_no_conversion(aconn, t):
    with pytest.raises(TypeError):
        await aconn.execute(t)


@pytest.mark.parametrize(
    "t, fmt",
    [
        (t"select {vint}", Format.BINARY),
        (t"select {vint:s}", Format.BINARY),
        (t"select {vint:t}", Format.TEXT),
        (t"select {vint:b}", Format.BINARY),
    ],
)
async def test_format(aconn, t, fmt):
    cur = await aconn.execute(t)
    assert await cur.fetchone() == (16,)
    assert cur._query.query == b"select $1"
    assert cur._query.types == (psycopg.adapters.types["smallint"].oid,)
    assert cur._query.params == [b"\x00\x10" if fmt == Format.BINARY else b"16"]
    assert cur._query.formats == [fmt]


async def test_format_bad(aconn):
    with pytest.raises(psycopg.ProgrammingError, match="format 'x' not supported"):
        await aconn.execute(t"select {vint:x}")


async def test_expression(aconn):
    cur = await aconn.execute(t"select {vint * 2}")
    assert await cur.fetchone() == (32,)
    assert cur._query.query == b"select $1"
    assert cur._query.types == (psycopg.adapters.types["smallint"].oid,)
    assert cur._query.params == [b"\x00\x20"]
    assert cur._query.formats == [Format.BINARY]


async def test_format_identifier(aconn):
    f1 = "foo-bar"
    f2 = "baz"
    cur = await aconn.execute(t"select {vint} as {f1:i}, {vint * 2:t} as {f2:i}")
    assert await cur.fetchone() == (16, 32)
    assert cur._query.query == b'select $1 as "foo-bar", $2 as "baz"'
    assert cur._query.types == (psycopg.adapters.types["smallint"].oid,) * 2
    assert cur._query.params == [b"\x00\x10", b"32"]
    assert cur._query.formats == [Format.BINARY, Format.TEXT]


async def test_format_literal(aconn):
    f1 = "foo-bar"
    f2 = "baz"
    cur = await aconn.execute(t"select {vint * 2:l} as {f1:i}, {vint:t} as {f2:i}")
    assert await cur.fetchone() == (32, 16)
    assert cur._query.query == b'select 32 as "foo-bar", $1 as "baz"'
    assert cur._query.types == (psycopg.adapters.types["smallint"].oid,)
    assert cur._query.params == [b"16"]
    assert cur._query.formats == [Format.TEXT]


async def test_nested(aconn):
    part = t"{vint} as foo"
    cur = await aconn.execute(t"select {part:q}")
    assert await cur.fetchone() == (16,)
    assert cur._query.query == b"select $1 as foo"
    assert cur._query.types == (psycopg.adapters.types["smallint"].oid,)
    assert cur._query.params == [b"\x00\x10"]
    assert cur._query.formats == [Format.BINARY]

    with pytest.raises(psycopg.ProgrammingError, match="Template.*':q'"):
        cur = await aconn.execute(t"select {part}")


async def test_scope(aconn):
    t = t"select "  # noqa: F542
    for i, name in enumerate(("foo", "bar", "baz")):
        if i:
            t += t", "  # noqa: F542
        t += t"{i} as {name:i}"

    cur = await aconn.execute(t)
    assert await cur.fetchone() == (0, 1, 2)
    assert cur.description[0].name == "foo"
    assert cur.description[2].name == "baz"


async def test_no_reuse(aconn):
    t = t"select {vint}, {vint}"
    cur = await aconn.execute(t)
    assert await cur.fetchone() == (vint, vint)
    assert b"$2" in cur._query.query


async def test_volatile(aconn):
    t = t"select {random()}, {random()}"
    cur = await aconn.execute(t)
    rec = await cur.fetchone()
    assert rec[0] != rec[1]
    assert b"$2" in cur._query.query


async def test_sql(aconn):
    part = sql.SQL("foo")
    cur = await aconn.execute(t"select {vint} as {part:q}")
    assert await cur.fetchone() == (16,)
    assert cur._query.query == b"select $1 as foo"

    with pytest.raises(psycopg.ProgrammingError, match=r"sql\.SQL.*':q'"):
        await aconn.execute(t"select {vint} as {part:i}")


async def test_sql_composed(aconn):
    part = sql.SQL("{} as {}").format(vint, sql.Identifier("foo"))
    cur = await aconn.execute(t"select {part:q}")
    assert await cur.fetchone() == (16,)
    assert cur._query.query == b'select 16 as "foo"'

    with pytest.raises(psycopg.ProgrammingError, match=r"sql\.Composed.*':q'"):
        await aconn.execute(t"select {part}")


async def test_sql_identifier(aconn):
    part = sql.Identifier("foo")
    cur = await aconn.execute(t"select {vint} as {part:i}")
    assert await cur.fetchone() == (16,)
    assert cur._query.query == b'select $1 as "foo"'

    with pytest.raises(psycopg.ProgrammingError, match=r"sql\.Identifier.*':i'"):
        await aconn.execute(t"select {vint} as {part}")


async def test_sql_literal(aconn):
    lit = sql.Literal(42)
    cur = await aconn.execute(t"select {lit:l} as foo")
    assert await cur.fetchone() == (42,)
    assert cur._query.query == b'select 42 as foo'

    with pytest.raises(psycopg.ProgrammingError, match=r"sql\.Literal.*':l'"):
        await aconn.execute(t"select {lit} as foo")


async def test_sql_placeholder(aconn):
    part = sql.Placeholder("foo")
    with pytest.raises(psycopg.ProgrammingError, match="Placeholder not supported"):
        await aconn.execute(t"select {part}")


@pytest.mark.xfail(reason="Template.join() needed")
async def test_template_join(aconn):
    ts = [t"{i} as {name:i}" for i, name in enumerate(("foo", "bar", "baz"))]
    fields = t','.join(ts)  # noqa: F542
    cur = await aconn.execute(t"select {fields}")
    assert await cur.fetchone() == (0, 1, 2)
    assert cur.description[0].name == "foo"
    assert cur.description[2].name == "baz"


async def test_sql_join(aconn):
    ts = [t"{i} as {name:i}" for i, name in enumerate(("foo", "bar", "baz"))]
    fields = sql.SQL(',').join(ts)
    cur = await aconn.execute(t"select {fields:q}")
    assert await cur.fetchone() == (0, 1, 2)
    assert cur.description[0].name == "foo"
    assert cur.description[2].name == "baz"


async def test_copy(aconn):
    cur = aconn.cursor()
    async with cur.copy(
        t"copy (select * from generate_series(1, {3})) to stdout"
    ) as copy:
        data = await alist(copy.rows())
    assert data == [("1",), ("2",), ("3",)]


async def test_client_cursor(aconn):
    cur = psycopg.AsyncClientCursor(aconn)
    await cur.execute(t"select {vint}, {vstr} as {vstr:i}")
    assert await cur.fetchone() == (vint, vstr)
    assert cur.description[1].name == vstr
    assert str(vint) in cur._query.query.decode()
    assert str(vint) == cur._query.params[0].decode()
    assert f"'{vstr}'" in cur._query.query.decode()
    assert f"'{vstr}'" in cur._query.params[1].decode()


async def test_mogrify(aconn):
    cur = psycopg.AsyncClientCursor(aconn)
    res = cur.mogrify(t"select {vint}, {vstr} as {vstr:i}")
    assert res == "select 16, 'hello' as \"hello\""


async def test_raw_cursor(aconn):
    cur = psycopg.AsyncRawCursor(aconn)
    with pytest.raises(psycopg.NotSupportedError):
        await cur.execute(t"select {vint}, {vstr} as {vstr:i}")


async def test_server_cursor(aconn):
    async with psycopg.AsyncServerCursor(aconn, "test") as cur:
        await cur.execute(t"select {vint}, {vstr} as {vstr:i}")
        assert await cur.fetchone() == (vint, vstr)
        assert cur.description[1].name == vstr
        assert b"$2" in cur._query.query
        assert b"$3" not in cur._query.query


def test_as_string():
    query = sql.as_string(t"select {vstr}")
    assert query == no_e("select 'hello'")


async def test_as_string_context(aconn):
    aconn.adapters.register_dumper(str, make_dumper("1"))
    query = sql.as_string(t"select {vstr}", context=aconn)
    assert query == no_e("select 'hello1'")


def test_as_bytes():
    query = sql.as_bytes(t"select {vstr}")
    assert query == no_e(b"select 'hello'")


async def test_as_bytes_context(aconn):
    aconn.adapters.register_dumper(str, make_dumper("1"))
    query = sql.as_bytes(t"select {vstr}", context=aconn)
    assert query == no_e(b"select 'hello1'")