Example #1
0
File: api.py Project: madar010/mad
 def decorator(*args, **kwargs):
     res = func(*args, **kwargs)
     if isinstance(res, types.Page):
         return types.Page([item.to_dict() for item in res],
                           res.prev, res.next)
     if isinstance(res, list):
         return [item.to_dict() for item in res]
     if res:
         return res.to_dict()
     else:
         return None
Example #2
0
 def handle(*args, **kwargs):
     ret = func(*args, **kwargs)
     if isinstance(ret, types.Page):
         return types.Page([resource_class(el) for el in ret], ret.prev,
                           ret.next)
     elif isinstance(ret, list):
         return [resource_class(el) for el in ret]
     elif ret:
         return resource_class(ret)
     else:
         return None
Example #3
0
class APIUtilsTest(testtools.TestCase):

    class FakeCluster(object):

        def to_dict(self):
            return {"id": 42, "name": "myFirstCluster"}

    page = types.Page([FakeCluster()])
    response = {"clusters":
                [
                    {
                        "id": 42,
                        "name": "myFirstCluster"
                    }
                ]
                }

    def test_render_pagination(self):
        def _assert_response_equal(response_obj, response, status, mimetype):
            self.assertEqual(response_obj.response, [response.encode()])
            self.assertEqual(response_obj.status, status)
            self.assertEqual(response_obj.mimetype, mimetype)

        from sahara.api.middleware import sahara_middleware  # noqa
        app = sahara_middleware.build_app()

        with app.test_request_context():
            _200_OK = '200 OK'
            serializer = wsgi.JSONDictSerializer()

            resp = \
                api.render(self.page, 'application/json', 200, name='clusters')
            body = serializer.serialize(self.response)
            _assert_response_equal(resp, body, _200_OK, 'application/json')

            self.page.prev, self.page.next = 35, 49
            resp = \
                api.render(self.page, 'application/json', 200, name='clusters')
            paginate_response = copy.copy(self.response)
            paginate_response["markers"] = \
                {"prev": 35, "next": 49}

            body = serializer.serialize(paginate_response)
            _assert_response_equal(resp, body, _200_OK, 'application/json')

            self.page.prev, self.page.next = 7, None
            resp = \
                api.render(self.page, 'application/json', 200, name='clusters')
            paginate_response = copy.copy(self.response)
            paginate_response["markers"] = {"prev": 7, "next": None}

            body = serializer.serialize(paginate_response)
            _assert_response_equal(resp, body, _200_OK, 'application/json')

            self.page.prev, self.page.next = None, 14
            resp = \
                api.render(self.page, 'application/json', 200, name='clusters')

            paginate_response = copy.copy(self.response)
            paginate_response["markers"] = {"prev": None, "next": 14}

            body = serializer.serialize(paginate_response)
            _assert_response_equal(resp, body, _200_OK, 'application/json')

            self.page.prev, self.page.next = None, 11
            resp = \
                api.render(self.page, 'application/json', 200, name='clusters')

            paginate_response = copy.copy(self.response)
            paginate_response["markers"] = \
                {"prev": None, "next": 11}

            body = serializer.serialize(paginate_response)
            _assert_response_equal(resp, body, _200_OK, 'application/json')
Example #4
0
class APIUtilsTest(testtools.TestCase):
    class FakeCluster(object):
        def to_dict(self):
            return {"id": 42, "name": "myFirstCluster"}

    page = types.Page([FakeCluster()])
    response = {"clusters": [{"id": 42, "name": "myFirstCluster"}]}

    @mock.patch('flask.request')
    @mock.patch('flask.Response')
    def test_render_pagination(self, flask, request):
        serializer = wsgi.JSONDictSerializer()
        request.status_code = 200

        api.render(self.page, 'application/json', 200, name='clusters')
        body = serializer.serialize(self.response)
        flask.assert_called_with(response=body,
                                 status=200,
                                 mimetype='application/json')

        self.page.prev, self.page.next = 35, 49
        api.render(self.page, 'application/json', 200, name='clusters')
        paginate_response = copy.copy(self.response)
        paginate_response["markers"] = \
            {"prev": 35, "next": 49}

        body = serializer.serialize(paginate_response)
        flask.assert_called_with(response=body,
                                 status=200,
                                 mimetype='application/json')

        self.page.prev, self.page.next = 7, None
        api.render(self.page, 'application/json', 200, name='clusters')
        paginate_response = copy.copy(self.response)
        paginate_response["markers"] = {"prev": 7, "next": None}

        body = serializer.serialize(paginate_response)
        flask.assert_called_with(response=body,
                                 status=200,
                                 mimetype='application/json')

        self.page.prev, self.page.next = None, 14
        api.render(self.page, 'application/json', 200, name='clusters')

        paginate_response = copy.copy(self.response)
        paginate_response["markers"] = {"prev": None, "next": 14}

        body = serializer.serialize(paginate_response)
        flask.assert_called_with(response=body,
                                 status=200,
                                 mimetype='application/json')

        self.page.prev, self.page.next = None, 11
        api.render(self.page, 'application/json', 200, name='clusters')

        paginate_response = copy.copy(self.response)
        paginate_response["markers"] = \
            {"prev": None, "next": 11}

        body = serializer.serialize(paginate_response)
        flask.assert_called_with(response=body,
                                 status=200,
                                 mimetype='application/json')

        self.page.prev, self.page.next = None, 11
        api.render(self.page, 'application/json', 200, name='clusters')