From bd42b83ea34bac311f51aa025dfe19257f0fd2e1 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Fri, 22 Oct 2021 12:10:00 +0200 Subject: [PATCH] Handle total=None on get_bootstraped_percent --- musician/tests.py | 35 ++++++++++++++++++++++++++++++++++- musician/utils.py | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/musician/tests.py b/musician/tests.py index 8becf4d..4927c4a 100644 --- a/musician/tests.py +++ b/musician/tests.py @@ -1,9 +1,37 @@ from django.test import TestCase -from .models import UserAccount +from .models import DatabaseService, UserAccount 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): def test_domain_not_found(self): response = self.client.post( @@ -118,3 +146,8 @@ class GetBootstrapedPercentTest(TestCase): def test_invalid_total_is_zero(self): 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) diff --git a/musician/utils.py b/musician/utils.py index affc93f..8dea94e 100644 --- a/musician/utils.py +++ b/musician/utils.py @@ -6,7 +6,7 @@ def get_bootstraped_percent(value, total): """ try: percent = value / total - except ZeroDivisionError: + except (TypeError, ZeroDivisionError): return 0 bootstraped = round(percent * 4) * 100 // 4