Пример #1
0
def test_get_posts_order(db, order: Optional[str]):
    """This test ensures that ordering works correct."""
    test_posts = posts.get_posts(order=order, limit=30, offset=0)
    if order.startswith('-'):
        order_field = order[1:]
        check_func = _check_order_desc
    else:
        order_field = order
        check_func = _check_order_asc
    for post_index in range(len(test_posts) - 1):
        check_func(
            test_posts[post_index],
            test_posts[post_index + 1],
            order_field,
        )
Пример #2
0
def get_posts(request: http.HttpRequest) -> http.JsonResponse:
    """Get hackernews posts."""
    form = post_forms.PostsRequest(request.GET)
    if not form.is_valid():
        return JsonResponseBadRequest(
            message='invalid GET data',
            details=form.errors,
        )

    post_list = posts.get_posts(
        order=form.cleaned_data.get('order'),
        limit=form.cleaned_data.get('limit'),
        offset=form.cleaned_data.get('offset'),
    )

    response_posts = []
    for post in post_list:
        response_posts.append(post.to_dict())
    return http.JsonResponse({'posts': response_posts})
Пример #3
0
def test_get_posts_offset_empty(db):
    """This test ensures that offset bigger than objects return nothing."""
    test_posts = posts.get_posts(order='id', limit=1, offset=50)
    assert not test_posts
Пример #4
0
def test_get_posts_offset(db, offset):
    """This test ensures that offset works correct."""
    test_posts = posts.get_posts(order='id', limit=1, offset=offset)
    assert test_posts[0].pk == offset + 1
Пример #5
0
def test_get_posts_limit(db):
    """This test ensures that limit works correct."""
    limit = 30
    test_posts = posts.get_posts(order=None, limit=limit, offset=0)
    assert len(test_posts) == limit