|
21 | 21 |
|
22 | 22 | from __future__ import absolute_import |
23 | 23 |
|
| 24 | +import re |
24 | 25 | from unittest import TestCase, skipIf |
25 | 26 | from unittest.mock import MagicMock, patch |
26 | 27 |
|
|
29 | 30 | from sqlalchemy.sql import select |
30 | 31 |
|
31 | 32 | try: |
32 | | - from sqlalchemy.orm import declarative_base |
| 33 | + from sqlalchemy.orm import Mapped, declarative_base, mapped_column |
33 | 34 | except ImportError: |
34 | 35 | from sqlalchemy.ext.declarative import declarative_base |
35 | 36 |
|
| 37 | + Mapped = None |
| 38 | + mapped_column = None |
| 39 | + |
36 | 40 | from crate.client.cursor import Cursor |
37 | 41 |
|
38 | 42 | from sqlalchemy_cratedb import ObjectArray, ObjectType |
39 | | -from sqlalchemy_cratedb.sa_version import SA_1_4, SA_VERSION |
| 43 | +from sqlalchemy_cratedb.sa_version import SA_1_4, SA_2_1, SA_VERSION |
40 | 44 |
|
41 | 45 | fake_cursor = MagicMock(name="fake_cursor") |
42 | 46 | FakeCursor = MagicMock(name="FakeCursor", spec=Cursor) |
@@ -96,21 +100,83 @@ def test_update_with_dict_column(self): |
96 | 100 | self.assertSQL("UPDATE mytable SET data['x'] = ? WHERE mytable.name = ?", stmt) |
97 | 101 |
|
98 | 102 | def set_up_character_and_cursor(self, return_value=None): |
99 | | - return_value = return_value or [("Trillian", {})] |
100 | | - fake_cursor.fetchall.return_value = return_value |
101 | | - fake_cursor.description = ( |
102 | | - ("characters_name", None, None, None, None, None, None), |
103 | | - ("characters_data", None, None, None, None, None, None), |
104 | | - ) |
| 103 | + """ |
| 104 | + Set up a ``Character`` model and a fake cursor, compatible with all |
| 105 | + supported SQLAlchemy versions. |
| 106 | +
|
| 107 | + **SA 2.1+** may issue SELECTs for different subsets of columns |
| 108 | + depending on which attributes have been expired after a flush. A |
| 109 | + dynamic ``execute`` side-effect is installed on the fake cursor so that |
| 110 | + ``cursor.description`` and ``cursor.fetchall`` are adjusted to return |
| 111 | + exactly the columns present in each SELECT statement. The model uses |
| 112 | + ``mapped_column`` / ``Mapped`` annotations available since SA 2.0. |
| 113 | +
|
| 114 | + **SA < 2.1** always selects the same fixed set of columns after a |
| 115 | + flush. A static cursor mock with a two-column description |
| 116 | + (``name``, ``data``) is used, matching the behaviour of those |
| 117 | + versions. The model uses plain ``sa.Column`` declarations. |
| 118 | + """ |
105 | 119 | fake_cursor.rowcount = 1 |
106 | 120 | Base = declarative_base() |
107 | 121 |
|
108 | | - class Character(Base): |
109 | | - __tablename__ = "characters" |
110 | | - name = sa.Column(sa.String, primary_key=True) |
111 | | - age = sa.Column(sa.Integer) |
112 | | - data = sa.Column(ObjectType) |
113 | | - data_list = sa.Column(ObjectArray) |
| 122 | + if SA_VERSION >= SA_2_1: |
| 123 | + # SA 2.1 may fire SELECTs for different subsets of columns depending |
| 124 | + # on what is expired. Use a dynamic mock that adjusts description and |
| 125 | + # return value to match exactly the columns present in each SELECT. |
| 126 | + data_rows = return_value or [("Trillian", {})] |
| 127 | + col_order = [ |
| 128 | + "characters_name", |
| 129 | + "characters_age", |
| 130 | + "characters_data", |
| 131 | + "characters_data_list", |
| 132 | + ] |
| 133 | + full_rows = [(r[0], None, r[1], None) for r in data_rows] |
| 134 | + |
| 135 | + def _col_in_sql(col, sql): |
| 136 | + # Match 'characters.<attr>' where attr does not bleed into another column name. |
| 137 | + attr = col.replace("characters_", "") |
| 138 | + return bool(re.search(rf"characters\.{attr}(?!\w)", sql)) |
| 139 | + |
| 140 | + def execute_side_effect(sql, *args, **kwargs): |
| 141 | + sql_str = str(sql) |
| 142 | + if "SELECT" in sql_str.upper(): |
| 143 | + cols = [c for c in col_order if _col_in_sql(c, sql_str)] |
| 144 | + if cols: |
| 145 | + indices = [col_order.index(c) for c in cols] |
| 146 | + fake_cursor.description = tuple( |
| 147 | + (c, None, None, None, None, None, None) for c in cols |
| 148 | + ) |
| 149 | + fake_cursor.fetchall.return_value = [ |
| 150 | + tuple(row[i] for i in indices) for row in full_rows |
| 151 | + ] |
| 152 | + |
| 153 | + fake_cursor.execute.side_effect = execute_side_effect |
| 154 | + # Set defaults so non-SELECT calls (INSERT/UPDATE) don't need description. |
| 155 | + fake_cursor.fetchall.return_value = [] |
| 156 | + fake_cursor.description = () |
| 157 | + |
| 158 | + class Character(Base): |
| 159 | + __tablename__ = "characters" |
| 160 | + name: Mapped[str] = mapped_column(primary_key=True) |
| 161 | + age = sa.Column(sa.Integer) |
| 162 | + data = sa.Column(ObjectType) |
| 163 | + data_list = sa.Column(ObjectArray) |
| 164 | + |
| 165 | + else: |
| 166 | + # Older SA always selects a fixed set of columns; use a static mock. |
| 167 | + fake_cursor.execute.side_effect = None |
| 168 | + fake_cursor.fetchall.return_value = return_value or [("Trillian", {})] |
| 169 | + fake_cursor.description = ( |
| 170 | + ("characters_name", None, None, None, None, None, None), |
| 171 | + ("characters_data", None, None, None, None, None, None), |
| 172 | + ) |
| 173 | + |
| 174 | + class Character(Base): |
| 175 | + __tablename__ = "characters" |
| 176 | + name = sa.Column(sa.String, primary_key=True) |
| 177 | + age = sa.Column(sa.Integer) |
| 178 | + data = sa.Column(ObjectType) |
| 179 | + data_list = sa.Column(ObjectArray) |
114 | 180 |
|
115 | 181 | session = Session(bind=self.engine) |
116 | 182 | return session, Character |
@@ -301,6 +367,9 @@ def test_partial_dict_update_with_setitem_delitem_setitem(self): |
301 | 367 |
|
302 | 368 | def set_up_character_and_cursor_data_list(self, return_value=None): |
303 | 369 | return_value = return_value or [("Trillian", {})] |
| 370 | + # Clear any side_effect installed by set_up_character_and_cursor so the |
| 371 | + # static description below is used as-is for this 2-column model. |
| 372 | + fake_cursor.execute.side_effect = None |
304 | 373 | fake_cursor.fetchall.return_value = return_value |
305 | 374 | fake_cursor.description = ( |
306 | 375 | ("characters_name", None, None, None, None, None, None), |
|
0 commit comments