def test_lazytrackparent(self):

        class Post(object):
            pass
        class Blog(object):
            pass
        instrumentation.register_class(Post)
        instrumentation.register_class(Blog)

        # set up instrumented attributes with backrefs
        attributes.register_attribute(Post, 'blog', uselist=False,
                                        backref='posts',
                                        trackparent=True, useobject=True)
        attributes.register_attribute(Blog, 'posts', uselist=True,
                                        backref='blog',
                                        trackparent=True, useobject=True)

        # create objects as if they'd been freshly loaded from the database (without history)
        b = Blog()
        p1 = Post()
        _set_callable(attributes.instance_state(b), attributes.instance_dict(b),
                                                    'posts', lambda state, passive:[p1])
        _set_callable(attributes.instance_state(p1), attributes.instance_dict(p1),
                                                    'blog', lambda state, passive:b)
        p1, attributes.instance_state(b)._commit_all(attributes.instance_dict(b))

        # no orphans (called before the lazy loaders fire off)
        assert attributes.has_parent(Blog, p1, 'posts', optimistic=True)
        assert attributes.has_parent(Post, b, 'blog', optimistic=True)

        # assert connections
        assert p1.blog is b
        assert p1 in b.posts

        # manual connections
        b2 = Blog()
        p2 = Post()
        b2.posts.append(p2)
        assert attributes.has_parent(Blog, p2, 'posts')
        assert attributes.has_parent(Post, b2, 'blog')
test that the "hasparent" flag works properly
dataset/ETHPy150Open zzzeek/sqlalchemy/test/orm/test_attributes.py/AttributesTest.test_lazytrackparent