Handle total=None on get_bootstraped_percent

This commit is contained in:
Santiago L 2021-10-22 12:10:00 +02:00
parent aee0267f17
commit bd42b83ea3
2 changed files with 35 additions and 2 deletions

View file

@ -1,9 +1,37 @@
from django.test import TestCase from django.test import TestCase
from .models import UserAccount from .models import DatabaseService, UserAccount
from .utils import get_bootstraped_percent from .utils import get_bootstraped_percent
class DatabaseTest(TestCase):
def test_database_from_json(self):
data = {
"url": "https://example.org/api/databases/1/",
"id": 1,
"name": "bluebird",
"type": "mysql",
"users": [
{
"url": "https://example.org/api/databaseusers/2/",
"id": 2,
"username": "bluebird"
}
],
"resources": [
{
"name": "disk",
"used": "1.798",
"allocated": None,
"unit": "MiB"
}
]
}
database = DatabaseService.new_from_json(data)
self.assertEqual(0, database.usage['percent'])
class DomainsTestCase(TestCase): class DomainsTestCase(TestCase):
def test_domain_not_found(self): def test_domain_not_found(self):
response = self.client.post( response = self.client.post(
@ -118,3 +146,8 @@ class GetBootstrapedPercentTest(TestCase):
def test_invalid_total_is_zero(self): def test_invalid_total_is_zero(self):
value = get_bootstraped_percent(25, 0) value = get_bootstraped_percent(25, 0)
self.assertEqual(value, 0)
def test_invalid_total_is_none(self):
value = get_bootstraped_percent(25, None)
self.assertEqual(value, 0)

View file

@ -6,7 +6,7 @@ def get_bootstraped_percent(value, total):
""" """
try: try:
percent = value / total percent = value / total
except ZeroDivisionError: except (TypeError, ZeroDivisionError):
return 0 return 0
bootstraped = round(percent * 4) * 100 // 4 bootstraped = round(percent * 4) * 100 // 4