|
| 1 | +# -*- coding: utf-8; -*- |
| 2 | +# |
| 3 | +# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor |
| 4 | +# license agreements. See the NOTICE file distributed with this work for |
| 5 | +# additional information regarding copyright ownership. Crate licenses |
| 6 | +# this file to you under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. You may |
| 8 | +# obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | +# License for the specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# |
| 18 | +# However, if you have executed another commercial license agreement |
| 19 | +# with Crate these terms will supersede the license and you may use the |
| 20 | +# software solely pursuant to the terms of the relevant commercial agreement. |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from crate.client.exceptions import ProgrammingError |
| 25 | +from crate.client.params import convert_named_to_positional |
| 26 | + |
| 27 | + |
| 28 | +def test_basic_conversion(): |
| 29 | + """Named placeholders are replaced with ? and values are ordered.""" |
| 30 | + sql, params = convert_named_to_positional( |
| 31 | + "SELECT * FROM t WHERE a = %(a)s AND b = %(b)s", |
| 32 | + {"a": 1, "b": 2}, |
| 33 | + ) |
| 34 | + assert sql == "SELECT * FROM t WHERE a = ? AND b = ?" |
| 35 | + assert params == [1, 2] |
| 36 | + |
| 37 | + |
| 38 | +def test_repeated_param(): |
| 39 | + """The same name appearing multiple times appends the value each time.""" |
| 40 | + sql, params = convert_named_to_positional( |
| 41 | + "SELECT %(x)s, %(x)s", |
| 42 | + {"x": 42}, |
| 43 | + ) |
| 44 | + assert sql == "SELECT ?, ?" |
| 45 | + assert params == [42, 42] |
| 46 | + |
| 47 | + |
| 48 | +def test_missing_param_raises(): |
| 49 | + """A placeholder without a matching key raises ProgrammingError.""" |
| 50 | + with pytest.raises(ProgrammingError, match="Named parameter 'z' not found"): |
| 51 | + convert_named_to_positional("SELECT %(z)s", {"a": 1}) |
| 52 | + |
| 53 | + |
| 54 | +def test_extra_params_ignored(): |
| 55 | + """Extra keys in the params dict cause no error.""" |
| 56 | + sql, params = convert_named_to_positional( |
| 57 | + "SELECT %(a)s", |
| 58 | + {"a": 10, "b": 99, "c": "unused"}, |
| 59 | + ) |
| 60 | + assert sql == "SELECT ?" |
| 61 | + assert params == [10] |
| 62 | + |
| 63 | + |
| 64 | +def test_no_named_params(): |
| 65 | + """SQL without %(...)s placeholders is returned unchanged.""" |
| 66 | + sql, params = convert_named_to_positional( |
| 67 | + "SELECT * FROM t WHERE a = ?", |
| 68 | + {}, |
| 69 | + ) |
| 70 | + assert sql == "SELECT * FROM t WHERE a = ?" |
| 71 | + assert params == [] |
| 72 | + |
| 73 | + |
| 74 | +def test_various_value_types(): |
| 75 | + """Different value types (str, int, float, None, bool) are handled.""" |
| 76 | + sql, params = convert_named_to_positional( |
| 77 | + "INSERT INTO t VALUES (%(s)s, %(i)s, %(f)s, %(n)s, %(b)s)", |
| 78 | + {"s": "hello", "i": 7, "f": 3.14, "n": None, "b": True}, |
| 79 | + ) |
| 80 | + assert sql == "INSERT INTO t VALUES (?, ?, ?, ?, ?)" |
| 81 | + assert params == ["hello", 7, 3.14, None, True] |
| 82 | + |
| 83 | + |
| 84 | +def test_preserves_surrounding_text(): |
| 85 | + """Non-placeholder text in the SQL is not modified.""" |
| 86 | + sql, params = convert_named_to_positional( |
| 87 | + "SELECT name FROM locations WHERE name = %(name)s ORDER BY name", |
| 88 | + {"name": "Algol"}, |
| 89 | + ) |
| 90 | + assert sql == "SELECT name FROM locations WHERE name = ? ORDER BY name" |
| 91 | + assert params == ["Algol"] |
0 commit comments