adf0d17497
publish / version_or_publish (push) Waiting to run
storybook-build / changes (push) Waiting to run
storybook-build / :storybook-build (push) Blocked by required conditions
Sync Gradio Skills to Hugging Face / sync-skills (push) Waiting to run
functional / changes (push) Waiting to run
functional / build-frontend (push) Blocked by required conditions
functional / functional-test-SSR=false (push) Blocked by required conditions
functional / functional-reload (push) Blocked by required conditions
functional / functional-test-SSR=true (push) Blocked by required conditions
hygiene / hygiene-test (push) Waiting to run
python / changes (push) Waiting to run
python / build (push) Blocked by required conditions
python / test-ubuntu-latest-flaky (push) Blocked by required conditions
python / test-ubuntu-latest-not-flaky (push) Blocked by required conditions
python / test-windows-latest-flaky (push) Blocked by required conditions
python / test-windows-latest-not-flaky (push) Blocked by required conditions
js / changes (push) Waiting to run
js / js-test (push) Blocked by required conditions
docs-build / changes (push) Waiting to run
docs-build / docs-build (push) Blocked by required conditions
docs-build / website-build (push) Blocked by required conditions
96 lines
3.4 KiB
Python
96 lines
3.4 KiB
Python
import pytest
|
|
from pydantic import BaseModel
|
|
|
|
import gradio as gr
|
|
from gradio.state_holder import SessionState
|
|
|
|
|
|
class TestModel(BaseModel):
|
|
name: str
|
|
|
|
|
|
class TestState:
|
|
def test_as_component(self):
|
|
state = gr.State(value=5)
|
|
assert state.preprocess(10) == 10
|
|
assert state.preprocess("abc") == "abc"
|
|
assert state.stateful
|
|
|
|
def test_initial_value_deepcopy(self):
|
|
with pytest.raises(TypeError):
|
|
gr.State(value=gr) # modules are not deepcopyable
|
|
|
|
def test_initial_value_pydantic(self):
|
|
state = gr.State(value=TestModel(name="Freddy"))
|
|
assert isinstance(state.value, TestModel)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_callable_default_is_called_before_load_event(self):
|
|
with gr.Blocks() as demo:
|
|
state = gr.State(value=lambda: {"count": 42})
|
|
num = gr.Number()
|
|
btn = gr.Button()
|
|
btn.click(lambda s: s["count"], state, num)
|
|
|
|
session = SessionState(demo)
|
|
result = await demo.process_api(0, [None], state=session)
|
|
assert result["data"][0] == 42
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_in_interface(self):
|
|
def test(x, y=" def"):
|
|
return (x + y, x + y)
|
|
|
|
io = gr.Interface(test, ["text", "state"], ["text", "state"])
|
|
result = await io.call_function(0, ["abc"])
|
|
assert result["prediction"][0] == "abc def"
|
|
result = await io.call_function(0, ["abc", result["prediction"][0]])
|
|
assert result["prediction"][0] == "abcabc def"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_in_blocks(self):
|
|
with gr.Blocks() as demo:
|
|
score = gr.State()
|
|
btn = gr.Button()
|
|
btn.click(lambda x: x + 1, score, score)
|
|
|
|
result = await demo.call_function(0, [0])
|
|
assert result["prediction"] == 1
|
|
result = await demo.call_function(0, [result["prediction"]])
|
|
assert result["prediction"] == 2
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_with_gr_update(self):
|
|
"""Test that gr.update(value=...) works with gr.State components."""
|
|
with gr.Blocks() as demo:
|
|
state = gr.State(value=False)
|
|
btn = gr.Button()
|
|
# First function sets state to True using gr.update
|
|
btn.click(lambda: gr.update(value=True), None, state)
|
|
# Second function returns the current state value
|
|
btn2 = gr.Button()
|
|
btn2.click(lambda x: x, state, state)
|
|
|
|
# Call first function to set state to True
|
|
result = await demo.call_function(0, [])
|
|
# The prediction is the raw return value (gr.update dict)
|
|
assert result["prediction"] == {"__type__": "update", "value": True}
|
|
|
|
# Call second function to verify state was actually updated
|
|
result = await demo.call_function(1, [True]) # Pass True as current state
|
|
assert result["prediction"] is True
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_with_gr_update_complex_value(self):
|
|
"""Test that gr.update(value=...) works with complex values."""
|
|
with gr.Blocks() as demo:
|
|
state = gr.State(value={"count": 0})
|
|
btn = gr.Button()
|
|
btn.click(
|
|
lambda x: gr.update(value={"count": x["count"] + 1}), state, state
|
|
)
|
|
|
|
result = await demo.call_function(0, [{"count": 5}])
|
|
# The prediction is the raw return value (gr.update dict)
|
|
assert result["prediction"] == {"__type__": "update", "value": {"count": 6}}
|