Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 47 additions & 16 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,31 +258,55 @@ async def client():
yield


def make_mock_response(return_value):
response = mock.MagicMock(name="mock_response")
response.status_code = 201
response.json.return_value = return_value
return response


@mock.patch("gradio.utils.client.send")
class TestRequest:
@pytest.mark.asyncio
async def test_get(self):
async def test_get(self, mock_send):

mock_send.return_value = make_mock_response({"Host": "headers.jsontest.com"})

client_response: Request = await Request(
method=Request.Method.GET,
url="http://headers.jsontest.com/",
url="very_real_url.com",
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the url so that it's clear that we're mocking

)
validated_data = client_response.get_validated_data()
assert client_response.is_valid() is True
assert validated_data["Host"] == "headers.jsontest.com"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's mock this request to "headers.jsontest.com" as well?


@pytest.mark.asyncio
async def test_post(self):
async def test_post(self, mock_send):

payload = {"name": "morpheus", "job": "leader"}
mock_send.return_value = make_mock_response(payload)

client_response: Request = await Request(
method=Request.Method.POST,
url="https://reqres.in/api/users",
json={"name": "morpheus", "job": "leader"},
url="very_real_url.com",
json=payload,
)
validated_data = client_response.get_validated_data()
assert client_response.status == 201
assert validated_data["job"] == "leader"
assert validated_data["name"] == "morpheus"

@pytest.mark.asyncio
async def test_validate_with_model(self):
async def test_validate_with_model(self, mock_send):
mock_send.return_value = make_mock_response(
{
"name": "morpheus",
"id": "1",
"job": "leader",
"createdAt": "2",
}
)

class TestModel(BaseModel):
name: str
job: str
Expand All @@ -291,22 +315,25 @@ class TestModel(BaseModel):

client_response: Request = await Request(
method=Request.Method.POST,
url="https://reqres.in/api/users",
url="very_real_url.com",
json={"name": "morpheus", "job": "leader"},
validation_model=TestModel,
)
assert isinstance(client_response.get_validated_data(), TestModel)

@pytest.mark.asyncio
async def test_validate_and_fail_with_model(self):
async def test_validate_and_fail_with_model(self, mock_send):
class TestModel(BaseModel):
name: Literal[str] = "John"
job: str

payload = {"name": "morpheus", "job": "leader"}
mock_send.return_value = make_mock_response(payload)

client_response: Request = await Request(
method=Request.Method.POST,
url="https://reqres.in/api/users",
json={"name": "morpheus", "job": "leader"},
url="very_real_url.com",
json=payload,
validation_model=TestModel,
)
with pytest.raises(Exception):
Expand All @@ -316,29 +343,31 @@ class TestModel(BaseModel):

@mock.patch("gradio.utils.Request._validate_response_data")
@pytest.mark.asyncio
async def test_exception_type(self, validate_response_data):
async def test_exception_type(self, mock_send, validate_response_data):
class ResponseValidationException(Exception):
message = "Response object is not valid."

validate_response_data.side_effect = Exception()

client_response: Request = await Request(
method=Request.Method.GET,
url="https://reqres.in/api/users",
url="very_real_url.com",
exception_type=ResponseValidationException,
)
assert isinstance(client_response.exception, ResponseValidationException)

@pytest.mark.asyncio
async def test_validate_with_function(self):
async def test_validate_with_function(self, mock_send):
mock_send.return_value = make_mock_response({"name": "morpheus", "id": 1})

def has_name(response):
if response["name"] is not None:
return response
raise Exception

client_response: Request = await Request(
method=Request.Method.POST,
url="https://reqres.in/api/users",
url="very_real_url.com",
json={"name": "morpheus", "job": "leader"},
validation_function=has_name,
)
Expand All @@ -348,16 +377,18 @@ def has_name(response):
assert client_response.exception is None

@pytest.mark.asyncio
async def test_validate_and_fail_with_function(self):
async def test_validate_and_fail_with_function(self, mock_send):
def has_name(response):
if response["name"] is not None:
if response["name"] == "Alex":
return response
raise Exception

mock_send.return_value = make_mock_response({"name": "morpheus"})

client_response: Request = await Request(
method=Request.Method.POST,
url="https://reqres.in/api/users",
url="very_real_url.com",
json={"name": "morpheus", "job": "leader"},
validation_function=has_name,
)
Expand Down