mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Tests for helper functions
This commit is contained in:
parent
08358f6961
commit
38ef9b2b13
@ -4,6 +4,7 @@ omit =
|
||||
# Do not run coverage on migration files
|
||||
*/migrations/*
|
||||
InvenTree/manage.py
|
||||
InvenTree/keygen.py
|
||||
Inventree/InvenTree/middleware.py
|
||||
Inventree/InvenTree/utils.py
|
||||
Inventree/InvenTree/wsgi.py
|
@ -30,67 +30,8 @@ def TestIfImageURL(url):
|
||||
'.jpg', '.jpeg',
|
||||
'.png', '.bmp',
|
||||
'.tif', '.tiff',
|
||||
'.webp',
|
||||
'.webp', '.gif',
|
||||
]
|
||||
|
||||
|
||||
def DownloadExternalFile(url, **kwargs):
|
||||
""" Attempt to download an external file
|
||||
|
||||
Args:
|
||||
url - External URL
|
||||
|
||||
"""
|
||||
|
||||
result = {
|
||||
'status': False,
|
||||
'url': url,
|
||||
'file': None,
|
||||
'status_code': 200,
|
||||
}
|
||||
|
||||
headers = {'User-Agent': 'Mozilla/5.0'}
|
||||
max_size = kwargs.get('max_size', 1048576) # 1MB default limit
|
||||
|
||||
# Get the HEAD for the file
|
||||
try:
|
||||
head = requests.head(url, stream=True, headers=headers)
|
||||
except:
|
||||
result['error'] = 'Error retrieving HEAD data'
|
||||
return result
|
||||
|
||||
if not head.status_code == 200:
|
||||
result['error'] = 'Incorrect HEAD status code'
|
||||
result['status_code'] = head.status_code
|
||||
return result
|
||||
|
||||
try:
|
||||
filesize = int(head.headers['Content-Length'])
|
||||
except ValueError:
|
||||
result['error'] = 'Could not decode filesize'
|
||||
result['extra'] = head.headers['Content-Length']
|
||||
return result
|
||||
|
||||
if filesize > max_size:
|
||||
result['error'] = 'File size too large ({s})'.format(s=filesize)
|
||||
return result
|
||||
|
||||
# All checks have passed - download the file
|
||||
|
||||
try:
|
||||
request = requests.get(url, stream=True, headers=headers)
|
||||
except:
|
||||
result['error'] = 'Error retriving GET data'
|
||||
return result
|
||||
|
||||
try:
|
||||
dl_file = io.StringIO(request.text)
|
||||
result['status'] = True
|
||||
result['file'] = dl_file
|
||||
return result
|
||||
except:
|
||||
result['error'] = 'Could not convert downloaded data to file'
|
||||
return result
|
||||
|
||||
|
||||
def str2bool(text, test=True):
|
||||
|
@ -2,6 +2,8 @@ from django.test import TestCase
|
||||
import django.core.exceptions as django_exceptions
|
||||
|
||||
from .validators import validate_overage, validate_part_name
|
||||
from . import helpers
|
||||
|
||||
|
||||
class ValidatorTest(TestCase):
|
||||
|
||||
@ -36,3 +38,66 @@ class ValidatorTest(TestCase):
|
||||
|
||||
with self.assertRaises(django_exceptions.ValidationError):
|
||||
validate_overage("aaaa")
|
||||
|
||||
|
||||
class TestHelpers(TestCase):
|
||||
""" Tests for InvenTree helper functions """
|
||||
|
||||
def test_image_url(self):
|
||||
""" Test if a filename looks like an image """
|
||||
|
||||
for name in ['ape.png', 'bat.GiF', 'apple.WeBP', 'BiTMap.Bmp']:
|
||||
self.assertTrue(helpers.TestIfImageURL(name))
|
||||
|
||||
for name in ['no.doc', 'nah.pdf', 'whatpng']:
|
||||
self.assertFalse(helpers.TestIfImageURL(name))
|
||||
|
||||
def test_str2bool(self):
|
||||
""" Test string to boolean conversion """
|
||||
|
||||
for s in ['yes', 'Y', 'ok', '1', 'OK', 'Ok', 'tRuE', 'oN']:
|
||||
self.assertTrue(helpers.str2bool(s))
|
||||
self.assertFalse(helpers.str2bool(s, test=False))
|
||||
|
||||
for s in ['nO', '0', 'none', 'noNE', None, False, 'falSe', 'off']:
|
||||
self.assertFalse(helpers.str2bool(s))
|
||||
self.assertTrue(helpers.str2bool(s, test=False))
|
||||
|
||||
for s in ['wombat', '', 'xxxx']:
|
||||
self.assertFalse(helpers.str2bool(s))
|
||||
self.assertFalse(helpers.str2bool(s, test=False))
|
||||
|
||||
|
||||
class TestQuoteWrap(TestCase):
|
||||
""" Tests for string wrapping """
|
||||
|
||||
def test_single(self):
|
||||
|
||||
self.assertEqual(helpers.WrapWithQuotes('hello'), '"hello"')
|
||||
self.assertEqual(helpers.WrapWithQuotes('hello"'), '"hello"')
|
||||
|
||||
|
||||
class TestMakeBarcoede(TestCase):
|
||||
""" Tests for barcode string creation """
|
||||
|
||||
def test_barcode(self):
|
||||
|
||||
data = {
|
||||
'animal': 'cat',
|
||||
'legs': 3,
|
||||
'noise': 'purr'
|
||||
}
|
||||
|
||||
bc = helpers.MakeBarcode("part", 3, "www.google.com", data)
|
||||
|
||||
self.assertIn('animal', bc)
|
||||
self.assertIn('tool', bc)
|
||||
self.assertIn('"tool": "InvenTree"', bc)
|
||||
|
||||
|
||||
class TestDownloadFile(TestCase):
|
||||
|
||||
|
||||
def test_download(self):
|
||||
helpers.DownloadFile("hello world", "out.txt")
|
||||
helpers.DownloadFile(bytes("hello world".encode("utf8")), "out.bin")
|
||||
|
Loading…
Reference in New Issue
Block a user