def test_page_defaults(): graph = create_object_graph(name="example", testing=True) with graph.flask.test_request_context(): page = Page() assert_that(page.to_dict(), is_(equal_to({"offset": 0, "limit": 20})))
def test_page_defaults_with_page_schema(): graph = create_object_graph(name="example", testing=True) with graph.flask.test_request_context(): page = Page.from_query_string(PageSchema().load({}).data) assert_that(page.to_dict(), is_(equal_to({"offset": 0, "limit": 20})))
def test_custom_paginated_list(): graph = create_object_graph(name="example", testing=True) ns = Namespace(subject="foo", object_="bar") @graph.route(ns.relation_path, Operation.SearchFor, ns) def search_foo(): pass uid = uuid4() paginated_list = PaginatedList( ns, Page.from_query_string( dict( offset=2, limit=2, baz="baz", uid=uid, value=MyEnum.ONE, )), ["1", "2"], 10, operation=Operation.SearchFor, foo_id="FOO_ID", ) rest = "baz=baz&uid={}&value=ONE".format(uid) with graph.flask.test_request_context(): assert_that( paginated_list.to_dict(), is_( equal_to({ "count": 10, "items": [ "1", "2", ], "offset": 2, "limit": 2, "_links": { "self": { "href": "http://localhost/api/foo/FOO_ID/bar?offset=2&limit=2&{}" .format(rest), }, "next": { "href": "http://localhost/api/foo/FOO_ID/bar?offset=4&limit=2&{}" .format(rest), }, "prev": { "href": "http://localhost/api/foo/FOO_ID/bar?offset=0&limit=2&{}" .format(rest), }, }, "baz": "baz", "uid": str(uid), "value": "ONE", })))
def test_page_from_query_string(): graph = create_object_graph(name="example", testing=True) with graph.flask.test_request_context(): qs = load_query_string_data(PageSchema()) page = Page.from_query_string(qs) assert_that(page.offset, is_(equal_to(0))) assert_that(page.limit, is_(equal_to(20)))
def discover(): # accept pagination limit from request page = Page.from_query_string(load_query_string_data(page_schema)) page.offset = 0 response_data = dict(_links=Links({ "self": Link.for_(Operation.Discover, ns, qs=page.to_tuples()), "search": [ link for link in iter_links( self.find_matching_endpoints(ns), page) ], }).to_dict()) return make_response(response_data)
def search(**path_data): request_data = load_query_string_data(definition.request_schema) page = Page.from_query_string(request_data) items, count, context = definition.func( **merge_data(path_data, request_data)) response_data = self.paginated_list_class( ns=ns, page=page, items=items, count=count, schema=definition.response_schema, operation=Operation.SearchFor, **context) return dump_response_data(paginated_list_schema, response_data)
def test_paginated_list_relation_to_dict(): graph = create_object_graph(name="example", testing=True) ns = Namespace(subject="foo", object_="bar") @graph.route(ns.relation_path, Operation.SearchFor, ns) def search_foo(): pass paginated_list = PaginatedList( ns, Page(2, 2), ["1", "2"], 10, operation=Operation.SearchFor, foo_id="FOO_ID", ) with graph.flask.test_request_context(): assert_that( paginated_list.to_dict(), is_( equal_to({ "count": 10, "items": [ "1", "2", ], "offset": 2, "limit": 2, "_links": { "self": { "href": "http://localhost/api/foo/FOO_ID/bar?offset=2&limit=2", }, "next": { "href": "http://localhost/api/foo/FOO_ID/bar?offset=4&limit=2", }, "prev": { "href": "http://localhost/api/foo/FOO_ID/bar?offset=0&limit=2", }, } })))
def test_page_prev(): page = Page(20, 10).prev() assert_that(page.offset, is_(equal_to(10))) assert_that(page.limit, is_(equal_to(10)))
def test_page_next(): page = Page(0, 10).next() assert_that(page.offset, is_(equal_to(10))) assert_that(page.limit, is_(equal_to(10)))
def test_page_to_dict(): page = Page(0, 10) assert_that(page.to_dict(), is_(equal_to({"offset": 0, "limit": 10})))