MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / assert_engine

Function assert_engine

lib/sqlalchemy/testing/assertsql.py:478–519  ·  view source on GitHub ↗
(engine)

Source from the content-addressed store, hash-verified

476
477@contextlib.contextmanager
478def assert_engine(engine):
479 asserter = SQLAsserter()
480
481 orig = []
482
483 @event.listens_for(engine, "before_execute")
484 def connection_execute(
485 conn, clauseelement, multiparams, params, execution_options
486 ):
487 # grab the original statement + params before any cursor
488 # execution
489 orig[:] = clauseelement, multiparams, params
490
491 @event.listens_for(engine, "after_cursor_execute")
492 def cursor_execute(
493 conn, cursor, statement, parameters, context, executemany
494 ):
495 if not context:
496 return
497 # then grab real cursor statements and associate them all
498 # around a single context
499 if (
500 asserter.accumulated
501 and asserter.accumulated[-1].context is context
502 ):
503 obs = asserter.accumulated[-1]
504 else:
505 obs = SQLExecuteObserved(context, orig[0], orig[1], orig[2])
506 asserter.accumulated.append(obs)
507
508 obs.statements.append(
509 SQLCursorExecuteObserved(
510 statement, parameters, context, executemany
511 )
512 )
513
514 try:
515 yield asserter
516 finally:
517 event.remove(engine, "after_cursor_execute", cursor_execute)
518 event.remove(engine, "before_execute", connection_execute)
519 asserter._close()

Callers 8

test_insert_computedMethod · 0.90
test_update_computedMethod · 0.90
test_insert_identityMethod · 0.90
test_query_joinMethod · 0.90
test_eager_joinMethod · 0.90
test_existsMethod · 0.90
test_eager_selectinMethod · 0.90
test_load_both_wpolyMethod · 0.90

Calls 3

_closeMethod · 0.95
SQLAsserterClass · 0.85
removeMethod · 0.45

Tested by 8

test_insert_computedMethod · 0.72
test_update_computedMethod · 0.72
test_insert_identityMethod · 0.72
test_query_joinMethod · 0.72
test_eager_joinMethod · 0.72
test_existsMethod · 0.72
test_eager_selectinMethod · 0.72
test_load_both_wpolyMethod · 0.72