def test_filter_posts_returns_the_response_when_called(self):
        api = qrest.API(jsonplaceholderconfig)
        api.filter_posts.response = ContentResponse()

        with mock.patch("requests.request", return_value=self.mock_response):
            response = api.filter_posts.get_response(user_id=1)
            self.assertIs(api.filter_posts.response, response)
    def test_create_post_accesses_the_right_endpoint_when_called(self):
        api = qrest.API(jsonplaceholderconfig)
        api.create_post.response = ContentResponse()

        title = "new post using qREST ORM"
        content = "this is the new data posted using qREST"
        user_id = 200

        with mock.patch("requests.request",
                        return_value=self.mock_response) as mock_request:
            response = api.create_post.get_response(title=title,
                                                    content=content,
                                                    user_id=user_id)

            mock_request.assert_called_with(
                method="POST",
                auth=None,
                verify=False,
                url="https://jsonplaceholder.typicode.com/posts",
                params={},
                json={
                    "title": title,
                    "body": content,
                    "userId": user_id
                },
                headers={"Content-type": "application/json; charset=UTF-8"},
            )
            self.assertIs(api.create_post.response, response)
Example #3
0
 def test_raise_proper_exception_when_no_APIConfig_class_is_present(self):
     registry = mock.Mock()
     registry.retrieve.return_value = []
     with mock.patch("qrest.resource.ModuleClassRegistry",
                     return_value=registry):
         message = "Imported module '.*' does not contain a subclass of APIConfig."
         with self.assertRaisesRegex(RestClientConfigurationError, message):
             _ = qrest.API(inspect.getmodule(self))
Example #4
0
    def test_bad_create_post_with_schema(self):
        api = qrest.API(jsonplaceholderconfig)
        api.create_post_with_schema.response = ContentResponse()

        # parameter that does not obey the schema
        post = {'user': '******', 'message': 'Something about bob'}

        with self.assertRaises(RestClientValidationError) as exc:
            api.create_post_with_schema.get_response(post=post)

        self.assertEqual(exc.exception.args[0], "value for post does not obey schema")
Example #5
0
    def test_raise_proper_exception_when_ResourceConfig_class_is_without_name(
            self):
        class WithoutName(ResourceConfig):
            path = ["without", "name"]
            method = "GET"

        registry = mock.Mock()
        registry.retrieve.return_value = [WithoutName]
        with mock.patch("qrest.resource.ModuleClassRegistry",
                        return_value=registry):
            message = "Imported class '.*' does not have a 'name' attribute."
            with self.assertRaisesRegex(RestClientConfigurationError, message):
                _ = qrest.API(inspect.getmodule(self))
    def test_all_posts_queries_the_right_endpoint_2(self):
        api = qrest.API(jsonplaceholderconfig)
        api.all_posts.response = ContentResponse()

        with mock.patch("requests.request",
                        return_value=self.mock_response) as mock_request:
            posts = api.all_posts()

            mock_request.assert_called_with(
                method="GET",
                auth=None,
                verify=False,
                url="https://jsonplaceholder.typicode.com/posts",
                params={},
                json={},
                headers={"Content-type": "application/json; charset=UTF-8"},
            )

            self.assertEqual(self.mock_response.content, posts)
Example #7
0
    def test_create_post_with_schema(self):
        api = qrest.API(jsonplaceholderconfig)
        api.create_post_with_schema.response = ContentResponse()

        post = {'user': '******', 'body': 'Something about bob'}

        with mock.patch("requests.request", return_value=self.mock_response) as mock_request:
            response = api.create_post_with_schema.get_response(post=post)

            mock_request.assert_called_with(
                method="POST",
                auth=None,
                verify=False,
                url="https://jsonplaceholder.typicode.com/posts",
                params={},
                json=post,
                files=[],
                headers={"Content-type": "application/json; charset=UTF-8"},
            )
            self.assertIs(api.create_post_with_schema.response, response)
Example #8
0
    def test_upload_file_accesses_the_right_endpoint_when_called(self):
        api = qrest.API(jsonplaceholderconfig)
        api.upload_file.response = ContentResponse()

        file = open(qrest.__file__, 'rb')

        with mock.patch("requests.request", return_value=self.mock_response) as mock_request:
            response = api.upload_file.get_response(file=('__init__.py', file))

            mock_request.assert_called_with(
                method="POST",
                auth=None,
                verify=False,
                url="https://jsonplaceholder.typicode.com/files",
                params={},
                json={},
                files=[('file', ('__init__.py', file))],
                headers={"Content-type": "application/json; charset=UTF-8"},
            )
            self.assertIs(api.upload_file.response, response)
    def test_single_post_queries_the_right_endpoint(self):
        api = qrest.API(jsonplaceholderconfig)
        api.single_post.response = ContentResponse()

        with mock.patch("requests.request",
                        return_value=self.mock_response) as mock_request:
            post = api.single_post(item=1)

            mock_request.assert_called_with(
                method="GET",
                auth=None,
                verify=False,
                url="https://jsonplaceholder.typicode.com/posts/1",
                params={},
                json={},
                headers={
                    "Content-type": "application/json; charset=UTF-8",
                    "X-test-post": "qREST python ORM",
                },
            )

            self.assertEqual(self.mock_response.content, post)
 def test_create_post_help_returns_the_correct_title(self):
     api = qrest.API(jsonplaceholderconfig)
     self.assertEqual("The title of the post",
                      api.create_post.help("title"))
Example #11
0
    def test_all_posts_becomes_an_attribute(self):

        api = qrest.API(inspect.getmodule(self))
        self.assertIsInstance(api.all_posts, JSONResource)