diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-03-17 08:39:45 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-03-17 12:30:22 -0400 |
| commit | aabc72bd33ba445c0a207432acf0aa1cf25263cb (patch) | |
| tree | f7c4608bb4a9a6e8516b4bcc24935bb055711f39 /test/sql | |
| parent | ecb392c5f927ab117f9704ce373bf2af1dbe5b69 (diff) | |
| download | sqlalchemy-aabc72bd33ba445c0a207432acf0aa1cf25263cb.tar.gz | |
Provide special row proxies for count and index
The Python ``namedtuple()`` has the behavior such that the names ``count``
and ``index`` will be served as tuple values if the named tuple includes
those names; if they are absent, then their behavior as methods of
``collections.abc.Sequence`` is maintained. Therefore the
:class:`_result.Row` and :class:`_result.LegacyRow` classes have been fixed
so that they work in this same way, maintaining the expected behavior for
database rows that have columns named "index" or "count".
Fixes: #6074
Change-Id: I49a093da02f33f231d22ed5999c09fcaa3a68601
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_resultset.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test/sql/test_resultset.py b/test/sql/test_resultset.py index e99ce881c..5439d63b5 100644 --- a/test/sql/test_resultset.py +++ b/test/sql/test_resultset.py @@ -28,6 +28,8 @@ from sqlalchemy import VARCHAR from sqlalchemy.engine import cursor as _cursor from sqlalchemy.engine import default from sqlalchemy.engine import Row +from sqlalchemy.engine.result import SimpleResultMetaData +from sqlalchemy.engine.row import LegacyRow from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql import ColumnElement from sqlalchemy.sql import expression @@ -1324,6 +1326,47 @@ class CursorResultTest(fixtures.TablesTest): ) is_true(isinstance(row, collections_abc.Sequence)) + @testing.combinations((Row,), (LegacyRow,)) + def test_row_special_names(self, row_cls): + metadata = SimpleResultMetaData(["key", "count", "index"]) + row = row_cls( + metadata, + [None, None, None], + metadata._keymap, + Row._default_key_style, + ["kv", "cv", "iv"], + ) + is_true(isinstance(row, collections_abc.Sequence)) + + eq_(row.key, "kv") + eq_(row.count, "cv") + eq_(row.index, "iv") + + if isinstance(row, LegacyRow): + eq_(row["count"], "cv") + eq_(row["index"], "iv") + + eq_(row._mapping["count"], "cv") + eq_(row._mapping["index"], "iv") + + metadata = SimpleResultMetaData(["key", "q", "p"]) + + row = row_cls( + metadata, + [None, None, None], + metadata._keymap, + Row._default_key_style, + ["kv", "cv", "iv"], + ) + is_true(isinstance(row, collections_abc.Sequence)) + + eq_(row.key, "kv") + eq_(row.q, "cv") + eq_(row.p, "iv") + eq_(row.index("cv"), 1) + eq_(row.count("cv"), 1) + eq_(row.count("x"), 0) + def test_row_is_hashable(self): row = Row( |
