Пример #1
0
def test_offset_limit_page_rejects_unknown_queries():
    graph = create_object_graph(name="example", testing=True)
    with graph.flask.test_request_context(query_string="offset=1&foo=bar"):
        assert_that(
            calling(OffsetLimitPage.from_query_string).with_args(OffsetLimitPageSchema()),
            raises(UnprocessableEntity),
        )
Пример #2
0
def test_offset_limit_page_from_query_string():
    graph = create_object_graph(name="example", testing=True)
    with graph.flask.test_request_context(query_string="offset=1&foo=bar"):
        page = OffsetLimitPage.from_query_string(OffsetLimitPageSchema())
        assert_that(page.offset, is_(equal_to(1)))
        assert_that(page.limit, is_(equal_to(20)))
        # schema filters out extra arguments
        assert_that(page.to_dict(), is_not(has_entry("foo", "bar")))
 def setup(self):
     self.graph = create_object_graph(name="example", testing=True)
     self.person_ns = Namespace(subject=Person)
     self.ns = Namespace(subject=PersonSearch)
     # ensure that link hrefs work
     configure_crud(self.graph, self.person_ns, {
         Operation.Retrieve: (person_retrieve, PersonLookupSchema(), PersonSchema()),
     })
     # enable saved search
     configure_saved_search(self.graph, self.ns, {
         Operation.SavedSearch: (person_search, OffsetLimitPageSchema(), PersonSchema()),
     })
     self.client = self.graph.flask.test_client()
Пример #4
0
    def configure_discover(self, ns, definition):
        """
        Register a discovery endpoint for a set of operations.

        """
        page_schema = OffsetLimitPageSchema()

        @self.add_route("/", Operation.Discover, ns)
        def discover():
            # accept pagination limit from request
            page = OffsetLimitPage.from_query_string(page_schema)
            page.offset = 0

            response_data = dict(_links=Links({
                "self":
                Link.for_(Operation.Discover, ns, qs=page.to_items()),
                "search": [
                    link for link in iter_links(
                        self.find_matching_endpoints(ns), page)
                ],
            }).to_dict())
            return make_response(response_data)
Пример #5
0
def add_request_id(headers, response_data):
    headers["X-Request-Id"] = "request-id"


PERSON_MAPPINGS = {
    Operation.Create:
    (person_create, NewPersonSchema(), PersonSchema(), add_request_id),
    Operation.Delete: (person_delete, ),
    Operation.DeleteBatch: (person_delete_batch, ),
    Operation.UpdateBatch:
    (person_update_batch, NewPersonBatchSchema(), PersonBatchSchema()),
    Operation.Replace: (person_replace, NewPersonSchema(), PersonSchema()),
    Operation.Retrieve:
    (person_retrieve, PersonLookupSchema(), PersonSchema()),
    Operation.Search: (person_search, OffsetLimitPageSchema(), PersonSchema()),
    Operation.Update: (person_update, UpdatePersonSchema(), PersonSchema()),
}

ADDRESS_MAPPINGS = {
    Operation.Delete: (address_delete, DeleteAddressSchema(), AddressSchema()),
    Operation.Retrieve: (address_retrieve, AddressSchema()),
    Operation.Search:
    (address_search, SearchAddressPageSchema(), AddressSchema()),
}


class TestCRUD:
    def setup(self):
        self.graph = create_object_graph(name="example", testing=True)
        person_ns = Namespace(subject=Person)
Пример #6
0
        return self.value


class SearchAddressPageSchema(OffsetLimitPageSchema):
    list_param = QueryStringList(String())
    enum_param = EnumField(TestEnum)


def add_request_id(headers):
    headers["X-Request-Id"] = "request-id"


PERSON_MAPPINGS = {
    Operation.Search: EndpointDefinition(
        func=person_search,
        request_schema=OffsetLimitPageSchema(),
        response_schema=PersonCSVSchema(),
        response_formats=[ResponseFormats.JSON, ResponseFormats.CSV],
    ),
}


ADDRESS_MAPPINGS = {
    Operation.Search: EndpointDefinition(
        func=address_search,
        request_schema=SearchAddressPageSchema(),
        response_schema=AddressCSVSchema(),
        response_formats=[ResponseFormats.JSON, ResponseFormats.CSV],
    ),
}
Пример #7
0
def test_offset_limit_page_from_query_string():
    graph = create_object_graph(name="example", testing=True)
    with graph.flask.test_request_context(query_string="offset=1"):
        page = OffsetLimitPage.from_query_string(OffsetLimitPageSchema())
        assert_that(page.offset, is_(equal_to(1)))
        assert_that(page.limit, is_(equal_to(20)))