Skip to content

Commit

Permalink
Merge pull request #6 from erunks/reduce-errors
Browse files Browse the repository at this point in the history
Reduce frequent errors
  • Loading branch information
erunks authored Aug 20, 2020
2 parents 9673f79 + a7dd659 commit 35715d9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
30 changes: 26 additions & 4 deletions src/tests/utils_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from freezegun import freeze_time
from unittest.mock import Mock, patch
from src.utils import calculate_percentage_lost, calculate_standard_deviation, get_addresses, get_downtime, ping_hosts
from src.utils import calculate_percentage_lost, calculate_standard_deviation, get_addresses, get_downtime, ping_hosts, remove_none

class TestUtilMethods(unittest.TestCase):
@classmethod
Expand All @@ -23,17 +23,33 @@ def test_calculate_standard_deviation_when_an_array_of_numbers_is_passed(self, m
(3.0, 1.5811388300841898)
)

@patch('logging.Logger')
def test_calculate_standard_deviation_when_an_array_of_numbers_and_none_is_passed(self, mock_logger):
self.assertEqual(
calculate_standard_deviation([None,1.0,2,3,None,4.0,5], mock_logger),
(3.0, 1.5811388300841898)
)

@patch('logging.Logger')
def test_calculate_standard_deviation_when_an_array_of_none_is_passed(self, mock_logger):
self.assertEqual(
calculate_standard_deviation([None, None, None, None], mock_logger),
None
)

# These two tests might not be the best, since we should never expect strings to be passed
# here, but we do still want to check the TypeError is raised and the exception is logged.
# The strings will help trigger those.
@patch('logging.Logger')
def test_calculate_standard_deviation_when_a_TypeError_is_raised(self, mock_logger):
self.assertRaises(
TypeError,
calculate_standard_deviation,
([None, None, None, None], mock_logger)
calculate_standard_deviation(["String", 1.0, 2.0, 3.0], mock_logger)
)

@patch('logging.Logger')
def test_calculate_standard_deviation_when_an_exception_is_logged(self, mock_logger):
calculate_standard_deviation([None, None, None, None], mock_logger)
calculate_standard_deviation(["some", "string", "right", "here"], mock_logger)
mock_logger.exception.assert_called_once()

def test_get_addresses_when_passed_an_empty_array(self):
Expand All @@ -59,3 +75,9 @@ def test_ping_hosts(self, mock_run):

self.assertListEqual(ping_hosts(['8.8.4.4'], 5), [self.ping_response])
mock_run.assert_called_with(self.ping_args, stdout=PIPE)

def test_remove_none_when_passed_an_array_of_none(self):
self.assertEqual(remove_none([None,None,None,None]),[])

def test_remove_none_when_passed_an_array_containing_none(self):
self.assertEqual(remove_none([1,None,"Something",420.69]),[1, "Something", 420.69])
6 changes: 5 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ def calculate_standard_deviation(data, logger):
from math import sqrt
from sys import exc_info

data = remove_none(data)
length = len(data)

if length == 0:
return

try:
mean = reduce(lambda a,b: a+b, data)/length
deviation = 0
Expand Down Expand Up @@ -53,3 +54,6 @@ def ping_hosts(host_addresses, ping_count):
responses.append(response)

return responses

def remove_none(array):
return [e for _,e in enumerate(array) if not e == None]

0 comments on commit 35715d9

Please sign in to comment.