Files
Joe SpadolaandGitHub 59a78c6824 auto-promote large query parameters to form body (#789)
* auto-promote large query parameters to form body

* address pr review comments

* add type hint
2026-06-09 22:33:40 -07:00

81 lines
2.5 KiB
Python

import pytest
from clickhouse_connect.driver.binding import MAX_URL_BIND_PARAM_LENGTH, quote_identifier, use_form_encoding
@pytest.mark.parametrize(
"identifier, expected",
[
("foo", "`foo`"),
("foo`bar", "`foo\\`bar`"),
('foo"bar', '`foo"bar`'),
("", "``"),
],
)
def test_quote_identifier_raw(identifier, expected):
assert quote_identifier(identifier) == expected
@pytest.mark.parametrize(
"identifier",
[
"`foo`",
'"foo"',
"`foo\\`bar`",
"`foo``bar`",
'"foo""bar"',
],
)
def test_quote_identifier_valid_prequoted_passthrough(identifier):
assert quote_identifier(identifier) == identifier
@pytest.mark.parametrize(
"identifier, expected",
[
("`weird`name`", "`\\`weird\\`name\\``"),
('"weird"name"', '`"weird"name"`'),
("`foo\\`", "`\\`foo\\\\\\``"),
("`", "`\\``"),
],
)
def test_quote_identifier_invalid_prequoted_escaped_as_raw(identifier, expected):
assert quote_identifier(identifier) == expected
def test_use_form_encoding_empty():
assert use_form_encoding("SELECT 1", {}) is False
assert use_form_encoding("SELECT 1", {}, force_form=True) is True
def test_use_form_encoding_force():
assert use_form_encoding("SELECT {id:UInt32}", {"param_id": "1"}, force_form=True) is True
def test_use_form_encoding_small_params_stay_in_url():
assert use_form_encoding("SELECT 1", {"param_id": "123", "param_name": "abc"}) is False
def test_use_form_encoding_large_params_promote():
big = {"param_big": "x" * (MAX_URL_BIND_PARAM_LENGTH + 1)}
assert use_form_encoding("SELECT {big:String}", big) is True
def test_use_form_encoding_total_across_params():
# Many individually small params whose combined encoded length exceeds the budget
params = {f"param_{i}": "v" * 200 for i in range(40)}
assert use_form_encoding("SELECT 1", params) is True
def test_use_form_encoding_percent_expansion_promotes():
# Raw length is under the budget but percent-encoding expands each space to %20
params = {"param_s": " " * (MAX_URL_BIND_PARAM_LENGTH // 2)}
assert use_form_encoding("SELECT 1", params) is True
def test_use_form_encoding_binary_query_not_promoted():
# Binary binds make the query bytes; auto-promotion must not kick in unless forced
big = {"param_big": "x" * (MAX_URL_BIND_PARAM_LENGTH + 1)}
assert use_form_encoding(b"SELECT \xff", big) is False
assert use_form_encoding(b"SELECT \xff", big, force_form=True) is True