diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2020-08-21 21:39:40 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2020-08-21 21:39:40 +0000 |
| commit | 317f2e1be2b06cdc12bc84510eb743d9752763dd (patch) | |
| tree | acf50269494e5a14ec58ef87e511a8a93f1b263d /lib/sqlalchemy/testing | |
| parent | 9b6b867fe59d74c23edca782dcbba9af99b62817 (diff) | |
| parent | 26e8d3b5bdee50192e3426fba48e6b326e428e0b (diff) | |
| download | sqlalchemy-317f2e1be2b06cdc12bc84510eb743d9752763dd.tar.gz | |
Merge "Add support for identity columns"
Diffstat (limited to 'lib/sqlalchemy/testing')
| -rw-r--r-- | lib/sqlalchemy/testing/plugin/pytestplugin.py | 9 | ||||
| -rw-r--r-- | lib/sqlalchemy/testing/requirements.py | 14 | ||||
| -rw-r--r-- | lib/sqlalchemy/testing/suite/test_select.py | 80 |
3 files changed, 95 insertions, 8 deletions
diff --git a/lib/sqlalchemy/testing/plugin/pytestplugin.py b/lib/sqlalchemy/testing/plugin/pytestplugin.py index 1b2bbca23..ca3fbe4a8 100644 --- a/lib/sqlalchemy/testing/plugin/pytestplugin.py +++ b/lib/sqlalchemy/testing/plugin/pytestplugin.py @@ -242,15 +242,10 @@ def pytest_pycollect_makeitem(collector, name, obj): if inspect.isclass(obj) and plugin_base.want_class(name, obj): - # in pytest 5.4.0 - # return [ - # pytest.Class.from_parent(collector, - # name=parametrize_cls.__name__) - # for parametrize_cls in _parametrize_cls(collector.module, obj) - # ] + ctor = getattr(pytest.Class, "from_parent", pytest.Class) return [ - pytest.Class(parametrize_cls.__name__, parent=collector) + ctor(name=parametrize_cls.__name__, parent=collector) for parametrize_cls in _parametrize_cls(collector.module, obj) ] elif ( diff --git a/lib/sqlalchemy/testing/requirements.py b/lib/sqlalchemy/testing/requirements.py index 301c9ef84..cc40022e9 100644 --- a/lib/sqlalchemy/testing/requirements.py +++ b/lib/sqlalchemy/testing/requirements.py @@ -1264,3 +1264,17 @@ class SuiteRequirements(Requirements): lambda config: not config.db.dialect.supports_is_distinct_from, "driver doesn't support an IS DISTINCT FROM construct", ) + + @property + def identity_columns(self): + """If a backend supports GENERATED { ALWAYS | BY DEFAULT } + AS IDENTITY""" + return exclusions.closed() + + @property + def identity_columns_standard(self): + """If a backend supports GENERATED { ALWAYS | BY DEFAULT } + AS IDENTITY with a standard syntax. + This is mainly to exclude MSSql. + """ + return exclusions.closed() diff --git a/lib/sqlalchemy/testing/suite/test_select.py b/lib/sqlalchemy/testing/suite/test_select.py index cff1f2cfc..675fac609 100644 --- a/lib/sqlalchemy/testing/suite/test_select.py +++ b/lib/sqlalchemy/testing/suite/test_select.py @@ -4,6 +4,7 @@ from .. import AssertsCompiledSQL from .. import AssertsExecutionResults from .. import config from .. import fixtures +from ..assertions import assert_raises from ..assertions import eq_ from ..assertions import in_ from ..assertsql import CursorSQL @@ -17,6 +18,7 @@ from ... import exists from ... import false from ... import ForeignKey from ... import func +from ... import Identity from ... import Integer from ... import literal from ... import literal_column @@ -30,6 +32,8 @@ from ... import true from ... import tuple_ from ... import union from ... import util +from ...exc import DatabaseError +from ...exc import ProgrammingError class CollateTest(fixtures.TablesTest): @@ -1044,6 +1048,81 @@ class ComputedColumnTest(fixtures.TablesTest): eq_(res, [(100, 40), (1764, 168)]) +class IdentityColumnTest(fixtures.TablesTest): + __backend__ = True + __requires__ = ("identity_columns",) + run_inserts = "once" + run_deletes = "once" + + @classmethod + def define_tables(cls, metadata): + Table( + "tbl_a", + metadata, + Column( + "id", + Integer, + Identity(always=True, start=42), + primary_key=True, + ), + Column("desc", String(100)), + ) + Table( + "tbl_b", + metadata, + Column( + "id", + Integer, + Identity(increment=-5, start=0, minvalue=-1000, maxvalue=0,), + primary_key=True, + ), + Column("desc", String(100)), + ) + + @classmethod + def insert_data(cls, connection): + connection.execute( + cls.tables.tbl_a.insert(), [{"desc": "a"}, {"desc": "b"}], + ) + connection.execute( + cls.tables.tbl_b.insert(), [{"desc": "a"}, {"desc": "b"}], + ) + connection.execute( + cls.tables.tbl_b.insert(), [{"id": 42, "desc": "c"}], + ) + + def test_select_all(self, connection): + res = connection.execute( + select([text("*")]) + .select_from(self.tables.tbl_a) + .order_by(self.tables.tbl_a.c.id) + ).fetchall() + eq_(res, [(42, "a"), (43, "b")]) + + res = connection.execute( + select([text("*")]) + .select_from(self.tables.tbl_b) + .order_by(self.tables.tbl_b.c.id) + ).fetchall() + eq_(res, [(-5, "b"), (0, "a"), (42, "c")]) + + def test_select_columns(self, connection): + + res = connection.execute( + select([self.tables.tbl_a.c.id]).order_by(self.tables.tbl_a.c.id) + ).fetchall() + eq_(res, [(42,), (43,)]) + + @testing.requires.identity_columns_standard + def test_insert_always_error(self, connection): + def fn(): + connection.execute( + self.tables.tbl_a.insert(), [{"id": 200, "desc": "a"}], + ) + + assert_raises((DatabaseError, ProgrammingError), fn) + + class ExistsTest(fixtures.TablesTest): __backend__ = True @@ -1093,7 +1172,6 @@ class ExistsTest(fixtures.TablesTest): class DistinctOnTest(AssertsCompiledSQL, fixtures.TablesTest): __backend__ = True - __requires__ = ("standard_cursor_sql",) @testing.fails_if(testing.requires.supports_distinct_on) def test_distinct_on(self): |
