Commit f98ae9d9 authored by Titouan Soulard's avatar Titouan Soulard

tests: use `assertAlmostEqual` for float comparison

Floating point arithmetics often lead to unsignificant differences between
expected and computed values (this is the typical example of `0.1 + 0.2 !=
0.3`). Because of this, some tests using `assertEqual` to compare two floats
were failing. The Python method
[`assertAlmostEqual`](https://docs.python.org/2.7/library/unittest.html#unittest
.TestCase.assertAlmostEqual) can prevent these issues by comparing values up to
the seventh decimal place. To find all occurences, two RegExp were ran on all
files of the `tests/` folder: `assertEquals?(\\(.+, [0-9]+\\.[0-9]+\\))` and
`assertEquals?(\\([0-9]+\\.[0-9]+)`.

/cc @xavier_thompson @tomo
/reviewed-by @jerome
/reviewed-on !136 slapos.core!719
parent f155972d
Pipeline #38377 failed with stage
in 0 seconds
...@@ -51,28 +51,28 @@ class TestMonitorCollect(unittest.TestCase): ...@@ -51,28 +51,28 @@ class TestMonitorCollect(unittest.TestCase):
self.collector = ResourceCollect('/tmp/') self.collector = ResourceCollect('/tmp/')
def test_getPartitionUsedMemoryAverage(self): def test_getPartitionUsedMemoryAverage(self):
self.assertEqual(1195.492578125, self.assertAlmostEqual(1195.492578125,
self.collector.getPartitionUsedMemoryAverage('slapuser15', '2017-09-16')) self.collector.getPartitionUsedMemoryAverage('slapuser15', '2017-09-16'))
def test_getPartitionCPULoadAverage(self): def test_getPartitionCPULoadAverage(self):
self.assertEqual(2.1599999999999993, self.assertAlmostEqual(2.16,
self.collector.getPartitionCPULoadAverage('slapuser15', '2017-09-16')) self.collector.getPartitionCPULoadAverage('slapuser15', '2017-09-16'))
def test_getPartitionDiskUsedAverage(self): def test_getPartitionDiskUsedAverage(self):
self.assertEqual(35.5234375, self.assertAlmostEqual(35.5234375,
self.collector.getPartitionDiskUsedAverage('slapuser15', '2017-04-18')) self.collector.getPartitionDiskUsedAverage('slapuser15', '2017-04-18'))
def test_getPartitionConsumption(self): def test_getPartitionConsumption(self):
data = self.collector.getPartitionConsumption('slapuser15', date_scope='2017-09-16', data = self.collector.getPartitionConsumption('slapuser15', date_scope='2017-09-16',
min_time='00:01:00', max_time='00:13:00') min_time='00:01:00', max_time='00:13:00')
self.assertEqual(1302.66, data[0]['cpu_time']) self.assertAlmostEqual(1302.66, data[0]['cpu_time'])
self.assertEqual(26825304064.0, data[0]['io_rw_counter']) self.assertAlmostEqual(26825304064.0, data[0]['io_rw_counter'])
def test_getPartitionComsumptionStatus(self): def test_getPartitionComsumptionStatus(self):
data = self.collector.getPartitionComsumptionStatus('slapuser15', date_scope='2017-09-16', data = self.collector.getPartitionComsumptionStatus('slapuser15', date_scope='2017-09-16',
min_time='00:01:00', max_time='00:13:00') min_time='00:01:00', max_time='00:13:00')
self.assertEqual(7.3, data[0]['cpu_percent']) self.assertAlmostEqual(7.3, data[0]['cpu_percent'])
self.assertEqual(2822535483392.0, data[2]['io_rw_counter']) self.assertAlmostEqual(2822535483392.0, data[2]['io_rw_counter'])
def tearDown(self): def tearDown(self):
os.remove("/tmp/collector.db") os.remove("/tmp/collector.db")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment