|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import datetime |
| 4 | +import decimal |
| 5 | + |
| 6 | +import ydb |
| 7 | +from ydb_dbapi.utils import convert_query_parameters |
| 8 | + |
| 9 | + |
| 10 | +class TestNamedStyle: |
| 11 | + """%(name)s placeholders with a dict.""" |
| 12 | + |
| 13 | + def test_basic_query_transformation(self): |
| 14 | + q, _ = convert_query_parameters( |
| 15 | + "SELECT %(id)s FROM t WHERE name = %(name)s", |
| 16 | + {"id": 1, "name": "alice"}, |
| 17 | + ) |
| 18 | + assert q == "SELECT $id FROM t WHERE name = $name" |
| 19 | + |
| 20 | + def test_keys_prefixed_with_dollar(self): |
| 21 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": 1}) |
| 22 | + assert "$x" in p |
| 23 | + assert "x" not in p |
| 24 | + |
| 25 | + def test_int(self): |
| 26 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": 42}) |
| 27 | + assert p["$x"] == ydb.TypedValue(42, ydb.PrimitiveType.Int64) |
| 28 | + |
| 29 | + def test_float(self): |
| 30 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": 3.14}) |
| 31 | + assert p["$x"] == ydb.TypedValue(3.14, ydb.PrimitiveType.Double) |
| 32 | + |
| 33 | + def test_str(self): |
| 34 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": "hello"}) |
| 35 | + assert p["$x"] == ydb.TypedValue("hello", ydb.PrimitiveType.Utf8) |
| 36 | + |
| 37 | + def test_bytes(self): |
| 38 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": b"data"}) |
| 39 | + assert p["$x"] == ydb.TypedValue(b"data", ydb.PrimitiveType.String) |
| 40 | + |
| 41 | + def test_bool(self): |
| 42 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": True}) |
| 43 | + assert p["$x"] == ydb.TypedValue(True, ydb.PrimitiveType.Bool) |
| 44 | + |
| 45 | + def test_bool_not_confused_with_int(self): |
| 46 | + # bool is subclass of int — must map to Bool, not Int64 |
| 47 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": False}) |
| 48 | + assert p["$x"].value_type == ydb.PrimitiveType.Bool |
| 49 | + |
| 50 | + def test_date(self): |
| 51 | + d = datetime.date(2024, 1, 15) |
| 52 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": d}) |
| 53 | + assert p["$x"] == ydb.TypedValue(d, ydb.PrimitiveType.Date) |
| 54 | + |
| 55 | + def test_datetime(self): |
| 56 | + tz = datetime.timezone.utc |
| 57 | + dt = datetime.datetime(2024, 1, 15, 12, 0, 0, tzinfo=tz) |
| 58 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": dt}) |
| 59 | + assert p["$x"] == ydb.TypedValue(dt, ydb.PrimitiveType.Timestamp) |
| 60 | + |
| 61 | + def test_datetime_not_confused_with_date(self): |
| 62 | + # datetime is subclass of date — must map to Timestamp, not Date |
| 63 | + tz = datetime.timezone.utc |
| 64 | + dt = datetime.datetime(2024, 6, 1, 0, 0, 0, tzinfo=tz) |
| 65 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": dt}) |
| 66 | + assert p["$x"].value_type == ydb.PrimitiveType.Timestamp |
| 67 | + |
| 68 | + def test_timedelta(self): |
| 69 | + td = datetime.timedelta(seconds=60) |
| 70 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": td}) |
| 71 | + assert p["$x"] == ydb.TypedValue(td, ydb.PrimitiveType.Interval) |
| 72 | + |
| 73 | + def test_decimal(self): |
| 74 | + d = decimal.Decimal("3.14") |
| 75 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": d}) |
| 76 | + assert isinstance(p["$x"], ydb.TypedValue) |
| 77 | + assert p["$x"].value == d |
| 78 | + |
| 79 | + def test_none_passed_as_is(self): |
| 80 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": None}) |
| 81 | + assert p["$x"] is None |
| 82 | + |
| 83 | + def test_unknown_type_passed_as_is(self): |
| 84 | + obj = object() |
| 85 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": obj}) |
| 86 | + assert p["$x"] is obj |
| 87 | + |
| 88 | + def test_multiple_params(self): |
| 89 | + q, p = convert_query_parameters( |
| 90 | + "INSERT INTO t VALUES (%(a)s, %(b)s, %(c)s)", |
| 91 | + {"a": 1, "b": "hi", "c": True}, |
| 92 | + ) |
| 93 | + assert q == "INSERT INTO t VALUES ($a, $b, $c)" |
| 94 | + assert "$a" in p |
| 95 | + assert "$b" in p |
| 96 | + assert "$c" in p |
| 97 | + |
| 98 | + def test_percent_percent_escape(self): |
| 99 | + q, _ = convert_query_parameters( |
| 100 | + "SELECT %% as pct, %(x)s", {"x": 1} |
| 101 | + ) |
| 102 | + assert q == "SELECT % as pct, $x" |
| 103 | + |
| 104 | + def test_empty_params(self): |
| 105 | + q, p = convert_query_parameters("SELECT 1", {}) |
| 106 | + assert q == "SELECT 1" |
| 107 | + assert p == {} |
| 108 | + |
| 109 | + |
| 110 | +class TestCustomTypes: |
| 111 | + """Pass-through for ydb.TypedValue (explicit type hint).""" |
| 112 | + |
| 113 | + def test_typed_value_passed_through(self): |
| 114 | + tv = ydb.TypedValue(42, ydb.PrimitiveType.Int32) |
| 115 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": tv}) |
| 116 | + assert p["$x"] is tv |
| 117 | + |
| 118 | + def test_typed_value_not_double_wrapped(self): |
| 119 | + tv = ydb.TypedValue("hello", ydb.PrimitiveType.Utf8) |
| 120 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": tv}) |
| 121 | + assert isinstance(p["$x"], ydb.TypedValue) |
| 122 | + assert p["$x"].value_type == ydb.PrimitiveType.Utf8 |
| 123 | + |
| 124 | + def test_typed_value_positional(self): |
| 125 | + tv = ydb.TypedValue(99, ydb.PrimitiveType.Int32) |
| 126 | + _, p = convert_query_parameters("SELECT %s", [tv]) |
| 127 | + assert p["$p1"] is tv |
| 128 | + |
| 129 | + def test_unknown_type_passed_as_is(self): |
| 130 | + # No TypedValue, no known type — value goes through unchanged |
| 131 | + val = object() |
| 132 | + _, p = convert_query_parameters("SELECT %(x)s", {"x": val}) |
| 133 | + assert p["$x"] is val |
| 134 | + |
| 135 | + |
| 136 | +class TestPositionalStyle: |
| 137 | + """Positional %s placeholders with a list or tuple.""" |
| 138 | + |
| 139 | + def test_basic_list(self): |
| 140 | + q, p = convert_query_parameters("SELECT %s", [42]) |
| 141 | + assert q == "SELECT $p1" |
| 142 | + assert p["$p1"] == ydb.TypedValue(42, ydb.PrimitiveType.Int64) |
| 143 | + |
| 144 | + def test_basic_tuple(self): |
| 145 | + q, p = convert_query_parameters("SELECT %s", (42,)) |
| 146 | + assert q == "SELECT $p1" |
| 147 | + assert p["$p1"] == ydb.TypedValue(42, ydb.PrimitiveType.Int64) |
| 148 | + |
| 149 | + def test_multiple_params_numbered_sequentially(self): |
| 150 | + q, p = convert_query_parameters( |
| 151 | + "INSERT INTO t VALUES (%s, %s, %s)", [1, "hi", 3.14] |
| 152 | + ) |
| 153 | + assert q == "INSERT INTO t VALUES ($p1, $p2, $p3)" |
| 154 | + assert p["$p1"] == ydb.TypedValue(1, ydb.PrimitiveType.Int64) |
| 155 | + assert p["$p2"] == ydb.TypedValue("hi", ydb.PrimitiveType.Utf8) |
| 156 | + assert p["$p3"] == ydb.TypedValue(3.14, ydb.PrimitiveType.Double) |
| 157 | + |
| 158 | + def test_none_passed_as_is(self): |
| 159 | + _, p = convert_query_parameters("SELECT %s", [None]) |
| 160 | + assert p["$p1"] is None |
| 161 | + |
| 162 | + def test_percent_percent_escape(self): |
| 163 | + q, p = convert_query_parameters("SELECT %%, %s", [7]) |
| 164 | + assert q == "SELECT %, $p1" |
| 165 | + assert p["$p1"] == ydb.TypedValue(7, ydb.PrimitiveType.Int64) |
| 166 | + |
| 167 | + def test_empty_list(self): |
| 168 | + q, p = convert_query_parameters("SELECT 1", []) |
| 169 | + assert q == "SELECT 1" |
| 170 | + assert p == {} |
0 commit comments