Skip to content

bpo-40331: Increase test coverage for the statistics module #19608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 13, 2020
59 changes: 59 additions & 0 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,10 @@ def test_nan(self):
x = statistics._convert(nan, type(nan))
self.assertTrue(_nan_equal(x, nan))

def test_invalid_input_type(self):
with self.assertRaises(TypeError):
statistics._convert(None, float)


class FailNegTest(unittest.TestCase):
"""Test _fail_neg private function."""
Expand Down Expand Up @@ -1033,6 +1037,47 @@ def test_error_msg(self):
self.assertEqual(errmsg, msg)


class FindLteqTest(unittest.TestCase):
# Test _find_lteq private function.

def test_invalid_input_values(self):
for a, x in [
([], 1),
([1, 2], 3),
([1, 3], 2)
]:
with self.assertRaises(ValueError):
statistics._find_lteq(a, x)

def test_locate_successfully(self):
for a, x, expected_i in [
([1, 1, 1, 2, 3], 1, 0),
([0, 1, 1, 1, 2, 3], 1, 1),
([1, 2, 3, 3, 3], 3, 2)
]:
self.assertEqual(expected_i, statistics._find_lteq(a, x))


class FindRteqTest(unittest.TestCase):
# Test _find_rteq private function.

def test_raise_value_error(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test method could also be renamed, similarly to the previous ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should have changed this too.

for a, l, x in [
([1], 2, 1),
([1, 3], 0, 2)
]:
with self.assertRaises(ValueError):
statistics._find_rteq(a, l, x)

def test_locate_successfully(self):
for a, l, x, expected_i in [
([1, 1, 1, 2, 3], 0, 1, 2),
([0, 1, 1, 1, 2, 3], 0, 1, 3),
([1, 2, 3, 3, 3], 0, 3, 4)
]:
self.assertEqual(expected_i, statistics._find_rteq(a, l, x))


# === Tests for public functions ===

class UnivariateCommonMixin:
Expand Down Expand Up @@ -1476,6 +1521,20 @@ def test_negative_error(self):
with self.subTest(values=values):
self.assertRaises(exc, self.func, values)

def test_single_value_unsupported_type(self):
with self.assertRaises(TypeError):
self.func(['3.14'])

def test_multiple_values_type_error(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be combined with the previous test method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, now they assert the same behavior, with the same way. I will combine them under a test with name test_invalid_type_error.

# Test TypeError is raised when given multiple (valid/invalid) values
for data in [
['1', '2', '3'], # only strings
[1, '2', 3, '4', 5], # mixed strings and valid integers
[2.3, 3.4, 4.5, '5.6'] # only one string and valid floats
]:
with self.assertRaises(TypeError):
self.func(data)

def test_ints(self):
# Test harmonic mean with ints.
data = [2, 4, 4, 8, 16, 16]
Expand Down