summaryrefslogtreecommitdiffstats
path: root/tests/test_prepared.py
blob: 56c580a9f0a8c2f937bee7e630496fe315ccfcb2 (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
"""
Prepared statements tests
"""

import datetime as dt
from decimal import Decimal

import pytest

from psycopg.rows import namedtuple_row


@pytest.mark.parametrize("value", [None, 0, 3])
def test_prepare_threshold_init(conn_cls, dsn, value):
    with conn_cls.connect(dsn, prepare_threshold=value) as conn:
        assert conn.prepare_threshold == value


def test_dont_prepare(conn):
    cur = conn.cursor()
    for i in range(10):
        cur.execute("select %s::int", [i], prepare=False)

    stmts = get_prepared_statements(conn)
    assert len(stmts) == 0


def test_do_prepare(conn):
    cur = conn.cursor()
    cur.execute("select %s::int", [10], prepare=True)
    stmts = get_prepared_statements(conn)
    assert len(stmts) == 1


def test_auto_prepare(conn):
    res = []
    for i in range(10):
        conn.execute("select %s::int", [0])
        stmts = get_prepared_statements(conn)
        res.append(len(stmts))

    assert res == [0] * 5 + [1] * 5


def test_dont_prepare_conn(conn):
    for i in range(10):
        conn.execute("select %s::int", [i], prepare=False)

    stmts = get_prepared_statements(conn)
    assert len(stmts) == 0


def test_do_prepare_conn(conn):
    conn.execute("select %s::int", [10], prepare=True)
    stmts = get_prepared_statements(conn)
    assert len(stmts) == 1


def test_auto_prepare_conn(conn):
    res = []
    for i in range(10):
        conn.execute("select %s", [0])
        stmts = get_prepared_statements(conn)
        res.append(len(stmts))

    assert res == [0] * 5 + [1] * 5


def test_prepare_disable(conn):
    conn.prepare_threshold = None
    res = []
    for i in range(10):
        conn.execute("select %s", [0])
        stmts = get_prepared_statements(conn)
        res.append(len(stmts))

    assert res == [0] * 10
    assert not conn._prepared._names
    assert not conn._prepared._counts


def test_no_prepare_multi(conn):
    res = []
    for i in range(10):
        conn.execute("select 1; select 2")
        stmts = get_prepared_statements(conn)
        res.append(len(stmts))

    assert res == [0] * 10


def test_no_prepare_multi_with_drop(conn):
    conn.execute("select 1", prepare=True)

    for i in range(10):
        conn.execute("drop table if exists noprep; create table noprep()")

    stmts = get_prepared_statements(conn)
    assert len(stmts) == 0


def test_no_prepare_error(conn):
    conn.autocommit = True
    for i in range(10):
        with pytest.raises(conn.ProgrammingError):
            conn.execute("select wat")

    stmts = get_prepared_statements(conn)
    assert len(stmts) == 0


@pytest.mark.parametrize(
    "query",
    [
        "create table test_no_prepare ()",
        pytest.param("notify foo, 'bar'", marks=pytest.mark.crdb_skip("notify")),
        "set timezone = utc",
        "select num from prepared_test",
        "insert into prepared_test (num) values (1)",
        "update prepared_test set num = num * 2",
        "delete from prepared_test where num > 10",
    ],
)
def test_misc_statement(conn, query):
    conn.execute("create table prepared_test (num int)", prepare=False)
    conn.prepare_threshold = 0
    conn.execute(query)
    stmts = get_prepared_statements(conn)
    assert len(stmts) == 1


def test_params_types(conn):
    conn.execute(
        "select %s, %s, %s",
        [dt.date(2020, 12, 10), 42, Decimal(42)],
        prepare=True,
    )
    stmts = get_prepared_statements(conn)
    want = [stmt.parameter_types for stmt in stmts]
    assert want == [["date", "smallint", "numeric"]]


def test_evict_lru(conn):
    conn.prepared_max = 5
    for i in range(10):
        conn.execute("select 'a'")
        conn.execute(f"select {i}")

    assert len(conn._prepared._names) == 1
    assert conn._prepared._names[b"select 'a'", ()] == b"_pg3_0"
    for i in [9, 8, 7, 6]:
        assert conn._prepared._counts[f"select {i}".encode(), ()] == 1

    stmts = get_prepared_statements(conn)
    assert len(stmts) == 1
    assert stmts[0].statement == "select 'a'"


def test_evict_lru_deallocate(conn):
    conn.prepared_max = 5
    conn.prepare_threshold = 0
    for i in range(10):
        conn.execute("select 'a'")
        conn.execute(f"select {i}")

    assert len(conn._prepared._names) == 5
    for j in [9, 8, 7, 6, "'a'"]:
        name = conn._prepared._names[f"select {j}".encode(), ()]
        assert name.startswith(b"_pg3_")

    stmts = get_prepared_statements(conn)
    stmts.sort(key=lambda rec: rec.prepare_time)
    got = [stmt.statement for stmt in stmts]
    assert got == [f"select {i}" for i in ["'a'", 6, 7, 8, 9]]


def test_different_types(conn):
    conn.prepare_threshold = 0
    conn.execute("select %s", [None])
    conn.execute("select %s", [dt.date(2000, 1, 1)])
    conn.execute("select %s", [42])
    conn.execute("select %s", [41])
    conn.execute("select %s", [dt.date(2000, 1, 2)])

    stmts = get_prepared_statements(conn)
    stmts.sort(key=lambda rec: rec.prepare_time)
    got = [stmt.parameter_types for stmt in stmts]
    assert got == [["text"], ["date"], ["smallint"]]


def test_untyped_json(conn):
    conn.prepare_threshold = 1
    conn.execute("create table testjson(data jsonb)")

    for i in range(2):
        conn.execute("insert into testjson (data) values (%s)", ["{}"])

    stmts = get_prepared_statements(conn)
    got = [stmt.parameter_types for stmt in stmts]
    assert got == [["jsonb"]]


def test_change_type_execute(conn):
    conn.prepare_threshold = 0
    for i in range(3):
        conn.execute("CREATE TYPE prepenum AS ENUM ('foo', 'bar', 'baz')")
        conn.execute("CREATE TABLE preptable(id integer, bar prepenum[])")
        conn.cursor().execute(
            "INSERT INTO preptable (bar) VALUES (%(enum_col)s::prepenum[])",
            {"enum_col": ["foo"]},
        )
        conn.rollback()


def test_change_type_executemany(conn):
    for i in range(3):
        conn.execute("CREATE TYPE prepenum AS ENUM ('foo', 'bar', 'baz')")
        conn.execute("CREATE TABLE preptable(id integer, bar prepenum[])")
        conn.cursor().executemany(
            "INSERT INTO preptable (bar) VALUES (%(enum_col)s::prepenum[])",
            [{"enum_col": ["foo"]}, {"enum_col": ["foo", "bar"]}],
        )
        conn.rollback()


@pytest.mark.crdb("skip", reason="can't re-create a type")
def test_change_type(conn):
    conn.prepare_threshold = 0
    conn.execute("CREATE TYPE prepenum AS ENUM ('foo', 'bar', 'baz')")
    conn.execute("CREATE TABLE preptable(id integer, bar prepenum[])")
    conn.cursor().execute(
        "INSERT INTO preptable (bar) VALUES (%(enum_col)s::prepenum[])",
        {"enum_col": ["foo"]},
    )
    conn.execute("DROP TABLE preptable")
    conn.execute("DROP TYPE prepenum")
    conn.execute("CREATE TYPE prepenum AS ENUM ('foo', 'bar', 'baz')")
    conn.execute("CREATE TABLE preptable(id integer, bar prepenum[])")
    conn.cursor().execute(
        "INSERT INTO preptable (bar) VALUES (%(enum_col)s::prepenum[])",
        {"enum_col": ["foo"]},
    )

    stmts = get_prepared_statements(conn)
    assert len(stmts) == 3


def test_change_type_savepoint(conn):
    conn.prepare_threshold = 0
    with conn.transaction():
        for i in range(3):
            with pytest.raises(ZeroDivisionError):
                with conn.transaction():
                    conn.execute("CREATE TYPE prepenum AS ENUM ('foo', 'bar', 'baz')")
                    conn.execute("CREATE TABLE preptable(id integer, bar prepenum[])")
                    conn.cursor().execute(
                        "INSERT INTO preptable (bar) VALUES (%(enum_col)s::prepenum[])",
                        {"enum_col": ["foo"]},
                    )
                    raise ZeroDivisionError()


def get_prepared_statements(conn):
    cur = conn.cursor(row_factory=namedtuple_row)
    cur.execute(
        # CRDB has 'PREPARE name AS' in the statement.
        r"""
select name,
    regexp_replace(statement, 'prepare _pg3_\d+ as ', '', 'i') as statement,
    prepare_time,
    parameter_types
from pg_prepared_statements
where name != ''
        """,
        prepare=False,
    )
    return cur.fetchall()