File size: 1,887 Bytes
a03b3ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
from unittest.mock import patch
import pytest
import gradio as gr
class TestClearButton:
def test_clear_event_setup_correctly(self):
with gr.Blocks() as demo:
chatbot = gr.Chatbot([["Hello", "How are you?"]])
with gr.Row():
textbox = gr.Textbox(scale=3, interactive=True)
gr.ClearButton([textbox, chatbot], scale=1)
clear_event_trigger = demo.dependencies.pop()
assert not clear_event_trigger["backend_fn"]
assert clear_event_trigger["js"]
assert clear_event_trigger["outputs"] == [textbox._id, chatbot._id]
def test_clear_event_setup_correctly_with_state(self):
with gr.Blocks() as demo:
chatbot = gr.Chatbot([["Hello", "How are you?"]])
state = gr.State("")
gr.ClearButton([state, chatbot], scale=1)
clear_event_trigger_state = demo.dependencies.pop()
assert clear_event_trigger_state["backend_fn"]
class TestOAuthButtons:
def test_login_button_warns_when_not_on_spaces(self):
with pytest.warns(UserWarning):
with gr.Blocks():
gr.LoginButton()
def test_logout_button_warns_when_not_on_spaces(self):
with pytest.warns(UserWarning):
with gr.Blocks():
gr.LogoutButton()
@patch("gradio.oauth.get_space", lambda: "fake_space")
@patch("gradio.oauth._add_oauth_routes")
def test_login_button_setup_correctly(self, mock_add_oauth_routes):
with gr.Blocks() as demo:
button = gr.LoginButton()
login_event = demo.dependencies[0]
assert login_event["targets"][0][1] == "click"
assert not login_event["backend_fn"] # No Python code
assert login_event["js"] # But JS code instead
assert login_event["inputs"] == [button._id]
assert login_event["outputs"] == []
|