def test_fetch_paginated_complaints(mocker, sample_email_notification):
    mocker.patch.dict("app.dao.complaint_dao.current_app.config", {"PAGE_SIZE": 2})
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 1, 1),
    )
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 1, 2),
    )
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 1, 3),
    )

    res = fetch_paginated_complaints(page=1)

    assert len(res.items) == 2
    assert res.items[0].created_at == datetime(2018, 1, 3)
    assert res.items[1].created_at == datetime(2018, 1, 2)

    res = fetch_paginated_complaints(page=2)

    assert len(res.items) == 1
    assert res.items[0].created_at == datetime(2018, 1, 1)
Example #2
0
def get_all_complaints():
    page = int(request.args.get('page', 1))
    pagination = fetch_paginated_complaints(page=page)

    return jsonify(complaints=[x.serialize() for x in pagination.items],
                   links=pagination_links(pagination, '.get_all_complaints',
                                          **request.args.to_dict())), 200