Skip to content

Commit 6ace296

Browse files
authored
Merge pull request #902 from gradio-app/blocks-tests
Blocks Tests
2 parents d4c91aa + 8daada1 commit 6ace296

4 files changed

Lines changed: 216 additions & 5 deletions

File tree

.circleci/config.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ jobs:
3838
- run:
3939
command: |
4040
mkdir screenshots
41-
- run:
42-
command: |
43-
. venv/bin/activate
44-
coverage run -m pytest
45-
coverage xml
4641
- run:
4742
command: |
4843
. venv/bin/activate
@@ -55,6 +50,11 @@ jobs:
5550
command: |
5651
. venv/bin/activate
5752
python -m flake8 --ignore=E731,E501,E722,W503,E126,F401,E203 gradio test
53+
- run:
54+
command: |
55+
. venv/bin/activate
56+
coverage run -m pytest
57+
coverage xml
5858
- codecov/upload:
5959
file: 'coverage.xml'
6060
- store_artifacts:

test/test_blocks.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import random
2+
import unittest
3+
4+
try:
5+
from .test_data.blocks_configs import XRAY_CONFIG # for pytest
6+
except ImportError:
7+
from test_data.blocks_configs import XRAY_CONFIG # for regular python
8+
9+
import gradio as gr
10+
11+
12+
class TestBlocks(unittest.TestCase):
13+
def test_xray(self):
14+
xray_model = lambda diseases, img: {
15+
disease: random.random() for disease in diseases
16+
}
17+
ct_model = lambda diseases, img: {disease: 0.1 for disease in diseases}
18+
19+
xray_blocks = gr.Blocks()
20+
21+
with xray_blocks:
22+
gr.components.Markdown(
23+
"""
24+
# Detect Disease From Scan
25+
With this model you can lorem ipsum
26+
- ipsum 1
27+
- ipsum 2
28+
"""
29+
)
30+
disease = gr.components.CheckboxGroup(
31+
choices=["Covid", "Malaria", "Lung Cancer"], label="Disease to Scan For"
32+
)
33+
34+
with gr.Tabs():
35+
with gr.TabItem("X-ray"):
36+
with gr.Row():
37+
xray_scan = gr.components.Image()
38+
xray_results = gr.components.JSON()
39+
xray_run = gr.Button(
40+
"Run",
41+
css={"background-color": "red", "--hover-color": "orange"},
42+
)
43+
xray_run.click(
44+
xray_model, inputs=[disease, xray_scan], outputs=xray_results
45+
)
46+
47+
with gr.TabItem("CT Scan"):
48+
with gr.Row():
49+
ct_scan = gr.components.Image()
50+
ct_results = gr.components.JSON()
51+
ct_run = gr.Button("Run")
52+
ct_run.click(
53+
ct_model, inputs=[disease, ct_scan], outputs=ct_results
54+
)
55+
56+
_ = gr.components.Textbox()
57+
58+
self.assertEqual(XRAY_CONFIG, xray_blocks.get_config_file())
59+
60+
61+
if __name__ == "__main__":
62+
unittest.main()

test/test_data/__init__.py

Whitespace-only changes.

test/test_data/blocks_configs.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
XRAY_CONFIG = {
2+
"mode": "blocks",
3+
"components": [
4+
{
5+
"id": 1,
6+
"type": "markdown",
7+
"props": {
8+
"default_value": "<pre><code> # Detect Disease From Scan\n With this model you can lorem ipsum\n - ipsum 1\n - ipsum 2\n</code></pre>\n",
9+
"name": "markdown",
10+
"label": None,
11+
"css": {},
12+
},
13+
},
14+
{
15+
"id": 2,
16+
"type": "checkboxgroup",
17+
"props": {
18+
"choices": ["Covid", "Malaria", "Lung Cancer"],
19+
"default_value": [],
20+
"name": "checkboxgroup",
21+
"label": "Disease to Scan For",
22+
"css": {},
23+
},
24+
},
25+
{"id": 3, "type": "tabs", "props": {"css": {}}},
26+
{"id": 4, "type": "tabitem", "props": {"label": "X-ray", "css": {}}},
27+
{"id": 5, "type": "tabitem", "props": {"label": "X-ray", "css": {}}},
28+
{"id": 6, "type": "row", "props": {"type": "row", "css": {}}},
29+
{
30+
"id": 7,
31+
"type": "image",
32+
"props": {
33+
"image_mode": "RGB",
34+
"shape": None,
35+
"source": "upload",
36+
"tool": "editor",
37+
"default_value": None,
38+
"name": "image",
39+
"label": None,
40+
"css": {},
41+
},
42+
},
43+
{
44+
"id": 8,
45+
"type": "json",
46+
"props": {"default_value": '""', "name": "json", "label": None, "css": {}},
47+
},
48+
{
49+
"id": 9,
50+
"type": "button",
51+
"props": {
52+
"default_value": "Run",
53+
"name": "button",
54+
"label": None,
55+
"css": {"background-color": "red", "--hover-color": "orange"},
56+
},
57+
},
58+
{"id": 10, "type": "tabitem", "props": {"label": "CT Scan", "css": {}}},
59+
{"id": 11, "type": "tabitem", "props": {"label": "CT Scan", "css": {}}},
60+
{"id": 12, "type": "row", "props": {"type": "row", "css": {}}},
61+
{
62+
"id": 13,
63+
"type": "image",
64+
"props": {
65+
"image_mode": "RGB",
66+
"shape": None,
67+
"source": "upload",
68+
"tool": "editor",
69+
"default_value": None,
70+
"name": "image",
71+
"label": None,
72+
"css": {},
73+
},
74+
},
75+
{
76+
"id": 14,
77+
"type": "json",
78+
"props": {"default_value": '""', "name": "json", "label": None, "css": {}},
79+
},
80+
{
81+
"id": 15,
82+
"type": "button",
83+
"props": {
84+
"default_value": "Run",
85+
"name": "button",
86+
"label": None,
87+
"css": {},
88+
},
89+
},
90+
{
91+
"id": 16,
92+
"type": "textbox",
93+
"props": {
94+
"lines": 1,
95+
"placeholder": None,
96+
"default_value": "",
97+
"name": "textbox",
98+
"label": None,
99+
"css": {},
100+
},
101+
},
102+
],
103+
"theme": "default",
104+
"layout": {
105+
"id": 0,
106+
"children": [
107+
{"id": 1},
108+
{"id": 2},
109+
{
110+
"id": 3,
111+
"children": [
112+
{
113+
"id": 5,
114+
"children": [
115+
{"id": 6, "children": [{"id": 7}, {"id": 8}]},
116+
{"id": 9},
117+
],
118+
},
119+
{
120+
"id": 5,
121+
"children": [
122+
{"id": 6, "children": [{"id": 7}, {"id": 8}]},
123+
{"id": 9},
124+
],
125+
},
126+
{
127+
"id": 11,
128+
"children": [
129+
{"id": 12, "children": [{"id": 13}, {"id": 14}]},
130+
{"id": 15},
131+
],
132+
},
133+
{
134+
"id": 11,
135+
"children": [
136+
{"id": 12, "children": [{"id": 13}, {"id": 14}]},
137+
{"id": 15},
138+
],
139+
},
140+
],
141+
},
142+
{"id": 16},
143+
],
144+
},
145+
"dependencies": [
146+
{"targets": [9], "trigger": "click", "inputs": [2, 7], "outputs": [8]},
147+
{"targets": [15], "trigger": "click", "inputs": [2, 13], "outputs": [14]},
148+
],
149+
}

0 commit comments

Comments
 (0)