mirror of
https://github.com/gradio-app/gradio.git
synced 2025-03-07 11:46:51 +08:00
fixed warnings & unittests
This commit is contained in:
parent
c45ca08b78
commit
69d29fa8a4
@ -1,5 +1,5 @@
|
||||
import json.decoder
|
||||
|
||||
import warnings
|
||||
import requests
|
||||
import pkg_resources
|
||||
from distutils.version import StrictVersion
|
||||
@ -21,13 +21,14 @@ def version_check():
|
||||
current_pkg_version, latest_pkg_version))
|
||||
print('--------')
|
||||
except pkg_resources.DistributionNotFound:
|
||||
raise RuntimeError("gradio is not setup or installed properly. Unable to get version info.")
|
||||
warnings.warn("gradio is not setup or installed properly. Unable to get version info.")
|
||||
except json.decoder.JSONDecodeError:
|
||||
print("Warning: unable to parse version details from package URL.")
|
||||
warnings.warn("unable to parse version details from package URL.")
|
||||
except KeyError:
|
||||
print("Warning: Package URL does not contain version info.")
|
||||
warnings.warn("package URL does not contain version info.")
|
||||
except:
|
||||
print("Warning: unable to connect with package URL to collect version info.")
|
||||
warnings.warn("unable to connect with package URL to collect version info.")
|
||||
|
||||
|
||||
|
||||
def error_analytics(type):
|
||||
|
@ -2,6 +2,7 @@ from gradio.utils import *
|
||||
import unittest
|
||||
import pkg_resources
|
||||
import unittest.mock as mock
|
||||
import warnings
|
||||
|
||||
|
||||
class TestUtils(unittest.TestCase):
|
||||
@ -10,36 +11,40 @@ class TestUtils(unittest.TestCase):
|
||||
|
||||
mock_require.side_effect = pkg_resources.DistributionNotFound()
|
||||
|
||||
with self.assertRaises(RuntimeError) as e:
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
version_check()
|
||||
self.assertEqual(str(e.exception), "gradio is not setup or installed properly. Unable to get version info.")
|
||||
self.assertEqual(str(w[-1].message), "gradio is not setup or installed properly. Unable to get version info.")
|
||||
|
||||
@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)
|
||||
|
||||
with self.assertRaises(RuntimeWarning) as e:
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
version_check()
|
||||
self.assertEqual(str(e.exception), "Unable to parse version details from package URL.")
|
||||
self.assertEqual(str(w[-1].message), "unable to parse version details from package URL.")
|
||||
|
||||
@mock.patch("requests.get")
|
||||
def test_should_warn_with_connection_error(self, mock_get):
|
||||
|
||||
mock_get.side_effect = ConnectionError()
|
||||
|
||||
with self.assertRaises(RuntimeWarning) as e:
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
version_check()
|
||||
self.assertEqual(str(e.exception), "Unable to connect with package URL to collect version info.")
|
||||
self.assertEqual(str(w[-1].message), "unable to connect with package URL to collect version info.")
|
||||
|
||||
@mock.patch("requests.Response.json")
|
||||
def test_should_warn_url_not_having_version(self, mock_json):
|
||||
|
||||
mock_json.return_value = {"foo": "bar"}
|
||||
|
||||
with self.assertRaises(RuntimeWarning) as e:
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
version_check()
|
||||
self.assertEqual(str(e.exception), "Package URL does not contain version info.")
|
||||
self.assertEqual(str(w[-1].message), "package URL does not contain version info.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
Reference in New Issue
Block a user