|
6 | 6 |
|
7 | 7 |
|
8 | 8 | 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\\'?'" |
12 | 11 | ) |
13 | 12 | 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) |
15 | 15 |
|
16 | 16 |
|
17 | 17 | 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?'") |
20 | 19 | 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) |
22 | 22 |
|
23 | 23 |
|
24 | 24 | 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") |
27 | 26 | 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) |
29 | 29 |
|
30 | 30 |
|
31 | 31 | 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)") |
34 | 33 |
|
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) |
36 | 36 |
|
37 | 37 |
|
38 | 38 | 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") |
44 | 40 | 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) |
46 | 43 |
|
47 | 44 |
|
48 | 45 | def test_format(): |
49 | | - args: typing.Tuple[int, int, int] = (1, 2, 3) |
50 | | - new_query, vals = convert( |
| 46 | + new_query, make_args = convert( |
51 | 47 | "format", |
52 | 48 | "SELECT %s, %s, \"f1_%%\", E'txt_%%' FROM t WHERE a=%s AND b='75%%' AND c = '%' -- Comment with %", |
53 | | - args, |
54 | 49 | ) |
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) |
57 | 55 |
|
58 | 56 | 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 |
61 | 59 |
|
62 | 60 |
|
63 | 61 | 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" |
67 | 64 |
|
68 | 65 |
|
69 | 66 | 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%%'" |
74 | 69 | ) |
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) |
77 | 73 |
|
78 | 74 | # 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