| 476 | |
| 477 | @contextlib.contextmanager |
| 478 | def 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() |