2011-06-03 14:56:16 +00:00
|
|
|
import unittest
|
2011-09-19 18:01:36 +00:00
|
|
|
from saucebrush.stats import Sum, Average, Median, MinMax, StandardDeviation, Histogram
|
2011-06-03 14:56:16 +00:00
|
|
|
|
|
|
|
|
2022-11-11 03:26:09 +00:00
|
|
|
class StatsTestCase(unittest.TestCase):
|
2011-06-03 14:56:16 +00:00
|
|
|
def _simple_data(self):
|
2022-11-11 03:26:09 +00:00
|
|
|
return [
|
|
|
|
{"a": 1, "b": 2, "c": 3},
|
|
|
|
{"a": 5, "b": 5, "c": 5},
|
|
|
|
{"a": 1, "b": 10, "c": 100},
|
|
|
|
]
|
2011-06-03 14:56:16 +00:00
|
|
|
|
|
|
|
def test_sum(self):
|
2022-11-11 03:26:09 +00:00
|
|
|
fltr = Sum("b")
|
2011-06-03 14:56:16 +00:00
|
|
|
list(fltr.attach(self._simple_data()))
|
|
|
|
self.assertEqual(fltr.value(), 17)
|
2022-11-11 03:26:09 +00:00
|
|
|
|
2011-06-03 14:56:16 +00:00
|
|
|
def test_average(self):
|
2022-11-11 03:26:09 +00:00
|
|
|
fltr = Average("c")
|
2011-06-03 14:56:16 +00:00
|
|
|
list(fltr.attach(self._simple_data()))
|
|
|
|
self.assertEqual(fltr.value(), 36.0)
|
2022-11-11 03:26:09 +00:00
|
|
|
|
2011-06-03 14:56:16 +00:00
|
|
|
def test_median(self):
|
2011-09-14 17:23:22 +00:00
|
|
|
# odd number of values
|
2022-11-11 03:26:09 +00:00
|
|
|
fltr = Median("a")
|
2011-06-03 14:56:16 +00:00
|
|
|
list(fltr.attach(self._simple_data()))
|
|
|
|
self.assertEqual(fltr.value(), 1)
|
2022-11-11 03:26:09 +00:00
|
|
|
|
2011-09-14 17:23:22 +00:00
|
|
|
# even number of values
|
2022-11-11 03:26:09 +00:00
|
|
|
fltr = Median("a")
|
2011-09-14 17:23:22 +00:00
|
|
|
list(fltr.attach(self._simple_data()[:2]))
|
|
|
|
self.assertEqual(fltr.value(), 3)
|
2022-11-11 03:26:09 +00:00
|
|
|
|
2011-06-03 14:56:16 +00:00
|
|
|
def test_minmax(self):
|
2022-11-11 03:26:09 +00:00
|
|
|
fltr = MinMax("b")
|
2011-06-03 14:56:16 +00:00
|
|
|
list(fltr.attach(self._simple_data()))
|
|
|
|
self.assertEqual(fltr.value(), (2, 10))
|
2022-11-11 03:26:09 +00:00
|
|
|
|
2011-06-03 14:56:16 +00:00
|
|
|
def test_standard_deviation(self):
|
2022-11-11 03:26:09 +00:00
|
|
|
fltr = StandardDeviation("c")
|
2011-06-03 14:56:16 +00:00
|
|
|
list(fltr.attach(self._simple_data()))
|
|
|
|
self.assertEqual(fltr.average(), 36.0)
|
|
|
|
self.assertEqual(fltr.median(), 5)
|
|
|
|
self.assertEqual(fltr.value(), (55.4346462061408, 3073.0))
|
|
|
|
self.assertEqual(fltr.value(True), (45.2621990922521, 2048.6666666666665))
|
2022-11-11 03:26:09 +00:00
|
|
|
|
2011-09-19 18:01:36 +00:00
|
|
|
def test_histogram(self):
|
2022-11-11 03:26:09 +00:00
|
|
|
fltr = Histogram("a")
|
2011-09-19 18:01:36 +00:00
|
|
|
fltr.label_length = 1
|
|
|
|
list(fltr.attach(self._simple_data()))
|
2011-09-19 19:23:02 +00:00
|
|
|
self.assertEqual(str(fltr), "\n1 **\n5 *\n")
|
2011-06-03 14:56:16 +00:00
|
|
|
|
2022-11-11 03:26:09 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2011-06-03 14:56:16 +00:00
|
|
|
unittest.main()
|