def test_execute_with_response_schema_and_unknown_response_fields(self): function_mock = MagicMock() function_mock.execute.return_value = Response( status_code=200, headers={ "Content-Type": "application/json" }, json={ "my-field": "this is an unknown field" } ) endpoint = Endpoint( method="GET", endpoint="/test", response_schema=SampleResponseSchema() ) endpoint._function = function_mock with self.assertRaises(ResponseDeserializationError) as e: endpoint.execute() self.assertIsInstance(e.exception, ResponseDeserializationError) self.assertEqual( e.exception.reason, "failed to deserialize endpoint response" ) self.assertDictEqual( e.exception.errors, { "my-field": ["Unknown field."], "message": ["Missing data for required field."] } ) self.assertEqual(e.exception.response.status_code, 200) self.assertDictEqual( e.exception.response.headers, { "Content-Type": "application/json" } ) self.assertDictEqual( e.exception.response.json, { "my-field": "this is an unknown field" } ) function_mock.execute.assert_called_once_with( args={}, params={}, json=None)
class JsonPlaceholder(Client): post = Endpoint(method="GET", endpoint="/posts/{post_id}", args=["post_id"], response_schema=PostSchema()) posts = Endpoint(method="GET", endpoint="/posts", response_schema=PostSchema(many=True)) create_post = Endpoint(method="POST", endpoint="/posts", response_schema=CreatedPostSchema(), payload_schema=NewPostSchema(), payload="post")
def test_execute_with_payload_schema(self): function_mock = MagicMock() function_mock.execute.return_value = Response( status_code=200, headers={ "Content-Type": "application/json" }, json={ "message": "hello world" } ) endpoint = Endpoint( method="GET", endpoint="/test", payload_schema=SamplePayloadSchema(), payload="data" ) endpoint._function = function_mock response = endpoint.execute(data=SamplePayload(message="hello")) self.assertIsInstance(response, Response) self.assertEqual(response.status_code, 200) self.assertDictEqual( response.headers, { "Content-Type": "application/json" } ) self.assertDictEqual( response.json, { "message": "hello world" } ) function_mock.execute.assert_called_once_with( args={}, params={}, json={ "message": "hello" } )
def test_execute_with_args(self): function_mock = MagicMock() function_mock.execute.return_value = Response( status_code=200, headers={ "Content-Type": "application/json" }, json={ "message": "hello world" } ) endpoint = Endpoint( method="GET", endpoint="/test/{arg1}", args=["arg1"] ) endpoint._function = function_mock response = endpoint.execute(arg1="value") self.assertIsInstance(response, Response) self.assertEqual(response.status_code, 200) self.assertDictEqual( response.headers, { "Content-Type": "application/json" } ) self.assertDictEqual( response.json, { "message": "hello world" } ) function_mock.execute.assert_called_once_with( args={ "arg1": "value" }, params={}, json=None )
def test_execute_with_response_schema_and_unsuccessful_status_code(self): function_mock = MagicMock() function_mock.execute.return_value = Response( status_code=404, headers={ "Content-Type": "application/json" }, json={ "error": "operation failed" } ) endpoint = Endpoint( method="GET", endpoint="/test", response_schema=SampleResponseSchema() ) endpoint._function = function_mock with self.assertRaises(ExecutionError) as e: endpoint.execute() self.assertEqual( e.exception.reason, "the request was not executed successfully") self.assertEqual(e.exception.response.status_code, 404) self.assertDictEqual( e.exception.response.headers, { "Content-Type": "application/json" } ) self.assertDictEqual( e.exception.response.json, { "error": "operation failed" } ) function_mock.execute.assert_called_once_with( args={}, params={}, json=None)