diff options
| author | Gord Thompson <gord@gordthompson.com> | 2020-12-07 18:37:29 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-12-08 19:54:05 -0500 |
| commit | b71e46f0470964358d44aec08f940260f78691f0 (patch) | |
| tree | 44c9b5934ad550b4688700e8124204411e42190f /test/sql | |
| parent | 18b1b261ff988549e75b011f2f4296fb13b24d64 (diff) | |
| download | sqlalchemy-b71e46f0470964358d44aec08f940260f78691f0.tar.gz | |
Implement `TypeEngine.as_generic`
Added :meth:`_types.TypeEngine.as_generic` to map dialect-specific types,
such as :class:`sqlalchemy.dialects.mysql.INTEGER`, with the "best match"
generic SQLAlchemy type, in this case :class:`_types.Integer`. Pull
request courtesy Andrew Hannigan.
Abstract away how we check for "overridden methods" so it is more
clear what the intent is and that the methodology can be
independently tested.
Fixes: #5659
Closes: #5714
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5714
Pull-request-sha: 91afb9a0ba3bfa81a1ded80c025989213cf6e4eb
Change-Id: Ic54d6690ecc10dc69e6e72856d5620036cea472a
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_types.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/test/sql/test_types.py b/test/sql/test_types.py index fd1783e09..3178eb09a 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -58,6 +58,9 @@ from sqlalchemy import types from sqlalchemy import Unicode from sqlalchemy import util from sqlalchemy import VARCHAR +import sqlalchemy.dialects.mysql as mysql +import sqlalchemy.dialects.oracle as oracle +import sqlalchemy.dialects.postgresql as pg from sqlalchemy.engine import default from sqlalchemy.schema import AddConstraint from sqlalchemy.schema import CheckConstraint @@ -69,6 +72,7 @@ from sqlalchemy.sql import operators from sqlalchemy.sql import sqltypes from sqlalchemy.sql import table from sqlalchemy.sql import visitors +from sqlalchemy.sql.sqltypes import TypeEngine from sqlalchemy.testing import assert_raises from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import AssertsCompiledSQL @@ -383,6 +387,69 @@ class TypeAffinityTest(fixtures.TestBase): assert t1.dialect_impl(d)._type_affinity is postgresql.UUID +class AsGenericTest(fixtures.TestBase): + @testing.combinations( + (String(), String()), + (VARCHAR(length=100), String(length=100)), + (NVARCHAR(length=100), Unicode(length=100)), + (DATE(), Date()), + (pg.JSON(), sa.JSON()), + (pg.ARRAY(sa.String), sa.ARRAY(sa.String)), + (Enum("a", "b", "c"), Enum("a", "b", "c")), + (pg.ENUM("a", "b", "c"), Enum("a", "b", "c")), + (mysql.ENUM("a", "b", "c"), Enum("a", "b", "c")), + (pg.INTERVAL(precision=5), Interval(native=True, second_precision=5)), + ( + oracle.INTERVAL(second_precision=5, day_precision=5), + Interval(native=True, day_precision=5, second_precision=5), + ), + ) + def test_as_generic(self, t1, t2): + assert repr(t1.as_generic(allow_nulltype=False)) == repr(t2) + + @testing.combinations( + *[ + (t,) + for t in _all_types(omit_special_types=True) + if not util.method_is_overridden(t, TypeEngine.as_generic) + ] + ) + def test_as_generic_all_types_heuristic(self, type_): + if issubclass(type_, ARRAY): + t1 = type_(String) + else: + t1 = type_() + + try: + gentype = t1.as_generic() + except NotImplementedError: + pass + else: + assert isinstance(t1, gentype.__class__) + assert isinstance(gentype, TypeEngine) + + gentype = t1.as_generic(allow_nulltype=True) + if not isinstance(gentype, types.NULLTYPE.__class__): + assert isinstance(t1, gentype.__class__) + assert isinstance(gentype, TypeEngine) + + @testing.combinations( + *[ + (t,) + for t in _all_types(omit_special_types=True) + if util.method_is_overridden(t, TypeEngine.as_generic) + ] + ) + def test_as_generic_all_types_custom(self, type_): + if issubclass(type_, ARRAY): + t1 = type_(String) + else: + t1 = type_() + + gentype = t1.as_generic(allow_nulltype=False) + assert isinstance(gentype, TypeEngine) + + class PickleTypesTest(fixtures.TestBase): @testing.combinations( ("Boo", Boolean()), |
