gradio/test/test_interfaces.py

63 lines
2.4 KiB
Python
Raw Normal View History

2021-10-26 03:29:55 +08:00
from gradio.interface import *
2020-09-22 02:51:39 +08:00
import unittest
2021-10-26 03:29:55 +08:00
import unittest.mock as mock
import requests
2021-11-04 05:30:36 +08:00
class TestInterface(unittest.TestCase):
2021-10-26 03:29:55 +08:00
# send_error_analytics should probably actually be a method of Interface
# (so it doesn't have to take the 'enabled' argument)
# and since it's specific to the launch method, it should probably be
# renamed to send_launch_error_analytics.
# these tests test its current behavior
@mock.patch("requests.post")
def test_error_analytics_doesnt_crash_on_connection_error(self, mock_post):
mock_post.side_effect = requests.ConnectionError()
send_error_analytics(True)
mock_post.assert_called()
@mock.patch("requests.post")
def test_error_analytics_doesnt_post_if_not_enabled(self, mock_post):
send_error_analytics(False)
mock_post.assert_not_called()
@mock.patch("requests.post")
def test_error_analytics_successful(self, mock_post):
send_error_analytics(True)
mock_post.assert_called()
2021-11-04 18:53:33 +08:00
2021-10-26 03:29:55 +08:00
# as above, send_launch_analytics should probably be a method of Interface
@mock.patch("requests.post")
def test_launch_analytics_doesnt_crash_on_connection_error(self, mock_post):
mock_post.side_effect = requests.ConnectionError()
send_launch_analytics(analytics_enabled=True,
inbrowser=True, is_colab="is_colab",
share="share", share_url="share_url")
mock_post.assert_called()
@mock.patch("requests.post")
def test_launch_analytics_doesnt_post_if_not_enabled(self, mock_post):
send_launch_analytics(analytics_enabled=False,
inbrowser=True, is_colab="is_colab",
share="share", share_url="share_url")
mock_post.assert_not_called()
def test_reset_all(self):
interface = Interface(lambda input: None, "textbox", "label")
interface.close = mock.MagicMock()
reset_all()
2021-11-04 05:30:36 +08:00
interface.close.assert_called()
2021-10-27 07:26:50 +08:00
def test_examples_invalid_input(self):
with self.assertRaises(ValueError):
Interface(lambda x: x, examples=1234)
2021-10-27 07:26:50 +08:00
def test_examples_not_valid_path(self):
with self.assertRaises(FileNotFoundError):
interface = Interface(lambda x: x, "textbox", "label", examples='invalid-path')
2021-10-27 07:26:50 +08:00
interface.launch()
2020-09-22 02:51:39 +08:00
if __name__ == '__main__':
unittest.main()