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
|
import pytest
import psycopg
from psycopg import pq
from psycopg.adapt import PyFormat, Transformer
from psycopg._queries import PostgresQuery, _split_query
@pytest.mark.parametrize(
"input, want",
[
(b"", [(b"", 0, PyFormat.AUTO)]),
(b"foo bar", [(b"foo bar", 0, PyFormat.AUTO)]),
(b"foo %% bar", [(b"foo % bar", 0, PyFormat.AUTO)]),
(b"%s", [(b"", 0, PyFormat.AUTO), (b"", 0, PyFormat.AUTO)]),
(b"%s foo", [(b"", 0, PyFormat.AUTO), (b" foo", 0, PyFormat.AUTO)]),
(b"%b foo", [(b"", 0, PyFormat.BINARY), (b" foo", 0, PyFormat.AUTO)]),
(b"foo %s", [(b"foo ", 0, PyFormat.AUTO), (b"", 0, PyFormat.AUTO)]),
(
b"foo %%%s bar",
[(b"foo %", 0, PyFormat.AUTO), (b" bar", 0, PyFormat.AUTO)],
),
(
b"foo %(name)s bar",
[(b"foo ", "name", PyFormat.AUTO), (b" bar", 0, PyFormat.AUTO)],
),
(
b"foo %(name)s %(name)b bar",
[
(b"foo ", "name", PyFormat.AUTO),
(b" ", "name", PyFormat.BINARY),
(b" bar", 0, PyFormat.AUTO),
],
),
(
b"foo %s%b bar %s baz",
[
(b"foo ", 0, PyFormat.AUTO),
(b"", 1, PyFormat.BINARY),
(b" bar ", 2, PyFormat.AUTO),
(b" baz", 0, PyFormat.AUTO),
],
),
],
)
def test_split_query(input, want):
assert _split_query(input) == want
@pytest.mark.parametrize(
"input",
[
b"foo %d bar",
b"foo % bar",
b"foo %%% bar",
b"foo %(foo)d bar",
b"foo %(foo)s bar %s baz",
b"foo %(foo) bar",
b"foo %(foo bar",
b"3%2",
],
)
def test_split_query_bad(input):
with pytest.raises(psycopg.ProgrammingError):
_split_query(input)
@pytest.mark.parametrize(
"query, params, want, wformats, wparams",
[
(b"", None, b"", None, None),
(b"", [], b"", [], []),
(b"%%", [], b"%", [], []),
(b"select %t", (1,), b"select $1", [pq.Format.TEXT], [b"1"]),
(
b"%t %% %t",
(1, 2),
b"$1 % $2",
[pq.Format.TEXT, pq.Format.TEXT],
[b"1", b"2"],
),
(
b"%t %% %t",
("a", 2),
b"$1 % $2",
[pq.Format.TEXT, pq.Format.TEXT],
[b"a", b"2"],
),
],
)
def test_pg_query_seq(query, params, want, wformats, wparams):
pq = PostgresQuery(Transformer())
pq.convert(query, params)
assert pq.query == want
assert pq.formats == wformats
assert pq.params == wparams
@pytest.mark.parametrize(
"query, params, want, wformats, wparams",
[
(b"", {}, b"", [], []),
(b"hello %%", {"a": 1}, b"hello %", [], []),
(
b"select %(hello)t",
{"hello": 1, "world": 2},
b"select $1",
[pq.Format.TEXT],
[b"1"],
),
(
b"select %(hi)s %(there)s %(hi)s",
{"hi": 0, "there": "a"},
b"select $1 $2 $1",
[pq.Format.BINARY, pq.Format.TEXT],
[b"\x00" * 2, b"a"],
),
],
)
def test_pg_query_map(query, params, want, wformats, wparams):
pq = PostgresQuery(Transformer())
pq.convert(query, params)
assert pq.query == want
assert pq.formats == wformats
assert pq.params == wparams
@pytest.mark.parametrize(
"query, params",
[
(b"select %s", {"a": 1}),
(b"select %(name)s", [1]),
(b"select %s", "a"),
(b"select %s", 1),
(b"select %s", b"a"),
(b"select %s", set()),
],
)
def test_pq_query_badtype(query, params):
pq = PostgresQuery(Transformer())
with pytest.raises(TypeError):
pq.convert(query, params)
@pytest.mark.parametrize(
"query, params",
[
(b"", [1]),
(b"%s", []),
(b"%%", [1]),
(b"$1", [1]),
(b"select %(", {"a": 1}),
(b"select %(a", {"a": 1}),
(b"select %(a)", {"a": 1}),
(b"select %s %(hi)s", [1]),
(b"select %(hi)s %(hi)b", {"hi": 1}),
],
)
def test_pq_query_badprog(query, params):
pq = PostgresQuery(Transformer())
with pytest.raises(psycopg.ProgrammingError):
pq.convert(query, params)
|