From 38e7af46f66d0dd89e6bd9245947f9bd468c19b4 Mon Sep 17 00:00:00 2001 From: cn Date: Wed, 14 Mar 2018 01:43:24 +0100 Subject: [PATCH] test: change to unified, ordered read/write log for stability --- tests/snapshots/snap_test_veml6070.py | 266 +++++++++++++++----------- tests/test_veml6070.py | 26 +-- 2 files changed, 162 insertions(+), 130 deletions(-) diff --git a/tests/snapshots/snap_test_veml6070.py b/tests/snapshots/snap_test_veml6070.py index a09594c..bcd13e0 100644 --- a/tests/snapshots/snap_test_veml6070.py +++ b/tests/snapshots/snap_test_veml6070.py @@ -7,122 +7,160 @@ from snapshottest import Snapshot snapshots = Snapshot() -snapshots['TestVeml6070::test_disable 1'] = { - 'readlog': [ - ], - 'writelog': [ - ( - '56', - [ - 6, - 6, - 6 - ] - ) - ] -} +snapshots['TestVeml6070::test_uva_light_intensity_raw 1'] = [ + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ), + ( + 'r', + 57, + 18 + ), + ( + 'r', + 56, + 52 + ), + ( + 'w', + 56, + 6 + ) +] -snapshots['TestVeml6070::test_enable 1'] = { - 'readlog': [ - ], - 'writelog': [ - ( - '56', - [ - 6, - 6, - 6 - ] - ) - ] -} +snapshots['TestVeml6070::test_disable 1'] = [ + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ) +] -snapshots['TestVeml6070::test_integration_time 1'] = { - 'readlog': [ - ], - 'writelog': [ - ( - '56', - [ - 2, - 2, - 14 - ] - ) - ] -} +snapshots['TestVeml6070::test_enable 1'] = [ + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ) +] -snapshots['TestVeml6070::test_setup 1'] = { - 'readlog': [ - ], - 'writelog': [ - ( - '56', - [ - 6, - 6 - ] - ) - ] -} +snapshots['TestVeml6070::test_integration_time 1'] = [ + ( + 'w', + 56, + 2 + ), + ( + 'w', + 56, + 2 + ), + ( + 'w', + 56, + 14 + ) +] -snapshots['TestVeml6070::test_uva_light_intensity 1'] = { - 'readlog': [ - ( - '56', - [ - 6, - 6 - ] - ), - ( - '57', - [ - 1, - 1 - ] - ) - ], - 'writelog': [ - ( - '56', - [ - 6, - 6, - 6, - 6, - 14, - 14, - 14 - ] - ) - ] -} +snapshots['TestVeml6070::test_setup 1'] = [ + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ) +] -snapshots['TestVeml6070::test_uva_light_intensity_raw 1'] = { - 'readlog': [ - ( - '56', - [ - 52 - ] - ), - ( - '57', - [ - 18 - ] - ) - ], - 'writelog': [ - ( - '56', - [ - 6, - 6, - 6, - 6 - ] - ) - ] -} +snapshots['TestVeml6070::test_uva_light_intensity 1'] = [ + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ), + ( + 'r', + 57, + 1 + ), + ( + 'r', + 56, + 6 + ), + ( + 'w', + 56, + 6 + ), + ( + 'w', + 56, + 14 + ), + ( + 'w', + 56, + 14 + ), + ( + 'r', + 57, + 1 + ), + ( + 'r', + 56, + 6 + ), + ( + 'w', + 56, + 14 + ) +] diff --git a/tests/test_veml6070.py b/tests/test_veml6070.py index 724a51e..efcc537 100644 --- a/tests/test_veml6070.py +++ b/tests/test_veml6070.py @@ -16,23 +16,16 @@ import veml6070 # inspired by https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/I2C.py class MockSMBus(object): def __init__(self, initial_read=None): - self._writelog = {} - self._readlog = {} + self._log = [] self.initial_read = initial_read or {} def read_byte(self, addr): val = self.initial_read.get(addr).pop(0) - self._readlog.setdefault(addr, []).append(val) + self._log.append(('r', addr, val)) return val def write_byte(self, addr, val): - self._writelog.setdefault(addr, []).append(val) - - def _get_log(self): - return { - 'readlog': [(str(key), value) for (key, value) in self._readlog.items()], - 'writelog': [(str(key), value) for (key, value) in self._writelog.items()] - } + self._log.append(('w', addr, val)) # def create_veml6070(**kwargs): # mockbus = MockSMBus() @@ -53,8 +46,9 @@ class TestVeml6070(snapshottest.TestCase): def test_setup(self): mockbus = setup_mockbus() veml = veml6070.Veml6070() + MOCKED_SMBUS_MODULE.SMBus.assert_called_with(1) self.assertIsNotNone(veml) - self.assertMatchSnapshot(mockbus._get_log()) + self.assertMatchSnapshot(mockbus._log) def test_integration_time(self): mockbus = setup_mockbus() @@ -62,19 +56,19 @@ class TestVeml6070(snapshottest.TestCase): self.assertEqual(veml.get_integration_time(), veml6070.INTEGRATIONTIME_1_2T) veml.set_integration_time(veml6070.INTEGRATIONTIME_4T) self.assertEqual(veml.get_integration_time(), veml6070.INTEGRATIONTIME_4T) - self.assertMatchSnapshot(mockbus._get_log()) + self.assertMatchSnapshot(mockbus._log) def test_enable(self): mockbus = setup_mockbus() veml = veml6070.Veml6070() veml.enable() - self.assertMatchSnapshot(mockbus._get_log()) + self.assertMatchSnapshot(mockbus._log) def test_disable(self): mockbus = setup_mockbus() veml = veml6070.Veml6070() veml.disable() - self.assertMatchSnapshot(mockbus._get_log()) + self.assertMatchSnapshot(mockbus._log) def test_uva_light_intensity_raw(self): mockbus = setup_mockbus(initial_read={ @@ -83,7 +77,7 @@ class TestVeml6070(snapshottest.TestCase): }) veml = veml6070.Veml6070() self.assertEqual(veml.get_uva_light_intensity_raw(), 0x1234) - self.assertMatchSnapshot(mockbus._get_log()) + self.assertMatchSnapshot(mockbus._log) def test_uva_light_intensity(self): mockbus = setup_mockbus(initial_read={ @@ -94,4 +88,4 @@ class TestVeml6070(snapshottest.TestCase): self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * 0.05625 / 1) veml.set_integration_time(veml6070.INTEGRATIONTIME_4T) self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * 0.05625 / 4) - self.assertMatchSnapshot(mockbus._get_log()) + self.assertMatchSnapshot(mockbus._log)