def test_eager_loading(control): # test using diffeent eager loading strategies with control.session as session: create_pages(session) session.commit() # sub query load result = session.query(Person).\ options(subqueryload(Person.pages)).order_by(Person.id).count() assert result >= 1000 # joined load result = session.query(Person).\ options(joinedload(Person.pages)).order_by(Person.id).all() # explicit join + eager load user_pages = session.query(Page).\ join(Page.owner).\ filter(Person.id == 1).\ options(contains_eager(Page.owner)).\ all()
def test_many_to_many(control): # test the creation of a many to many relationship with control.session as session: create_pages(session) session.add(Permission(perm_type='admin')) session.commit() users = session.query(Person).\ filter(Person.perm.any(Permission.perm_type == 'user')).\ all() for user in users: assert user.perm[0].perm_type == 'user' control.save_person({ 'perm': ['admin', 'user'] }, 1) admin = session.query(Person).\ filter(Person.perm.any(Permission.perm_type == 'admin')).\ all() assert len(admin) == 1 assert admin[0].id == 1