summaryrefslogtreecommitdiff
path: root/test/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2021-12-09 14:23:42 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2021-12-09 14:23:42 -0500
commitf113e979219e20a22044c4b262e4531ba9993b8a (patch)
treef0c3f868f1e7ca64ffa5518208b81945b7a723f0 /test/sql
parentaf50c8064d668ba33ef2399a288fd1594b1b5602 (diff)
downloadsqlalchemy-f113e979219e20a22044c4b262e4531ba9993b8a.tar.gz
implement correct errors for Row immutability
Corrected the error message for the ``AttributeError`` that's raised when attempting to write to an attribute on the :class:`_result.Row` class, which is immutable. The previous message claimed the column didn't exist which is misleading. Fixes: #7432 Change-Id: If0e2cbd3f763dca6c99a18aa42252c69f1207d59
Diffstat (limited to 'test/sql')
-rw-r--r--test/sql/test_resultset.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/test/sql/test_resultset.py b/test/sql/test_resultset.py
index e4f07a758..e5b1a0a26 100644
--- a/test/sql/test_resultset.py
+++ b/test/sql/test_resultset.py
@@ -746,6 +746,80 @@ class CursorResultTest(fixtures.TablesTest):
eq_(r._mapping[users.c.user_name], "john")
eq_(r.user_name, "john")
+ @testing.fixture
+ def _ab_row_fixture(self, connection):
+ r = connection.execute(
+ select(literal(1).label("a"), literal(2).label("b"))
+ ).first()
+ return r
+
+ def test_named_tuple_access(self, _ab_row_fixture):
+ r = _ab_row_fixture
+ eq_(r.a, 1)
+ eq_(r.b, 2)
+
+ def test_named_tuple_missing_attr(self, _ab_row_fixture):
+ r = _ab_row_fixture
+ with expect_raises_message(
+ AttributeError, "Could not locate column in row for column 'c'"
+ ):
+ r.c
+
+ def test_named_tuple_no_delete_present(self, _ab_row_fixture):
+ r = _ab_row_fixture
+ with expect_raises_message(AttributeError, "can't delete attribute"):
+ del r.a
+
+ def test_named_tuple_no_delete_missing(self, _ab_row_fixture):
+ r = _ab_row_fixture
+ # including for non-existent attributes
+ with expect_raises_message(AttributeError, "can't delete attribute"):
+ del r.c
+
+ def test_named_tuple_no_assign_present(self, _ab_row_fixture):
+ r = _ab_row_fixture
+ with expect_raises_message(AttributeError, "can't set attribute"):
+ r.a = 5
+
+ with expect_raises_message(AttributeError, "can't set attribute"):
+ r.a += 5
+
+ def test_named_tuple_no_assign_missing(self, _ab_row_fixture):
+ r = _ab_row_fixture
+ # including for non-existent attributes
+ with expect_raises_message(AttributeError, "can't set attribute"):
+ r.c = 5
+
+ def test_named_tuple_no_self_assign_missing(self, _ab_row_fixture):
+ r = _ab_row_fixture
+ with expect_raises_message(
+ AttributeError, "Could not locate column in row for column 'c'"
+ ):
+ r.c += 5
+
+ def test_mapping_tuple_readonly_errors(self, connection):
+ r = connection.execute(
+ select(literal(1).label("a"), literal(2).label("b"))
+ ).first()
+ r = r._mapping
+ eq_(r["a"], 1)
+ eq_(r["b"], 2)
+
+ with expect_raises_message(
+ KeyError, "Could not locate column in row for column 'c'"
+ ):
+ r["c"]
+
+ with expect_raises_message(
+ TypeError, "'RowMapping' object does not support item assignment"
+ ):
+ r["a"] = 5
+
+ with expect_raises_message(
+ TypeError, "'RowMapping' object does not support item assignment"
+ ):
+ r["a"] += 5
+
def test_column_accessor_err(self, connection):
r = connection.execute(select(1)).first()
assert_raises_message(