(
connection, randomize_rows=False, warn_on_downgraded=False
)
| 455 | |
| 456 | |
| 457 | def insertmanyvalues_fixture( |
| 458 | connection, randomize_rows=False, warn_on_downgraded=False |
| 459 | ): |
| 460 | dialect = connection.dialect |
| 461 | orig_dialect = dialect._deliver_insertmanyvalues_batches |
| 462 | orig_conn = connection._exec_insertmany_context |
| 463 | |
| 464 | class RandomCursor: |
| 465 | __slots__ = ("cursor",) |
| 466 | |
| 467 | def __init__(self, cursor): |
| 468 | self.cursor = cursor |
| 469 | |
| 470 | # only this method is called by the deliver method. |
| 471 | # by not having the other methods we assert that those aren't being |
| 472 | # used |
| 473 | |
| 474 | @property |
| 475 | def description(self): |
| 476 | return self.cursor.description |
| 477 | |
| 478 | def fetchall(self): |
| 479 | rows = self.cursor.fetchall() |
| 480 | rows = list(rows) |
| 481 | random.shuffle(rows) |
| 482 | return rows |
| 483 | |
| 484 | def _deliver_insertmanyvalues_batches( |
| 485 | connection, |
| 486 | cursor, |
| 487 | statement, |
| 488 | parameters, |
| 489 | generic_setinputsizes, |
| 490 | context, |
| 491 | ): |
| 492 | if randomize_rows: |
| 493 | cursor = RandomCursor(cursor) |
| 494 | for batch in orig_dialect( |
| 495 | connection, |
| 496 | cursor, |
| 497 | statement, |
| 498 | parameters, |
| 499 | generic_setinputsizes, |
| 500 | context, |
| 501 | ): |
| 502 | if warn_on_downgraded and batch.is_downgraded: |
| 503 | util.warn("Batches were downgraded for sorted INSERT") |
| 504 | |
| 505 | yield batch |
| 506 | |
| 507 | def _exec_insertmany_context(dialect, context): |
| 508 | with mock.patch.object( |
| 509 | dialect, |
| 510 | "_deliver_insertmanyvalues_batches", |
| 511 | new=_deliver_insertmanyvalues_batches, |
| 512 | ): |
| 513 | return orig_conn(dialect, context) |
| 514 |
no outgoing calls