Ejemplo n.º 1
0
    def test_schema_with_class_properties(self):
        u1, u2, u3, p11, p12, p21, p22, cm11, cm12, cm21, cm22, cm_empty = \
            self._seed()

        # standalone function
        query = Comment.query
        res = smart_query(query,
            filters={
                'post___public': True,
                'user__isnull': False
            },
            sort_attrs=['user___name', '-created_at'],
            schema={
                Comment.post: {
                    Post.user: JOINED
                }
            }).all()
        self.assertEqual(res, [cm12, cm21, cm22])

        # class method
        res = Comment.smart_query(
            filters={
                'post___public': True,
                'user__isnull': False
            },
            sort_attrs=['user___name', '-created_at'],
            schema={
                Comment.post: {
                    Post.user: JOINED
                }
            }).all()
        self.assertEqual(res, [cm12, cm21, cm22])
Ejemplo n.º 2
0
    def test_lazy_dynamic(self):
        u1, u2, u3, p11, p12, p21, p22, cm11, cm12, cm21, cm22, cm_empty = \
            self._seed()

        schema = {'post': {'user': JOINED}}

        user = sess.query(User).first()
        # and we have initial query for his/her comments
        #  (see User.comments_ relationship)
        query = user.comments_
        # now we just smartly apply all filters, sorts and eagerload. Perfect!
        res = smart_query(query,
                          filters={
                              'post___public': True,
                              'user__isnull': False
                          },
                          sort_attrs=['user___name', '-created_at'],
                          schema=schema).all()

        assert res[0] == cm21
Ejemplo n.º 3
0
    filters={
        'post___public': True,
        'user__isnull': False
    },
    sort_attrs=['user___name', '-created_at'],
    schema=schema).all()
log(res)  # cm12, cm21, cm22

##### 3.2 more flexible smart_query() function #####

##### 3.2.1. The same as 3.1
query = Comment.query  # could be any query you want
res = smart_query(query,
    filters={
        'post___public': True,
        'user__isnull': False
    },
    sort_attrs=['user___name', '-created_at'],
    schema=schema).all()
log(res)  # cm12, cm21, cm22

##### 3.2.2. Real-life example with lazy='dynamic' relationship
# let's imagine we want to display some user relations
#  and flexibly filter, sort and eagerload them
# like this http://www.qopy.me/LwfSCu_ETM6At6el8wlbYA
#  (no sort on screenshot, but you've git the idea)

# so we have a user
user = session.query(User).first()
# and we have initial query for his/her comments
#  (see User.comments_ relationship)