MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _test_lastrow_accessor

Method _test_lastrow_accessor

test/sql/test_insert_exec.py:137–192  ·  view source on GitHub ↗

Tests the inserted_primary_key and lastrow_has_id() functions.

(self, connection, table_, values, assertvalues)

Source from the content-addressed store, hash-verified

135 )
136
137 def _test_lastrow_accessor(self, connection, table_, values, assertvalues):
138 """Tests the inserted_primary_key and lastrow_has_id() functions."""
139
140 def insert_values(table_, values):
141 """
142 Inserts a row into a table, returns the full list of values
143 INSERTed including defaults that fired off on the DB side and
144 detects rows that had defaults and post-fetches.
145 """
146
147 # verify implicit_returning is working
148 if (
149 connection.dialect.insert_returning
150 and table_.implicit_returning
151 and not connection.dialect.postfetch_lastrowid
152 ):
153 ins = table_.insert()
154 comp = ins.compile(connection, column_keys=list(values))
155 if not set(values).issuperset(
156 c.key for c in table_.primary_key
157 ):
158 is_(bool(comp.returning), True)
159
160 result = connection.execute(table_.insert(), values)
161 ret = values.copy()
162
163 ipk = result.inserted_primary_key
164 for col, id_ in zip(table_.primary_key, ipk):
165 ret[col.key] = id_
166
167 if result.lastrow_has_defaults():
168 criterion = and_(
169 *[
170 col == id_
171 for col, id_ in zip(
172 table_.primary_key, result.inserted_primary_key
173 )
174 ]
175 )
176 row = connection.execute(
177 table_.select().where(criterion)
178 ).first()
179 for c in table_.c:
180 ret[c.key] = row._mapping[c]
181 return ret, ipk
182
183 table_.create(connection, checkfirst=True)
184 i, ipk = insert_values(table_, values)
185 eq_(i, assertvalues)
186
187 # named tuple tests
188 for col in table_.primary_key:
189 eq_(getattr(ipk, col.key), assertvalues[col.key])
190 eq_(ipk._mapping[col.key], assertvalues[col.key])
191
192 eq_(ipk._fields, tuple([col.key for col in table_.primary_key]))
193
194 @testing.requires.supports_autoincrement_w_composite_pk

Calls 2

eq_Function · 0.90
createMethod · 0.45

Tested by

no test coverage detected