Skip to content

Commit

Permalink
test: regression test for stringified body params
Browse files Browse the repository at this point in the history
  • Loading branch information
roryschadler committed Aug 23, 2024
1 parent c1a58f4 commit edd933a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 5 deletions.
74 changes: 74 additions & 0 deletions samples/openapi3/client/petstore/python-aiohttp/tests/test_rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import unittest
from unittest.mock import AsyncMock, Mock, patch


import petstore_api


def get_field_from_formdata(formdata, name):
return next(filter(lambda x: x[0]["name"] == name, formdata._fields))[-1]


class TestMultipleResponseTypes(unittest.IsolatedAsyncioTestCase):
def setUpFiles(self):
self.test_file_dir = os.path.join(os.path.dirname(__file__), "..", "testfiles")
self.test_file_dir = os.path.realpath(self.test_file_dir)
self.test_file_path = os.path.join(self.test_file_dir, "foo.png")

def setUp(self):
self.api_client = petstore_api.ApiClient()
self.fake_api = petstore_api.FakeApi(self.api_client)
self.setUpFiles()

async def test_multipart_requests(self):
mock_resp = AsyncMock()
mock_resp.return_value.read.return_value = b"some text"
mock_resp.return_value.status = 200
mock_resp.return_value.headers = {}

marker = petstore_api.TestObjectForMultipartRequestsRequestMarker(
name="name",
)

with patch("aiohttp.ClientSession.request", mock_resp):
returned = await self.fake_api.test_object_for_multipart_requests(
marker=marker
)
assert returned is None

async def test_multipart_requests_with_file_and_additional_properties(self):
mock_resp = Mock()
mock_resp.status = 200
mock_resp.read = AsyncMock(
return_value=b'{"code": 200, "type": "success", "message": "OK"}'
)
mock_resp.headers = {"Content-Type": "application/json"}

mock_request = AsyncMock(return_value=mock_resp)
with open(self.test_file_path, "rb") as f, patch(
"aiohttp.ClientSession.request", mock_request
):
returned = await self.fake_api.upload_file_with_additional_properties(
file=(self.test_file_path, f.read()),
count=100,
object=petstore_api.UploadFileWithAdditionalPropertiesRequestObject(
name="foo"
),
)

assert (
returned.code == 200
and returned.type == "success"
and returned.message == "OK"
)

mock_request.assert_called_once()

formdata = mock_request.call_args_list[0].kwargs["data"]

data_object = get_field_from_formdata(formdata, "object")
data_count = get_field_from_formdata(formdata, "count")

assert type(data_count) is str
assert type(data_object) is str
36 changes: 31 additions & 5 deletions samples/openapi3/client/petstore/python/tests/test_rest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import unittest
from unittest.mock import patch, Mock

Expand All @@ -8,9 +9,15 @@


class TestMultipleResponseTypes(unittest.TestCase):
def setUpFiles(self):
self.test_file_dir = os.path.join(os.path.dirname(__file__), "..", "testfiles")
self.test_file_dir = os.path.realpath(self.test_file_dir)
self.test_file_path = os.path.join(self.test_file_dir, "pix.gif")

def setUp(self):
self.api_client = petstore_api.ApiClient()
self.fake_api = petstore_api.FakeApi(self.api_client)
self.setUpFiles()

def test_multipart_requests(self):
mock_resp = Mock()
Expand All @@ -22,10 +29,29 @@ def test_multipart_requests(self):
name="name",
)

with patch(
"urllib3.PoolManager.urlopen", return_value=mock_resp
with patch("urllib3.PoolManager.urlopen", return_value=mock_resp):
returned = self.fake_api.test_object_for_multipart_requests(marker=marker)
assert returned is None

def test_multipart_requests_with_file_and_additional_properties(self):
mock_resp = Mock()
mock_resp.status = 200
mock_resp.data = b'{"code": 200, "type": "success", "message": "OK"}'
mock_resp.getheader.return_value = "application/json"
mock_resp.getheaders.return_value = {"Content-Type": "application/json"}
with open(self.test_file_path, "rb") as f, patch(
"petstore_api.rest.RESTClientObject.request", return_value=mock_resp
):
returned = self.fake_api.test_object_for_multipart_requests(
marker=marker
returned = self.fake_api.upload_file_with_additional_properties(
file=(self.test_file_path, f.read()),
count=100,
object=petstore_api.UploadFileWithAdditionalPropertiesRequestObject(
name="foo"
),
)
assert returned is None

assert (
returned.code == 200
and returned.type == "success"
and returned.message == "OK"
)

0 comments on commit edd933a

Please sign in to comment.