forked from crate/crate-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_params.py
More file actions
91 lines (75 loc) · 3.22 KB
/
test_params.py
File metadata and controls
91 lines (75 loc) · 3.22 KB
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
# -*- coding: utf-8; -*-
#
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. Crate licenses
# this file to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# However, if you have executed another commercial license agreement
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.
import pytest
from crate.client.exceptions import ProgrammingError
from crate.client.params import convert_named_to_positional
def test_basic_conversion():
"""Named placeholders are replaced with ? and values are ordered."""
sql, params = convert_named_to_positional(
"SELECT * FROM t WHERE a = %(a)s AND b = %(b)s",
{"a": 1, "b": 2},
)
assert sql == "SELECT * FROM t WHERE a = ? AND b = ?"
assert params == [1, 2]
def test_repeated_param():
"""The same name appearing multiple times appends the value each time."""
sql, params = convert_named_to_positional(
"SELECT %(x)s, %(x)s",
{"x": 42},
)
assert sql == "SELECT ?, ?"
assert params == [42, 42]
def test_missing_param_raises():
"""A placeholder without a matching key raises ProgrammingError."""
with pytest.raises(ProgrammingError, match="Named parameter 'z' not found"):
convert_named_to_positional("SELECT %(z)s", {"a": 1})
def test_extra_params_ignored():
"""Extra keys in the params dict cause no error."""
sql, params = convert_named_to_positional(
"SELECT %(a)s",
{"a": 10, "b": 99, "c": "unused"},
)
assert sql == "SELECT ?"
assert params == [10]
def test_no_named_params():
"""SQL without %(...)s placeholders is returned unchanged."""
sql, params = convert_named_to_positional(
"SELECT * FROM t WHERE a = ?",
{},
)
assert sql == "SELECT * FROM t WHERE a = ?"
assert params == []
def test_various_value_types():
"""Different value types (str, int, float, None, bool) are handled."""
sql, params = convert_named_to_positional(
"INSERT INTO t VALUES (%(s)s, %(i)s, %(f)s, %(n)s, %(b)s)",
{"s": "hello", "i": 7, "f": 3.14, "n": None, "b": True},
)
assert sql == "INSERT INTO t VALUES (?, ?, ?, ?, ?)"
assert params == ["hello", 7, 3.14, None, True]
def test_preserves_surrounding_text():
"""Non-placeholder text in the SQL is not modified."""
sql, params = convert_named_to_positional(
"SELECT name FROM locations WHERE name = %(name)s ORDER BY name",
{"name": "Algol"},
)
assert sql == "SELECT name FROM locations WHERE name = ? ORDER BY name"
assert params == ["Algol"]