def test_subquery(self): self.create_data(10) query = User.where(User.id._in(Post.select(Post.user_id))).select() result = query.execute() assert result.count == 10 query = User.where(id=(Post.select(fn.max(Post.user_id)))).select() result = query.execute() assert result.count == 1 assert result.one().id == 10 query = User.where( User.id < Post.select(fn.min(Post.user_id))).select() result = query.execute() assert result.count == 0
def post(id): post = Post.at(id).getone() if post is None: abort(404) setattr(post, 'html', markdown.render(post.body)) query = Post.where(Post.id._in( Post.where(Post.id > id).select(fn.min(Post.id)), Post.where(Post.id < id).select(fn.max(Post.id)), )).select(Post.id, Post.title) setattr(post, 'next', None) setattr(post, 'prev', None) for pst in query: # execute query if pst.id > id: post.next = pst elif pst.id < id: post.prev = pst return render_public('post.html', post=post)
def test_fullname(self): assert fn.max(User.id).fullname == 'max(user.id)' assert fn.count(User.id).fullname == 'count(user.id)' assert fn.concat(User.name, 'hello') == "concat(user.name, 'hello')"
def test_max(self): self.create_data(4) query = User.select(fn.max(User.id)) result = query.execute() assert result.count == 1 assert result.tuples()[0][0] == 4