MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_options

Method test_options

test/orm/test_deferred.py:494–560  ·  view source on GitHub ↗

Options on a mapper to create deferred and undeferred columns

(self)

Source from the content-addressed store, hash-verified

492 __dialect__ = "default"
493
494 def test_options(self):
495 """Options on a mapper to create deferred and undeferred columns"""
496
497 orders, Order = self.tables.orders, self.classes.Order
498
499 self.mapper_registry.map_imperatively(Order, orders)
500
501 sess = fixture_session()
502 q = sess.query(Order).order_by(Order.id).options(defer(Order.user_id))
503
504 def go():
505 q.all()[0].user_id
506
507 self.sql_eq_(
508 go,
509 [
510 (
511 "SELECT orders.id AS orders_id, "
512 "orders.address_id AS orders_address_id, "
513 "orders.description AS orders_description, "
514 "orders.isopen AS orders_isopen "
515 "FROM orders ORDER BY orders.id",
516 {},
517 ),
518 (
519 "SELECT orders.user_id "
520 "FROM orders WHERE orders.id = :pk_1",
521 {"pk_1": 1},
522 ),
523 ],
524 )
525 sess.expunge_all()
526
527 # hypothetical for 2.0 - don't overwrite conflicting user-defined
528 # options, raise instead.
529
530 # not sure if this behavior will fly with the userbase. however,
531 # it at least gives us a clear place to affirmatively resolve
532 # conflicts like this if we see that we need to re-enable overwriting
533 # of conflicting options.
534 q2 = q.options(undefer(Order.user_id))
535 with expect_raises_message(
536 sa.exc.InvalidRequestError,
537 r"Loader strategies for ORM Path\[Mapper\[Order\(orders\)\] -> "
538 r"Order.user_id\] conflict",
539 ):
540 q2.all()
541
542 q3 = (
543 sess.query(Order)
544 .order_by(Order.id)
545 .options(undefer(Order.user_id))
546 )
547 self.sql_eq_(
548 q3.all,
549 [
550 (
551 "SELECT orders.id AS orders_id, "

Callers

nothing calls this directly

Calls 11

fixture_sessionFunction · 0.90
deferFunction · 0.90
undeferFunction · 0.90
expect_raises_messageFunction · 0.90
map_imperativelyMethod · 0.80
sql_eq_Method · 0.80
optionsMethod · 0.45
order_byMethod · 0.45
queryMethod · 0.45
expunge_allMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected