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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
import datetime
import re
from types import MappingProxyType
from sqlalchemy import exc
from sqlalchemy.engine import processors
from sqlalchemy.testing import assert_raises_message
from sqlalchemy.testing import eq_
from sqlalchemy.testing import expect_raises_message
from sqlalchemy.testing import fixtures
from sqlalchemy.util import immutabledict
class _BooleanProcessorTest(fixtures.TestBase):
def test_int_to_bool_none(self):
eq_(self.module.int_to_boolean(None), None)
def test_int_to_bool_zero(self):
eq_(self.module.int_to_boolean(0), False)
def test_int_to_bool_one(self):
eq_(self.module.int_to_boolean(1), True)
def test_int_to_bool_positive_int(self):
eq_(self.module.int_to_boolean(12), True)
def test_int_to_bool_negative_int(self):
eq_(self.module.int_to_boolean(-4), True)
class CyBooleanProcessorTest(_BooleanProcessorTest):
__requires__ = ("cextensions",)
@classmethod
def setup_test_class(cls):
from sqlalchemy.cyextension import processors
cls.module = processors
class _DateProcessorTest(fixtures.TestBase):
def test_iso_datetime(self):
eq_(
self.module.str_to_datetime("2022-04-03 17:12:34.353"),
datetime.datetime(2022, 4, 3, 17, 12, 34, 353000),
)
eq_(
self.module.str_to_datetime("2022-04-03 17:12:34.353123"),
datetime.datetime(2022, 4, 3, 17, 12, 34, 353123),
)
eq_(
self.module.str_to_datetime("2022-04-03 17:12:34"),
datetime.datetime(2022, 4, 3, 17, 12, 34),
)
eq_(
self.module.str_to_time("17:12:34.353123"),
datetime.time(17, 12, 34, 353123),
)
eq_(
self.module.str_to_time("17:12:34.353"),
datetime.time(17, 12, 34, 353000),
)
eq_(
self.module.str_to_time("17:12:34"),
datetime.time(17, 12, 34),
)
eq_(self.module.str_to_date("2022-04-03"), datetime.date(2022, 4, 3))
def test_date_no_string(self):
assert_raises_message(
TypeError,
"fromisoformat: argument must be str",
self.module.str_to_date,
2012,
)
def test_datetime_no_string_custom_reg(self):
assert_raises_message(
ValueError,
"Couldn't parse datetime string '2012' - value is not a string",
processors.str_to_datetime_processor_factory(
re.compile(r"(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)(?:\.(\d+))?"),
datetime.datetime,
),
2012,
)
def test_time_no_string_custom_reg(self):
assert_raises_message(
ValueError,
"Couldn't parse time string '2012' - value is not a string",
processors.str_to_datetime_processor_factory(
re.compile(r"^(\d+):(\d+):(\d+)(?:\.(\d{6}))?$"), datetime.time
),
2012,
)
def test_date_invalid_string(self):
assert_raises_message(
ValueError,
"Invalid isoformat string: '5:a'",
self.module.str_to_date,
"5:a",
)
def test_datetime_invalid_string(self):
assert_raises_message(
ValueError,
"Invalid isoformat string: '5:a'",
self.module.str_to_datetime,
"5:a",
)
def test_time_invalid_string(self):
assert_raises_message(
ValueError,
"Invalid isoformat string: '5:a'",
self.module.str_to_time,
"5:a",
)
class PyDateProcessorTest(_DateProcessorTest):
@classmethod
def setup_test_class(cls):
from sqlalchemy.engine import _py_processors
cls.module = _py_processors
class CyDateProcessorTest(_DateProcessorTest):
__requires__ = ("cextensions",)
@classmethod
def setup_test_class(cls):
from sqlalchemy.cyextension import processors
cls.module = processors
class _DistillArgsTest(fixtures.TestBase):
def test_distill_20_none(self):
eq_(self.module._distill_params_20(None), ())
def test_distill_20_empty_sequence(self):
eq_(self.module._distill_params_20(()), ())
eq_(self.module._distill_params_20([]), [])
def test_distill_20_sequence_sequence(self):
eq_(self.module._distill_params_20(((1, 2, 3),)), ((1, 2, 3),))
eq_(self.module._distill_params_20([(1, 2, 3)]), [(1, 2, 3)])
eq_(self.module._distill_params_20(((1, 2), (2, 3))), ((1, 2), (2, 3)))
eq_(self.module._distill_params_20([(1, 2), (2, 3)]), [(1, 2), (2, 3)])
def test_distill_20_sequence_dict(self):
eq_(self.module._distill_params_20(({"a": 1},)), ({"a": 1},))
eq_(
self.module._distill_params_20([{"a": 1}, {"a": 2}]),
[{"a": 1}, {"a": 2}],
)
eq_(
self.module._distill_params_20((MappingProxyType({"a": 1}),)),
(MappingProxyType({"a": 1}),),
)
def test_distill_20_sequence_error(self):
with expect_raises_message(
exc.ArgumentError,
"List argument must consist only of tuples or dictionaries",
):
self.module._distill_params_20((1, 2, 3))
with expect_raises_message(
exc.ArgumentError,
"List argument must consist only of tuples or dictionaries",
):
self.module._distill_params_20(([1, 2, 3],))
with expect_raises_message(
exc.ArgumentError,
"List argument must consist only of tuples or dictionaries",
):
self.module._distill_params_20([1, 2, 3])
with expect_raises_message(
exc.ArgumentError,
"List argument must consist only of tuples or dictionaries",
):
self.module._distill_params_20(["a", "b"])
def test_distill_20_dict(self):
eq_(self.module._distill_params_20({"foo": "bar"}), [{"foo": "bar"}])
eq_(
self.module._distill_params_20(immutabledict({"foo": "bar"})),
[immutabledict({"foo": "bar"})],
)
eq_(
self.module._distill_params_20(MappingProxyType({"foo": "bar"})),
[MappingProxyType({"foo": "bar"})],
)
def test_distill_20_error(self):
with expect_raises_message(
exc.ArgumentError, "mapping or list expected for parameters"
):
self.module._distill_params_20("foo")
with expect_raises_message(
exc.ArgumentError, "mapping or list expected for parameters"
):
self.module._distill_params_20(1)
def test_distill_raw_none(self):
eq_(self.module._distill_raw_params(None), ())
def test_distill_raw_empty_list(self):
eq_(self.module._distill_raw_params([]), [])
def test_distill_raw_list_sequence(self):
eq_(self.module._distill_raw_params([(1, 2, 3)]), [(1, 2, 3)])
eq_(
self.module._distill_raw_params([(1, 2), (2, 3)]), [(1, 2), (2, 3)]
)
def test_distill_raw_list_dict(self):
eq_(
self.module._distill_raw_params([{"a": 1}, {"a": 2}]),
[{"a": 1}, {"a": 2}],
)
eq_(
self.module._distill_raw_params([MappingProxyType({"a": 1})]),
[MappingProxyType({"a": 1})],
)
def test_distill_raw_sequence_error(self):
with expect_raises_message(
exc.ArgumentError,
"List argument must consist only of tuples or dictionaries",
):
self.module._distill_raw_params([1, 2, 3])
with expect_raises_message(
exc.ArgumentError,
"List argument must consist only of tuples or dictionaries",
):
self.module._distill_raw_params([[1, 2, 3]])
with expect_raises_message(
exc.ArgumentError,
"List argument must consist only of tuples or dictionaries",
):
self.module._distill_raw_params(["a", "b"])
def test_distill_raw_tuple(self):
eq_(self.module._distill_raw_params(()), [()])
eq_(self.module._distill_raw_params((1, 2, 3)), [(1, 2, 3)])
def test_distill_raw_dict(self):
eq_(self.module._distill_raw_params({"foo": "bar"}), [{"foo": "bar"}])
eq_(
self.module._distill_raw_params(immutabledict({"foo": "bar"})),
[immutabledict({"foo": "bar"})],
)
eq_(
self.module._distill_raw_params(MappingProxyType({"foo": "bar"})),
[MappingProxyType({"foo": "bar"})],
)
def test_distill_raw_error(self):
with expect_raises_message(
exc.ArgumentError, "mapping or sequence expected for parameters"
):
self.module._distill_raw_params("foo")
with expect_raises_message(
exc.ArgumentError, "mapping or sequence expected for parameters"
):
self.module._distill_raw_params(1)
class PyDistillArgsTest(_DistillArgsTest):
@classmethod
def setup_test_class(cls):
from sqlalchemy.engine import _py_util
cls.module = _py_util
class CyDistillArgsTest(_DistillArgsTest):
__requires__ = ("cextensions",)
@classmethod
def setup_test_class(cls):
from sqlalchemy.cyextension import util
cls.module = util
|