MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_bulk_update

Method test_bulk_update

test/orm/test_composites.py:454–520  ·  view source on GitHub ↗
(self, type_)

Source from the content-addressed store, hash-verified

452
453 @testing.combinations("legacy", "statement")
454 def test_bulk_update(self, type_):
455 Edge, Point = (self.classes.Edge, self.classes.Point)
456 Graph = self.classes.Graph
457
458 sess = self._fixture()
459
460 graph = Graph(id=2)
461 sess.add(graph)
462 sess.flush()
463 graph_id = 2
464
465 data = [
466 {
467 "start": Point(random.randint(1, 50), random.randint(1, 50)),
468 "end": Point(random.randint(1, 50), random.randint(1, 50)),
469 "graph_id": graph_id,
470 }
471 for i in range(25)
472 ]
473 sess.execute(insert(Edge), data)
474
475 inserted_data = [
476 dict(row._mapping)
477 for row in sess.execute(
478 select(Edge.id, Edge.start, Edge.end, Edge.graph_id)
479 .where(Edge.graph_id == graph_id)
480 .order_by(Edge.id)
481 )
482 ]
483
484 to_update = []
485 updated_pks = {}
486 for rec in random.choices(inserted_data, k=7):
487 rec_copy = dict(rec)
488 updated_pks[rec_copy["id"]] = rec_copy
489 rec_copy["start"] = Point(
490 random.randint(1, 50), random.randint(1, 50)
491 )
492 rec_copy["end"] = Point(
493 random.randint(1, 50), random.randint(1, 50)
494 )
495 to_update.append(rec_copy)
496
497 expected_dataset = [
498 updated_pks[row["id"]] if row["id"] in updated_pks else row
499 for row in inserted_data
500 ]
501
502 if type_ == "statement":
503 sess.execute(update(Edge), to_update)
504 elif type_ == "legacy":
505 sess.bulk_update_mappings(Edge, to_update)
506 else:
507 assert False
508
509 edges = self.tables.edges
510 eq_(
511 sess.execute(

Callers

nothing calls this directly

Calls 15

_fixtureMethod · 0.95
insertFunction · 0.90
selectFunction · 0.90
updateFunction · 0.90
eq_Function · 0.90
GraphClass · 0.70
PointClass · 0.70
addMethod · 0.45
flushMethod · 0.45
executeMethod · 0.45
order_byMethod · 0.45
whereMethod · 0.45

Tested by

no test coverage detected