File: test_deprecations.py

package info (click to toggle)
sqlalchemy 1.3.22%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,916 kB
  • sloc: python: 263,220; ansic: 1,342; makefile: 255; xml: 17
file content (587 lines) | stat: -rw-r--r-- 20,691 bytes parent folder | download
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#! coding: utf-8

from copy import deepcopy

import pytest

from sqlalchemy import bindparam
from sqlalchemy import Column
from sqlalchemy import column
from sqlalchemy import create_engine
from sqlalchemy import DateTime
from sqlalchemy import exc
from sqlalchemy import ForeignKey
from sqlalchemy import func
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import table
from sqlalchemy import testing
from sqlalchemy import text
from sqlalchemy import util
from sqlalchemy.engine import default
from sqlalchemy.schema import DDL
from sqlalchemy.sql import functions
from sqlalchemy.sql import util as sql_util
from sqlalchemy.sql.functions import GenericFunction
from sqlalchemy.sql.sqltypes import NullType
from sqlalchemy.testing import assert_raises
from sqlalchemy.testing import assert_raises_message
from sqlalchemy.testing import AssertsCompiledSQL
from sqlalchemy.testing import engines
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import in_
from sqlalchemy.testing import mock
from sqlalchemy.testing import not_in


class DeprecationWarningsTest(fixtures.TestBase):
    __backend__ = True

    def test_ident_preparer_force(self):
        preparer = testing.db.dialect.identifier_preparer
        preparer.quote("hi")
        with testing.expect_deprecated(
            "The IdentifierPreparer.quote.force parameter is deprecated"
        ):
            preparer.quote("hi", True)

        with testing.expect_deprecated(
            "The IdentifierPreparer.quote.force parameter is deprecated"
        ):
            preparer.quote("hi", False)

        preparer.quote_schema("hi")
        with testing.expect_deprecated(
            "The IdentifierPreparer.quote_schema.force parameter is deprecated"
        ):
            preparer.quote_schema("hi", True)

        with testing.expect_deprecated(
            "The IdentifierPreparer.quote_schema.force parameter is deprecated"
        ):
            preparer.quote_schema("hi", True)

    def test_string_convert_unicode(self):
        with testing.expect_deprecated(
            "The String.convert_unicode parameter is deprecated and "
            "will be removed in a future release."
        ):
            String(convert_unicode=True)

    def test_string_convert_unicode_force(self):
        with testing.expect_deprecated(
            "The String.convert_unicode parameter is deprecated and "
            "will be removed in a future release."
        ):
            String(convert_unicode="force")

    def test_engine_convert_unicode(self):
        with testing.expect_deprecated(
            "The create_engine.convert_unicode parameter and "
            "corresponding dialect-level"
        ):
            create_engine("mysql://", convert_unicode=True, module=mock.Mock())

    def test_join_condition_ignore_nonexistent_tables(self):
        m = MetaData()
        t1 = Table("t1", m, Column("id", Integer))
        t2 = Table(
            "t2", m, Column("id", Integer), Column("t1id", ForeignKey("t1.id"))
        )
        with testing.expect_deprecated(
            "The join_condition.ignore_nonexistent_tables "
            "parameter is deprecated"
        ):
            join_cond = sql_util.join_condition(
                t1, t2, ignore_nonexistent_tables=True
            )

        t1t2 = t1.join(t2)

        assert t1t2.onclause.compare(join_cond)

    def test_select_autocommit(self):
        with testing.expect_deprecated(
            "The select.autocommit parameter is deprecated and "
            "will be removed in a future release."
        ):
            select([column("x")], autocommit=True)

    def test_select_for_update(self):
        with testing.expect_deprecated(
            "The select.for_update parameter is deprecated and "
            "will be removed in a future release."
        ):
            select([column("x")], for_update=True)

    @testing.provide_metadata
    def test_table_useexisting(self):
        meta = self.metadata

        Table("t", meta, Column("x", Integer))
        meta.create_all()

        with testing.expect_deprecated(
            "The Table.useexisting parameter is deprecated and "
            "will be removed in a future release."
        ):
            Table("t", meta, useexisting=True, autoload_with=testing.db)

        with testing.expect_deprecated(
            "The Table.useexisting parameter is deprecated and "
            "will be removed in a future release."
        ):
            assert_raises_message(
                exc.ArgumentError,
                "useexisting is synonymous with extend_existing.",
                Table,
                "t",
                meta,
                useexisting=True,
                extend_existing=True,
                autoload_with=testing.db,
            )


class CaseSensitiveFunctionDeprecationsTest(fixtures.TestBase):
    def setup(self):
        self._registry = deepcopy(functions._registry)
        self._case_sensitive_registry = deepcopy(
            functions._case_sensitive_registry
        )
        functions._registry.clear()
        functions._case_sensitive_registry.clear()

    def teardown(self):
        functions._registry = self._registry
        functions._case_sensitive_registry = self._case_sensitive_registry

    def test_case_sensitive(self):
        reg = functions._registry["_default"]
        cs_reg = functions._case_sensitive_registry["_default"]

        class MYFUNC(GenericFunction):
            type = DateTime

        assert isinstance(func.MYFUNC().type, DateTime)
        assert isinstance(func.MyFunc().type, DateTime)
        assert isinstance(func.mYfUnC().type, DateTime)
        assert isinstance(func.myfunc().type, DateTime)

        in_("myfunc", reg)
        not_in("MYFUNC", reg)
        not_in("MyFunc", reg)
        in_("myfunc", cs_reg)
        eq_(set(cs_reg["myfunc"].keys()), set(["MYFUNC"]))

        with testing.expect_deprecated(
            "GenericFunction 'MyFunc' is already registered with"
            " different letter case, so the previously registered function "
            "'MYFUNC' is switched into case-sensitive mode. "
            "GenericFunction objects will be fully case-insensitive in a "
            "future release.",
            regex=False,
        ):

            class MyFunc(GenericFunction):
                type = Integer

        assert isinstance(func.MYFUNC().type, DateTime)
        assert isinstance(func.MyFunc().type, Integer)
        with pytest.raises(AssertionError):
            assert isinstance(func.mYfUnC().type, Integer)
        with pytest.raises(AssertionError):
            assert isinstance(func.myfunc().type, Integer)

        eq_(reg["myfunc"], functions._CASE_SENSITIVE)
        not_in("MYFUNC", reg)
        not_in("MyFunc", reg)
        in_("myfunc", cs_reg)
        eq_(set(cs_reg["myfunc"].keys()), set(["MYFUNC", "MyFunc"]))

    def test_replace_function_case_sensitive(self):
        reg = functions._registry["_default"]
        cs_reg = functions._case_sensitive_registry["_default"]

        class replaceable_func(GenericFunction):
            type = Integer
            identifier = "REPLACEABLE_FUNC"

        assert isinstance(func.REPLACEABLE_FUNC().type, Integer)
        assert isinstance(func.Replaceable_Func().type, Integer)
        assert isinstance(func.RePlAcEaBlE_fUnC().type, Integer)
        assert isinstance(func.replaceable_func().type, Integer)

        in_("replaceable_func", reg)
        not_in("REPLACEABLE_FUNC", reg)
        not_in("Replaceable_Func", reg)
        in_("replaceable_func", cs_reg)
        eq_(set(cs_reg["replaceable_func"].keys()), set(["REPLACEABLE_FUNC"]))

        with testing.expect_deprecated(
            "GenericFunction 'Replaceable_Func' is already registered with"
            " different letter case, so the previously registered function "
            "'REPLACEABLE_FUNC' is switched into case-sensitive mode. "
            "GenericFunction objects will be fully case-insensitive in a "
            "future release.",
            regex=False,
        ):

            class Replaceable_Func(GenericFunction):
                type = DateTime
                identifier = "Replaceable_Func"

        assert isinstance(func.REPLACEABLE_FUNC().type, Integer)
        assert isinstance(func.Replaceable_Func().type, DateTime)
        assert isinstance(func.RePlAcEaBlE_fUnC().type, NullType)
        assert isinstance(func.replaceable_func().type, NullType)

        eq_(reg["replaceable_func"], functions._CASE_SENSITIVE)
        not_in("REPLACEABLE_FUNC", reg)
        not_in("Replaceable_Func", reg)
        in_("replaceable_func", cs_reg)
        eq_(
            set(cs_reg["replaceable_func"].keys()),
            set(["REPLACEABLE_FUNC", "Replaceable_Func"]),
        )

        with testing.expect_warnings(
            "The GenericFunction 'REPLACEABLE_FUNC' is already registered and "
            "is going to be overriden.",
            regex=False,
        ):

            class replaceable_func_override(GenericFunction):
                type = DateTime
                identifier = "REPLACEABLE_FUNC"

        with testing.expect_deprecated(
            "GenericFunction(s) '['REPLACEABLE_FUNC', 'Replaceable_Func']' "
            "are already registered with different letter cases and might "
            "interact with 'replaceable_func'. GenericFunction objects will "
            "be fully case-insensitive in a future release.",
            regex=False,
        ):

            class replaceable_func_lowercase(GenericFunction):
                type = String
                identifier = "replaceable_func"

        with testing.expect_warnings(
            "The GenericFunction 'Replaceable_Func' is already registered and "
            "is going to be overriden.",
            regex=False,
        ):

            class Replaceable_Func_override(GenericFunction):
                type = Integer
                identifier = "Replaceable_Func"

        assert isinstance(func.REPLACEABLE_FUNC().type, DateTime)
        assert isinstance(func.Replaceable_Func().type, Integer)
        assert isinstance(func.RePlAcEaBlE_fUnC().type, NullType)
        assert isinstance(func.replaceable_func().type, String)

        eq_(reg["replaceable_func"], functions._CASE_SENSITIVE)
        not_in("REPLACEABLE_FUNC", reg)
        not_in("Replaceable_Func", reg)
        in_("replaceable_func", cs_reg)
        eq_(
            set(cs_reg["replaceable_func"].keys()),
            set(["REPLACEABLE_FUNC", "Replaceable_Func", "replaceable_func"]),
        )


class DDLListenerDeprecationsTest(fixtures.TestBase):
    def setup(self):
        self.bind = self.engine = engines.mock_engine()
        self.metadata = MetaData(self.bind)
        self.table = Table("t", self.metadata, Column("id", Integer))
        self.users = Table(
            "users",
            self.metadata,
            Column("user_id", Integer, primary_key=True),
            Column("user_name", String(40)),
        )

    def test_append_listener(self):
        metadata, table = self.metadata, self.table

        def fn(*a):
            return None

        with testing.expect_deprecated(".* is deprecated .*"):
            table.append_ddl_listener("before-create", fn)
        with testing.expect_deprecated(".* is deprecated .*"):
            assert_raises(
                exc.InvalidRequestError, table.append_ddl_listener, "blah", fn
            )

        with testing.expect_deprecated(".* is deprecated .*"):
            metadata.append_ddl_listener("before-create", fn)
        with testing.expect_deprecated(".* is deprecated .*"):
            assert_raises(
                exc.InvalidRequestError,
                metadata.append_ddl_listener,
                "blah",
                fn,
            )

    def test_deprecated_append_ddl_listener_table(self):
        metadata, users, engine = self.metadata, self.users, self.engine
        canary = []
        with testing.expect_deprecated(".* is deprecated .*"):
            users.append_ddl_listener(
                "before-create", lambda e, t, b: canary.append("mxyzptlk")
            )
        with testing.expect_deprecated(".* is deprecated .*"):
            users.append_ddl_listener(
                "after-create", lambda e, t, b: canary.append("klptzyxm")
            )
        with testing.expect_deprecated(".* is deprecated .*"):
            users.append_ddl_listener(
                "before-drop", lambda e, t, b: canary.append("xyzzy")
            )
        with testing.expect_deprecated(".* is deprecated .*"):
            users.append_ddl_listener(
                "after-drop", lambda e, t, b: canary.append("fnord")
            )

        metadata.create_all()
        assert "mxyzptlk" in canary
        assert "klptzyxm" in canary
        assert "xyzzy" not in canary
        assert "fnord" not in canary
        del engine.mock[:]
        canary[:] = []
        metadata.drop_all()
        assert "mxyzptlk" not in canary
        assert "klptzyxm" not in canary
        assert "xyzzy" in canary
        assert "fnord" in canary

    def test_deprecated_append_ddl_listener_metadata(self):
        metadata, engine = self.metadata, self.engine
        canary = []
        with testing.expect_deprecated(".* is deprecated .*"):
            metadata.append_ddl_listener(
                "before-create",
                lambda e, t, b, tables=None: canary.append("mxyzptlk"),
            )
        with testing.expect_deprecated(".* is deprecated .*"):
            metadata.append_ddl_listener(
                "after-create",
                lambda e, t, b, tables=None: canary.append("klptzyxm"),
            )
        with testing.expect_deprecated(".* is deprecated .*"):
            metadata.append_ddl_listener(
                "before-drop",
                lambda e, t, b, tables=None: canary.append("xyzzy"),
            )
        with testing.expect_deprecated(".* is deprecated .*"):
            metadata.append_ddl_listener(
                "after-drop",
                lambda e, t, b, tables=None: canary.append("fnord"),
            )

        metadata.create_all()
        assert "mxyzptlk" in canary
        assert "klptzyxm" in canary
        assert "xyzzy" not in canary
        assert "fnord" not in canary
        del engine.mock[:]
        canary[:] = []
        metadata.drop_all()
        assert "mxyzptlk" not in canary
        assert "klptzyxm" not in canary
        assert "xyzzy" in canary
        assert "fnord" in canary

    def test_filter_deprecated(self):
        cx = self.engine

        tbl = Table("t", MetaData(), Column("id", Integer))
        target = cx.name

        assert DDL("")._should_execute_deprecated("x", tbl, cx)
        with testing.expect_deprecated(".* is deprecated .*"):
            assert DDL("", on=target)._should_execute_deprecated("x", tbl, cx)
        with testing.expect_deprecated(".* is deprecated .*"):
            assert not DDL("", on="bogus")._should_execute_deprecated(
                "x", tbl, cx
            )
        with testing.expect_deprecated(".* is deprecated .*"):
            assert DDL(
                "", on=lambda d, x, y, z: True
            )._should_execute_deprecated("x", tbl, cx)
        with testing.expect_deprecated(".* is deprecated .*"):
            assert DDL(
                "", on=lambda d, x, y, z: z.engine.name != "bogus"
            )._should_execute_deprecated("x", tbl, cx)


class ConvertUnicodeDeprecationTest(fixtures.TestBase):

    __backend__ = True

    data = util.u(
        "Alors vous imaginez ma surprise, au lever du jour, quand "
        "une drôle de petite voix m’a réveillé. "
        "Elle disait: « S’il vous plaît… dessine-moi un mouton! »"
    )

    def test_unicode_warnings_dialectlevel(self):

        unicodedata = self.data

        with testing.expect_deprecated(
            "The create_engine.convert_unicode parameter and "
            "corresponding dialect-level"
        ):
            dialect = default.DefaultDialect(convert_unicode=True)
        dialect.supports_unicode_binds = False

        s = String()
        uni = s.dialect_impl(dialect).bind_processor(dialect)

        uni(util.b("x"))
        assert isinstance(uni(unicodedata), util.binary_type)

        eq_(uni(unicodedata), unicodedata.encode("utf-8"))

    def test_ignoring_unicode_error(self):
        """checks String(unicode_error='ignore') is passed to
        underlying codec."""

        unicodedata = self.data

        with testing.expect_deprecated(
            "The String.convert_unicode parameter is deprecated and "
            "will be removed in a future release.",
            "The String.unicode_errors parameter is deprecated and "
            "will be removed in a future release.",
        ):
            type_ = String(
                248, convert_unicode="force", unicode_error="ignore"
            )
        dialect = default.DefaultDialect(encoding="ascii")
        proc = type_.result_processor(dialect, 10)

        utfdata = unicodedata.encode("utf8")
        eq_(proc(utfdata), unicodedata.encode("ascii", "ignore").decode())


class ForUpdateTest(fixtures.TestBase, AssertsCompiledSQL):
    __dialect__ = "default"

    def _assert_legacy(self, leg, read=False, nowait=False):
        t = table("t", column("c"))

        with testing.expect_deprecated(
            "The select.for_update parameter is deprecated and "
            "will be removed in a future release."
        ):
            s1 = select([t], for_update=leg)

        if leg is False:
            assert s1._for_update_arg is None
            assert s1.for_update is None
        else:
            eq_(s1._for_update_arg.read, read)
            eq_(s1._for_update_arg.nowait, nowait)
            eq_(s1.for_update, leg)

    def test_false_legacy(self):
        self._assert_legacy(False)

    def test_plain_true_legacy(self):
        self._assert_legacy(True)

    def test_read_legacy(self):
        self._assert_legacy("read", read=True)

    def test_nowait_legacy(self):
        self._assert_legacy("nowait", nowait=True)

    def test_read_nowait_legacy(self):
        self._assert_legacy("read_nowait", read=True, nowait=True)

    def test_unknown_mode(self):
        t = table("t", column("c"))

        with testing.expect_deprecated(
            "The select.for_update parameter is deprecated and "
            "will be removed in a future release."
        ):
            assert_raises_message(
                exc.ArgumentError,
                "Unknown for_update argument: 'unknown_mode'",
                t.select,
                t.c.c == 7,
                for_update="unknown_mode",
            )

    def test_legacy_setter(self):
        t = table("t", column("c"))
        s = select([t])
        s.for_update = "nowait"
        eq_(s._for_update_arg.nowait, True)


class TextTest(fixtures.TestBase, AssertsCompiledSQL):
    __dialect__ = "default"

    def test_legacy_bindparam(self):
        with testing.expect_deprecated(
            "The text.bindparams parameter is deprecated"
        ):
            t = text(
                "select * from foo where lala=:bar and hoho=:whee",
                bindparams=[bindparam("bar", 4), bindparam("whee", 7)],
            )

        self.assert_compile(
            t,
            "select * from foo where lala=:bar and hoho=:whee",
            checkparams={"bar": 4, "whee": 7},
        )

    def test_legacy_typemap(self):
        table1 = table(
            "mytable",
            column("myid", Integer),
            column("name", String),
            column("description", String),
        )
        with testing.expect_deprecated(
            "The text.typemap parameter is deprecated"
        ):
            t = text(
                "select id, name from user",
                typemap=dict(id=Integer, name=String),
            )

        stmt = select([table1.c.myid]).select_from(
            table1.join(t, table1.c.myid == t.c.id)
        )
        compiled = stmt.compile()
        eq_(
            compiled._create_result_map(),
            {
                "myid": (
                    "myid",
                    (table1.c.myid, "myid", "myid"),
                    table1.c.myid.type,
                )
            },
        )

    def test_autocommit(self):
        with testing.expect_deprecated(
            "The text.autocommit parameter is deprecated"
        ):
            text("select id, name from user", autocommit=True)