From 5999d0347bc4ddb3c2add89067634543c368f46c Mon Sep 17 00:00:00 2001 From: Edward Runkel Date: Mon, 31 Aug 2020 11:53:47 -0400 Subject: [PATCH 1/2] Add edge case for when the data passed contains only one number and the rest is a NoneType --- src/tests/utils_test.py | 7 +++++++ src/utils.py | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/tests/utils_test.py b/src/tests/utils_test.py index 23ac848..042f759 100644 --- a/src/tests/utils_test.py +++ b/src/tests/utils_test.py @@ -30,6 +30,13 @@ def test_calculate_standard_deviation_when_an_array_of_numbers_and_none_is_passe (3.0, 1.5811388300841898) ) + @patch('logging.Logger') + def test_calculate_standard_deviation_when_an_array_of_one_number_and_none_is_passed(self, mock_logger): + self.assertEqual( + calculate_standard_deviation([None,1.0,None], mock_logger), + (1.0, 0) + ) + @patch('logging.Logger') def test_calculate_standard_deviation_when_an_array_of_none_is_passed(self, mock_logger): self.assertEqual( diff --git a/src/utils.py b/src/utils.py index 765987f..94ad002 100644 --- a/src/utils.py +++ b/src/utils.py @@ -20,6 +20,8 @@ def calculate_standard_deviation(data, logger): if length == 0: return + elif length == 1: + return data[0], 0 try: mean = reduce(lambda a,b: a+b, data)/length From 9fe20f76f2b6ba6d6b05f7ece4405c4ac3b368c3 Mon Sep 17 00:00:00 2001 From: Edward Runkel Date: Tue, 1 Sep 2020 11:02:38 -0400 Subject: [PATCH 2/2] Move where `remove_none` is being called so we stop trying to compare NoneTypes and Numbers --- src/InternetStatusReporter.py | 3 ++- src/tests/utils_test.py | 15 ++++----------- src/utils.py | 1 - 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/InternetStatusReporter.py b/src/InternetStatusReporter.py index 18804d9..ba0fa12 100644 --- a/src/InternetStatusReporter.py +++ b/src/InternetStatusReporter.py @@ -114,13 +114,14 @@ def measure_latency(self): from json import dumps from sys import exc_info from tcp_latency import measure_latency - from src.utils import calculate_standard_deviation + from src.utils import calculate_standard_deviation, remove_none results = {} for latency_address in self.latency_addresses: try: runs = int(getenv('LATENCY_RUNS')) data = measure_latency(host=latency_address, port=80, runs=runs, timeout=2.5) + data = remove_none(data) mean, deviation = calculate_standard_deviation(data, self.logger) except: self.logger.exception(f'Unexpected error: {exc_info()[0]}') diff --git a/src/tests/utils_test.py b/src/tests/utils_test.py index 042f759..8a18f2f 100644 --- a/src/tests/utils_test.py +++ b/src/tests/utils_test.py @@ -24,23 +24,16 @@ def test_calculate_standard_deviation_when_an_array_of_numbers_is_passed(self, m ) @patch('logging.Logger') - def test_calculate_standard_deviation_when_an_array_of_numbers_and_none_is_passed(self, mock_logger): + def test_calculate_standard_deviation_when_an_array_of_one_number_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_one_number_and_none_is_passed(self, mock_logger): - self.assertEqual( - calculate_standard_deviation([None,1.0,None], mock_logger), + calculate_standard_deviation([1.0], mock_logger), (1.0, 0) ) @patch('logging.Logger') - def test_calculate_standard_deviation_when_an_array_of_none_is_passed(self, mock_logger): + def test_calculate_standard_deviation_when_an_empty_array_is_passed(self, mock_logger): self.assertEqual( - calculate_standard_deviation([None, None, None, None], mock_logger), + calculate_standard_deviation([], mock_logger), None ) diff --git a/src/utils.py b/src/utils.py index 94ad002..7a08c7b 100644 --- a/src/utils.py +++ b/src/utils.py @@ -15,7 +15,6 @@ 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: