fixed warnings & unittests

This commit is contained in:
Abubakar Abid 2021-09-14 10:28:55 -05:00
parent c45ca08b78
commit 69d29fa8a4
2 changed files with 19 additions and 13 deletions

View File

@ -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):

View File

@ -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__':