MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_cursor_is_closed_on_exhausted

Method test_cursor_is_closed_on_exhausted

test/orm/test_query.py:5378–5420  ·  view source on GitHub ↗

test #7274

(self, fetch_method)

Source from the content-addressed store, hash-verified

5376
5377 @testing.combinations("fetchmany", "fetchone", "fetchall")
5378 def test_cursor_is_closed_on_exhausted(self, fetch_method):
5379 """test #7274"""
5380 self._eagerload_mappings()
5381
5382 User = self.classes.User
5383
5384 sess = fixture_session()
5385
5386 stmt = select(User).execution_options(yield_per=15)
5387 result = sess.execute(stmt)
5388
5389 with mock.patch.object(result.raw, "_soft_close") as mock_close:
5390 # call assertions are implementation specific.
5391 # test needs that _soft_close called at least once and without
5392 # the hard=True flag
5393 if fetch_method == "fetchmany":
5394 while True:
5395 buf = result.fetchmany(2)
5396 if not buf:
5397 break
5398 eq_(mock_close.mock_calls, [mock.call()])
5399 elif fetch_method == "fetchall":
5400 eq_(len(result.all()), 4)
5401 eq_(
5402 mock_close.mock_calls, [mock.call(), mock.call(hard=False)]
5403 )
5404 elif fetch_method == "fetchone":
5405 while True:
5406 row = result.fetchone()
5407 if row is None:
5408 break
5409 eq_(
5410 mock_close.mock_calls, [mock.call(), mock.call(hard=False)]
5411 )
5412 else:
5413 assert False
5414
5415 # soft closed, we can still get an empty result
5416 eq_(result.all(), [])
5417
5418 # real closed
5419 result.close()
5420 assert_raises(sa.exc.ResourceClosedError, result.all)
5421
5422 def test_yield_per_close_on_interrupted_iteration_legacy(self):
5423 """test #8710"""

Callers

nothing calls this directly

Calls 12

_eagerload_mappingsMethod · 0.95
fixture_sessionFunction · 0.90
selectFunction · 0.90
eq_Function · 0.90
assert_raisesFunction · 0.90
objectMethod · 0.80
execution_optionsMethod · 0.45
executeMethod · 0.45
fetchmanyMethod · 0.45
allMethod · 0.45
fetchoneMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected