Skip to content

Commit 32a17d8

Browse files
committed
test(paramstyle): add integration tests, rework
1 parent 51a5f14 commit 32a17d8

File tree

2 files changed

+162
-39
lines changed

2 files changed

+162
-39
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import typing
2+
3+
import pytest # type: ignore
4+
5+
6+
@pytest.mark.parametrize(
7+
"parameters",
8+
[
9+
({"c1": "abc", "c2": "defg", "c3": "hijkl"}, ["abc", "defg", "hijkl"]),
10+
({"c1": "a", "c2": "b", "c3": "c"}, ["a", "b", "c"]),
11+
],
12+
)
13+
def test_pyformat(cursor, parameters):
14+
cursor.paramstyle = "pyformat"
15+
data, exp_result = parameters
16+
cursor.execute("create temporary table test_pyformat(c1 varchar, c2 varchar, c3 varchar)")
17+
cursor.execute("insert into test_pyformat(c1, c2, c3) values(%(c1)s, %(c2)s, %(c3)s)", data)
18+
cursor.execute("select * from test_pyformat")
19+
res: typing.Tuple[typing.List[str, str, str]] = cursor.fetchall()
20+
assert len(res) == 1
21+
assert len(res[0]) == len(data)
22+
assert res[0] == exp_result
23+
24+
25+
@pytest.mark.parametrize(
26+
"parameters",
27+
[
28+
(
29+
({"c1": "abc", "c2": "defg", "c3": "hijkl"}, {"c1": "a", "c2": "b", "c3": "c"}),
30+
[["abc", "defg", "hijkl"], ["a", "b", "c"]],
31+
),
32+
],
33+
)
34+
def test_pyformat_multiple_insert(cursor, parameters):
35+
cursor.paramstyle = "pyformat"
36+
data, exp_result = parameters
37+
cursor.execute("create temporary table test_pyformat(c1 varchar, c2 varchar, c3 varchar)")
38+
cursor.executemany("insert into test_pyformat(c1, c2, c3) values(%(c1)s, %(c2)s, %(c3)s)", data)
39+
cursor.execute("select * from test_pyformat")
40+
res: typing.Tuple[typing.List[str, str, str], ...] = cursor.fetchall()
41+
assert len(res) == len(exp_result)
42+
for idx, row in enumerate(res):
43+
assert len(row) == len(exp_result[idx])
44+
assert row == exp_result[idx]
45+
46+
47+
@pytest.mark.parametrize(
48+
"parameters", [(["abc", "defg", "hijkl"], ["abc", "defg", "hijkl"]), (["a", "b", "c"], ["a", "b", "c"])]
49+
)
50+
def test_qmark(cursor, parameters):
51+
cursor.paramstyle = "qmark"
52+
data, exp_result = parameters
53+
cursor.execute("create temporary table test_qmark(c1 varchar, c2 varchar, c3 varchar)")
54+
cursor.execute("insert into test_qmark(c1, c2, c3) values(?, ?, ?)", data)
55+
cursor.execute("select * from test_qmark")
56+
res: typing.Tuple[typing.List[str, str, str]] = cursor.fetchall()
57+
assert len(res) == 1
58+
assert len(res[0]) == len(data)
59+
assert res[0] == exp_result
60+
61+
62+
@pytest.mark.parametrize(
63+
"parameters", [(["abc", "defg", "hijkl"], ["abc", "defg", "hijkl"]), (["a", "b", "c"], ["a", "b", "c"])]
64+
)
65+
def test_numeric(cursor, parameters):
66+
cursor.paramstyle = "numeric"
67+
data, exp_result = parameters
68+
cursor.execute("create temporary table test_numeric(c1 varchar, c2 varchar, c3 varchar)")
69+
cursor.execute("insert into test_numeric(c1, c2, c3) values(:1, :2, :3)", data)
70+
cursor.execute("select * from test_numeric")
71+
res: typing.Tuple[typing.List[str, str, str]] = cursor.fetchall()
72+
assert len(res) == 1
73+
assert len(res[0]) == len(data)
74+
assert res[0] == exp_result
75+
76+
77+
@pytest.mark.parametrize(
78+
"parameters",
79+
[
80+
({"parameter1": "abc", "parameter2": "defg", "parameter3": "hijkl"}, ["abc", "defg", "hijkl"]),
81+
({"parameter1": "a", "parameter2": "b", "parameter3": "c"}, ["a", "b", "c"]),
82+
],
83+
)
84+
def test_named(cursor, parameters):
85+
cursor.paramstyle = "named"
86+
data, exp_result = parameters
87+
cursor.execute("create temporary table test_named(c1 varchar, c2 varchar, c3 varchar)")
88+
cursor.execute("insert into test_named(c1, c2, c3) values(:parameter1, :parameter2, :parameter3)", data)
89+
cursor.execute("select * from test_named")
90+
res: typing.Tuple[typing.List[str, str, str]] = cursor.fetchall()
91+
assert len(res) == 1
92+
assert len(res[0]) == len(data)
93+
assert res[0] == exp_result
94+
95+
96+
@pytest.mark.parametrize(
97+
"parameters", [(["abc", "defg", "hijkl"], ["abc", "defg", "hijkl"]), (["a", "b", "c"], ["a", "b", "c"])]
98+
)
99+
def test_format(cursor, parameters):
100+
cursor.paramstyle = "format"
101+
data, exp_result = parameters
102+
cursor.execute("create temporary table test_format(c1 varchar, c2 varchar, c3 varchar)")
103+
cursor.execute("insert into test_format(c1, c2, c3) values(%s, %s, %s)", data)
104+
cursor.execute("select * from test_format")
105+
res: typing.Tuple[typing.List[str, str, str]] = cursor.fetchall()
106+
assert len(res) == 1
107+
assert len(res[0]) == len(data)
108+
assert res[0] == exp_result
109+
110+
111+
@pytest.mark.parametrize(
112+
"parameters",
113+
[
114+
([["abc", "defg", "hijkl"], ["a", "b", "c"]], [["abc", "defg", "hijkl"], ["a", "b", "c"]]),
115+
],
116+
)
117+
def test_format_multiple(cursor, parameters):
118+
cursor.paramstyle = "format"
119+
data, exp_result = parameters
120+
cursor.execute("create temporary table test_format(c1 varchar, c2 varchar, c3 varchar)")
121+
cursor.executemany("insert into test_format(c1, c2, c3) values(%s, %s, %s)", data)
122+
cursor.execute("select * from test_format")
123+
res: typing.Tuple[typing.List[str, str, str], ...] = cursor.fetchall()
124+
assert len(res) == len(exp_result)
125+
for idx, row in enumerate(res):
126+
assert len(row) == len(exp_result[idx])
127+
assert row == exp_result[idx]

test/unit/test_paramstyle.py

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,73 @@
66

77

88
def test_qmark():
9-
args: typing.Tuple[int, int, int] = (1, 2, 3)
10-
new_query, vals = convert(
11-
"qmark", 'SELECT ?, ?, "field_?" FROM t ' "WHERE a='say ''what?''' AND b=? AND c=E'?\\'test\\'?'", args
9+
new_query, make_args = convert(
10+
"qmark", 'SELECT ?, ?, "field_?" FROM t ' "WHERE a='say ''what?''' AND b=? AND c=E'?\\'test\\'?'"
1211
)
1312
expected: str = 'SELECT $1, $2, "field_?" FROM t WHERE ' "a='say ''what?''' AND b=$3 AND c=E'?\\'test\\'?'"
14-
assert (new_query, vals) == (expected, args)
13+
assert new_query == expected
14+
assert make_args((1, 2, 3)) == (1, 2, 3)
1515

1616

1717
def test_qmark_2():
18-
args: typing.Tuple[int, int, int] = (1, 2, 3)
19-
new_query, vals = convert("qmark", "SELECT ?, ?, * FROM t WHERE a=? AND b='are you ''sure?'", args)
18+
new_query, make_args = convert("qmark", "SELECT ?, ?, * FROM t WHERE a=? AND b='are you ''sure?'")
2019
expected: str = "SELECT $1, $2, * FROM t WHERE a=$3 AND b='are you ''sure?'"
21-
assert (new_query, vals) == (expected, args)
20+
assert new_query == expected
21+
assert make_args((1, 2, 3)) == (1, 2, 3)
2222

2323

2424
def test_numeric():
25-
args: typing.Tuple[int, int, int] = (1, 2, 3)
26-
new_query, vals = convert("numeric", "SELECT sum(x)::decimal(5, 2) :2, :1, * FROM t WHERE a=:3", args)
25+
new_query, make_args = convert("numeric", "SELECT sum(x)::decimal(5, 2) :2, :1, * FROM t WHERE a=:3")
2726
expected: str = "SELECT sum(x)::decimal(5, 2) $2, $1, * FROM t WHERE a=$3"
28-
assert (new_query, vals) == (expected, args)
27+
assert new_query == expected
28+
assert make_args((1, 2, 3)) == (1, 2, 3)
2929

3030

3131
def test_numeric_default_parameter():
32-
args: typing.Tuple[int, int, int] = (1, 2, 3)
33-
new_query, vals = convert("numeric", "make_interval(days := 10)", args)
32+
new_query, make_args = convert("numeric", "make_interval(days := 10)")
3433

35-
assert (new_query, vals) == ("make_interval(days := 10)", args)
34+
assert new_query == "make_interval(days := 10)"
35+
assert make_args((1, 2, 3)) == (1, 2, 3)
3636

3737

3838
def test_named():
39-
args: typing.Dict[str, int] = {
40-
"f_2": 1,
41-
"f1": 2,
42-
}
43-
new_query, vals = convert("named", "SELECT sum(x)::decimal(5, 2) :f_2, :f1 FROM t WHERE a=:f_2", args)
39+
new_query, make_args = convert("named", "SELECT sum(x)::decimal(5, 2) :f_2, :f1 FROM t WHERE a=:f_2")
4440
expected: str = "SELECT sum(x)::decimal(5, 2) $1, $2 FROM t WHERE a=$1"
45-
assert (new_query, vals) == (expected, (1, 2))
41+
assert new_query == expected
42+
assert make_args({"f_2": 1, "f1": 2}) == (1, 2)
4643

4744

4845
def test_format():
49-
args: typing.Tuple[int, int, int] = (1, 2, 3)
50-
new_query, vals = convert(
46+
new_query, make_args = convert(
5147
"format",
5248
"SELECT %s, %s, \"f1_%%\", E'txt_%%' FROM t WHERE a=%s AND b='75%%' AND c = '%' -- Comment with %",
53-
args,
5449
)
55-
expected: str = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$3 AND b='75%%' AND c = '%' -- Comment with %"
56-
assert (new_query, vals) == (expected, args)
50+
expected: str = (
51+
"SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$3 AND " "b='75%%' AND c = '%' -- Comment with %"
52+
)
53+
assert new_query == expected
54+
assert make_args((1, 2, 3)) == (1, 2, 3)
5755

5856
sql: str = r"""COMMENT ON TABLE test_schema.comment_test """ r"""IS 'the test % '' " \ table comment'"""
59-
new_query, vals = convert("format", sql, args)
60-
assert (new_query, vals) == (sql, args)
57+
new_query, make_args = convert("format", sql)
58+
assert new_query == sql
6159

6260

6361
def test_format_multiline():
64-
args: typing.Tuple[int, int, int] = (1, 2, 3)
65-
new_query, vals = convert("format", "SELECT -- Comment\n%s FROM t", args)
66-
assert (new_query, vals) == ("SELECT -- Comment\n$1 FROM t", args)
62+
new_query, make_args = convert("format", "SELECT -- Comment\n%s FROM t")
63+
assert new_query == "SELECT -- Comment\n$1 FROM t"
6764

6865

6966
def test_py_format():
70-
args: typing.Dict[str, int] = {"f2": 1, "f1": 2, "f3": 3}
71-
72-
new_query, vals = convert(
73-
"pyformat", "SELECT %(f2)s, %(f1)s, \"f1_%%\", E'txt_%%' FROM t WHERE a=%(f2)s AND b='75%%'", args
67+
new_query, make_args = convert(
68+
"pyformat", "SELECT %(f2)s, %(f1)s, \"f1_%%\", E'txt_%%' " "FROM t WHERE a=%(f2)s AND b='75%%'"
7469
)
75-
expected: str = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$1 AND b='75%%'"
76-
assert (new_query, vals) == (expected, (1, 2))
70+
expected: str = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$1 AND " "b='75%%'"
71+
assert new_query == expected
72+
assert make_args({"f2": 1, "f1": 2, "f3": 3}) == (1, 2)
7773

7874
# pyformat should support %s and an array, too:
79-
args: typing.Tuple[int, int, int] = (1, 2, 3)
80-
new_query, vals = convert("pyformat", "SELECT %s, %s, \"f1_%%\", E'txt_%%' FROM t WHERE a=%s AND b='75%%'", args)
81-
expected: str = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$3 AND b='75%%'"
82-
assert (new_query, vals) == (expected, args)
75+
new_query, make_args = convert("pyformat", "SELECT %s, %s, \"f1_%%\", E'txt_%%' " "FROM t WHERE a=%s AND b='75%%'")
76+
expected = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$3 AND " "b='75%%'"
77+
assert new_query, expected
78+
assert make_args((1, 2, 3)) == (1, 2, 3)

0 commit comments

Comments
 (0)