Skip to content
This repository was archived by the owner on May 3, 2023. It is now read-only.

Commit 5b00fc5

Browse files
committedFeb 19, 2013
#75: Added post limits.
1 parent 626857d commit 5b00fc5

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed
 

‎tentd/blueprints/posts.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from flask import json, request, g, abort, make_response
66
from flask.views import MethodView
77

8+
from mongoengine import Q
9+
810
from tentd.lib.flask import EntityBlueprint, jsonify
911
from tentd.utils import follow
1012
from tentd.utils.exceptions import APIBadRequest
@@ -13,6 +15,7 @@
1315

1416
posts = EntityBlueprint('posts', __name__, url_prefix='/posts')
1517

18+
DEFAULT_POST_LIMIT = 200
1619

1720
@posts.route_class('', endpoint='posts')
1821
class PostsView(MethodView):
@@ -22,7 +25,12 @@ class PostsView(MethodView):
2225

2326
def get(self):
2427
"""Gets all posts"""
25-
return jsonify(g.entity.posts)
28+
query = []
29+
limit = DEFAULT_POST_LIMIT
30+
if 'limit' in request.args:
31+
limit = min(int(request.args['limit']), limit)
32+
33+
return jsonify(g.entity.posts(*query).limit(limit))
2634

2735
def post(self):
2836
""" Used by apps to create a new post.

‎tentd/tests/blueprints/test_posts.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,22 @@ def test_get_post_mentions(post):
6060

6161
def test_get_posts(entity, post):
6262
"""Test that getting all posts returns correctly."""
63-
response = GET('posts.posts', secure=True)
63+
response = SGET('posts.posts')
6464
posts = jsonify([p.to_json() for p in entity.posts])
6565
assert response.data == posts.data
6666

6767
def test_get_empty_posts(entity):
6868
"""Test that /posts works when there are no posts to return"""
6969
assert SGET('posts.posts').json() == list()
70+
71+
def test_get_filtered_posts(entity, posts):
72+
"""Test that /posts?limit=1 works"""
73+
# Check limits from 0 to N+1 (check boundary conditions)
74+
for i in xrange(len(posts) + 1):
75+
# Make sure the length of posts returned is either i or the number of
76+
# posts which exist.
77+
assert len(SGET('posts.posts', limit=i).json()) == min(i, len(posts))
78+
7079

7180
def test_update_post(post):
7281
"""Test a single post can be updated."""

‎tentd/tests/conftest.py

+13
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ def post(request, entity):
117117
request.addfinalizer(post.delete)
118118
return post
119119

120+
@fixture
121+
def posts(request, entity):
122+
"""Sets up multiple posts"""
123+
schema = 'https://tent.io/types/post/status/v0.1.0'
124+
posts = []
125+
for i in xrange(15):
126+
post = Post.new(entity=entity, schema=schema, content={
127+
'text': 'Post {}'.format(i)})
128+
post.save()
129+
request.addfinalizer(post.delete)
130+
posts.append(post)
131+
return posts
132+
120133
@fixture
121134
def follower(request, entity):
122135
"""A follower with an identity of http://follower.example.com"""

0 commit comments

Comments
 (0)
This repository has been archived.