MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_lazytrackparent

Method test_lazytrackparent

test/orm/test_attributes.py:585–651  ·  view source on GitHub ↗

test that the "hasparent" flag works properly when lazy loaders and backrefs are used

(self)

Source from the content-addressed store, hash-verified

583 eq_(u.addresses[1].email_address, "foo@bar.com")
584
585 def test_lazytrackparent(self):
586 """test that the "hasparent" flag works properly
587 when lazy loaders and backrefs are used
588
589 """
590
591 class Post:
592 pass
593
594 class Blog:
595 pass
596
597 instrumentation.register_class(Post)
598 instrumentation.register_class(Blog)
599
600 # set up instrumented attributes with backrefs
601 _register_attribute(
602 Post,
603 "blog",
604 uselist=False,
605 backref="posts",
606 trackparent=True,
607 useobject=True,
608 )
609 _register_attribute(
610 Blog,
611 "posts",
612 uselist=True,
613 backref="blog",
614 trackparent=True,
615 useobject=True,
616 )
617
618 # create objects as if they'd been freshly loaded from the database
619 # (without history)
620 b = Blog()
621 p1 = Post()
622 _set_callable(
623 attributes.instance_state(b),
624 attributes.instance_dict(b),
625 "posts",
626 lambda state, passive: [p1],
627 )
628 _set_callable(
629 attributes.instance_state(p1),
630 attributes.instance_dict(p1),
631 "blog",
632 lambda state, passive: b,
633 )
634 p1, attributes.instance_state(b)._commit_all(
635 attributes.instance_dict(b)
636 )
637
638 # no orphans (called before the lazy loaders fire off)
639 assert attributes.has_parent(Blog, p1, "posts", optimistic=True)
640 assert attributes.has_parent(Post, b, "blog", optimistic=True)
641
642 # assert connections

Callers

nothing calls this directly

Calls 7

_set_callableFunction · 0.85
_commit_allMethod · 0.80
has_parentMethod · 0.80
_register_attributeFunction · 0.70
BlogClass · 0.70
PostClass · 0.70
appendMethod · 0.45

Tested by

no test coverage detected