Improvements for CI tests (#3850)

* Improvements for CI tests

* Add delay between retries

* Reduce tiemeout for request

* Change exception type we are looking for
This commit is contained in:
Oliver 2022-10-25 21:53:44 +11:00 committed by GitHub
parent c07d5207e0
commit 824a44a486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -258,7 +258,7 @@ class TestHelpers(TestCase):
def test_download_image(self):
"""Test function for downloading image from remote URL"""
# Run check with a sequency of bad URLs
# Run check with a sequence of bad URLs
for url in [
"blog",
"htp://test.com/?",
@ -268,17 +268,35 @@ class TestHelpers(TestCase):
with self.assertRaises(django_exceptions.ValidationError):
helpers.download_image_from_url(url)
def dl_helper(url, expected_error, timeout=2.5, retries=3):
"""Helper function for unit testing downloads.
As the httpstat.us service occassionaly refuses a connection,
we will simply try multiple times
"""
with self.assertRaises(expected_error):
while retries > 0:
try:
helpers.download_image_from_url(url, timeout=timeout)
break
except Exception as exc:
if type(exc) is expected_error:
# Re-throw this error
raise exc
time.sleep(30)
retries -= 1
# Attempt to download an image which throws a 404
with self.assertRaises(requests.exceptions.HTTPError):
helpers.download_image_from_url("https://httpstat.us/404", timeout=10)
dl_helper("https://httpstat.us/404", requests.exceptions.HTTPError, timeout=10)
# Attempt to download, but timeout
with self.assertRaises(requests.exceptions.Timeout):
helpers.download_image_from_url("https://httpstat.us/200?sleep=5000")
dl_helper("https://httpstat.us/200?sleep=5000", requests.exceptions.ReadTimeout, timeout=1)
# Attempt to download, but not a valid image
with self.assertRaises(TypeError):
helpers.download_image_from_url("https://httpstat.us/200", timeout=10)
dl_helper("https://httpstat.us/200", TypeError, timeout=10)
large_img = "https://github.com/inventree/InvenTree/raw/master/InvenTree/InvenTree/static/img/paper_splash_large.jpg"