Beispiel #1
0
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",
                })))
Beispiel #2
0
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",
                        },
                    }
                })))
Beispiel #3
0
        def search(**path_data):
            request_data = load_query_string_data(definition.request_schema)
            page = self.page_cls.from_query_string(request_data)
            return_value = definition.func(
                **merge_data(path_data, request_data))

            if len(return_value) == 3:
                items, count, context = return_value
            else:
                context = {}
                items, count = return_value

            response_data = PaginatedList(ns=ns,
                                          page=page,
                                          items=items,
                                          count=count,
                                          schema=definition.response_schema,
                                          operation=Operation.Search,
                                          **context)
            return dump_response_data(paginated_list_schema, response_data)