2021-09-03 05:01:51 +08:00
|
|
|
from gradio.utils import *
|
|
|
|
import unittest
|
|
|
|
import pkg_resources
|
|
|
|
import unittest.mock as mock
|
2021-09-14 23:28:55 +08:00
|
|
|
import warnings
|
2021-09-03 05:01:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestUtils(unittest.TestCase):
|
|
|
|
@mock.patch("pkg_resources.require")
|
|
|
|
def test_should_fail_with_distribution_not_found(self, mock_require):
|
|
|
|
|
|
|
|
mock_require.side_effect = pkg_resources.DistributionNotFound()
|
|
|
|
|
2021-09-14 23:28:55 +08:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
2021-09-03 05:01:51 +08:00
|
|
|
version_check()
|
2021-09-14 23:28:55 +08:00
|
|
|
self.assertEqual(str(w[-1].message), "gradio is not setup or installed properly. Unable to get version info.")
|
2021-09-03 05:01:51 +08:00
|
|
|
|
|
|
|
@mock.patch("requests.get")
|
|
|
|
def test_should_warn_with_unable_to_parse(self, mock_get):
|
|
|
|
|
|
|
|
mock_get.side_effect = json.decoder.JSONDecodeError("Expecting value", "", 0)
|
|
|
|
|
2021-09-14 23:28:55 +08:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
2021-09-03 05:01:51 +08:00
|
|
|
version_check()
|
2021-09-14 23:28:55 +08:00
|
|
|
self.assertEqual(str(w[-1].message), "unable to parse version details from package URL.")
|
2021-09-03 05:01:51 +08:00
|
|
|
|
|
|
|
@mock.patch("requests.get")
|
|
|
|
def test_should_warn_with_connection_error(self, mock_get):
|
|
|
|
|
|
|
|
mock_get.side_effect = ConnectionError()
|
|
|
|
|
2021-09-14 23:28:55 +08:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
2021-09-03 05:01:51 +08:00
|
|
|
version_check()
|
2021-09-14 23:28:55 +08:00
|
|
|
self.assertEqual(str(w[-1].message), "unable to connect with package URL to collect version info.")
|
2021-09-03 05:01:51 +08:00
|
|
|
|
|
|
|
@mock.patch("requests.Response.json")
|
|
|
|
def test_should_warn_url_not_having_version(self, mock_json):
|
|
|
|
|
|
|
|
mock_json.return_value = {"foo": "bar"}
|
|
|
|
|
2021-09-14 23:28:55 +08:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
2021-09-03 05:01:51 +08:00
|
|
|
version_check()
|
2021-09-14 23:28:55 +08:00
|
|
|
self.assertEqual(str(w[-1].message), "package URL does not contain version info.")
|
2021-10-21 00:33:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
@mock.patch("requests.post")
|
|
|
|
def test_error_analytics_doesnt_crash_on_connection_error(self, mock_post):
|
|
|
|
|
|
|
|
mock_post.side_effect = ConnectionError()
|
|
|
|
error_analytics()
|
2021-09-03 05:01:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|