summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2013-03-29 16:56:24 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2013-03-29 17:01:43 -0400
commitcbf60d32586bf848fed9d6b0a37451c6cf05fa99 (patch)
tree89ec652a352caab8c096c937ec5a9b0ab979edee
parentfa402689293283222915f372fa37f233c97f4ed6 (diff)
downloadbcfg2-cbf60d32586bf848fed9d6b0a37451c6cf05fa99.tar.gz
bcfg2-cbf60d32586bf848fed9d6b0a37451c6cf05fa99.tar.bz2
bcfg2-cbf60d32586bf848fed9d6b0a37451c6cf05fa99.zip
Statistics: wrote unit tests
-rw-r--r--src/lib/Bcfg2/Statistics.py4
-rw-r--r--testsuite/Testsrc/Testlib/TestStatistics.py44
2 files changed, 46 insertions, 2 deletions
diff --git a/src/lib/Bcfg2/Statistics.py b/src/lib/Bcfg2/Statistics.py
index 01b0a80b1..3825941af 100644
--- a/src/lib/Bcfg2/Statistics.py
+++ b/src/lib/Bcfg2/Statistics.py
@@ -28,8 +28,8 @@ class Statistic(object):
:param value: The value to add to this statistic
:type value: int or float
"""
- self.min = min(self.min, value)
- self.max = max(self.max, value)
+ self.min = min(self.min, float(value))
+ self.max = max(self.max, float(value))
self.count += 1
self.ave = (((self.ave * (self.count - 1)) + value) / self.count)
diff --git a/testsuite/Testsrc/Testlib/TestStatistics.py b/testsuite/Testsrc/Testlib/TestStatistics.py
new file mode 100644
index 000000000..496cbac28
--- /dev/null
+++ b/testsuite/Testsrc/Testlib/TestStatistics.py
@@ -0,0 +1,44 @@
+import os
+import sys
+from mock import Mock, MagicMock, patch
+
+# add all parent testsuite directories to sys.path to allow (most)
+# relative imports in python 2.4
+path = os.path.dirname(__file__)
+while path != "/":
+ if os.path.basename(path).lower().startswith("test"):
+ sys.path.append(path)
+ if os.path.basename(path) == "testsuite":
+ break
+ path = os.path.dirname(path)
+from common import *
+
+from Bcfg2.Statistics import *
+
+
+class TestStatistic(Bcfg2TestCase):
+ def test_stat(self):
+ stat = Statistic("test", 1)
+ self.assertEqual(stat.get_value(), ("test", (1.0, 1.0, 1.0, 1)))
+ stat.add_value(10)
+ self.assertEqual(stat.get_value(), ("test", (1.0, 10.0, 5.5, 2)))
+ stat.add_value(100)
+ self.assertEqual(stat.get_value(), ("test", (1.0, 100.0, 37.0, 3)))
+ stat.add_value(12.345)
+ self.assertEqual(stat.get_value(), ("test", (1.0, 100.0, 30.83625, 4)))
+ stat.add_value(0.655)
+ self.assertEqual(stat.get_value(), ("test", (0.655, 100.0, 24.8, 5)))
+
+
+class TestStatistics(Bcfg2TestCase):
+ def test_stats(self):
+ stats = Statistics()
+ self.assertEqual(stats.display(), dict())
+ stats.add_value("test1", 1)
+ self.assertEqual(stats.display(), dict(test1=(1.0, 1.0, 1.0, 1)))
+ stats.add_value("test2", 1.23)
+ self.assertEqual(stats.display(), dict(test1=(1.0, 1.0, 1.0, 1),
+ test2=(1.23, 1.23, 1.23, 1)))
+ stats.add_value("test1", 10)
+ self.assertEqual(stats.display(), dict(test1=(1.0, 10.0, 5.5, 2),
+ test2=(1.23, 1.23, 1.23, 1)))