From 3c3a33d3c5f2d075d5d22aab79fe81a96275dcc5 Mon Sep 17 00:00:00 2001 From: AK391 <81195143+AK391@users.noreply.github.com> Date: Mon, 25 Oct 2021 15:29:55 -0400 Subject: [PATCH] test update inferface --- test/test_interfaces.py | 65 +++++++++++++++++++++++++++++++++++++++++ test/test_utils.py | 2 ++ 2 files changed, 67 insertions(+) diff --git a/test/test_interfaces.py b/test/test_interfaces.py index 1c35782b60..5913e16768 100644 --- a/test/test_interfaces.py +++ b/test/test_interfaces.py @@ -1,4 +1,69 @@ +from gradio.interface import * import unittest +import unittest.mock as mock +import requests + + +class TestUtils(unittest.TestCase): + + + + # 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() + + + # 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() + + @mock.patch("requests.post") + def test_launch_analytics_successful(self, mock_post): + send_launch_analytics(analytics_enabled=True, + inbrowser=True, is_colab="is_colab", + share="share", share_url="share_url") + assert mock_post.call_args.kwargs["data"] == { + 'launch_method': 'browser', + 'is_google_colab': "is_colab", + 'is_sharing_on': "share", + 'share_url': "share_url", + 'ip_address': ip_address + } + + def test_reset_all(self): + interface = Interface(lambda input: None, "textbox", "label") + interface.close = mock.MagicMock() + reset_all() + interface.close.assert_called_with() if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/test/test_utils.py b/test/test_utils.py index a7b74fd5f9..5e33bb866d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -53,10 +53,12 @@ class TestUtils(unittest.TestCase): mock_post.side_effect = requests.ConnectionError() error_analytics("placeholder") + mock_post.assert_called() @mock.patch("requests.post") def test_error_analytics_successful(self, mock_post): error_analytics("placeholder") + mock_post.assert_called() @mock.patch("IPython.get_ipython")