1
0
mirror of https://github.com/cmur2/python-veml6070.git synced 2024-12-22 00:54:23 +01:00

test: change to unified, ordered read/write log for stability

This commit is contained in:
cn 2018-03-14 01:43:24 +01:00
parent 555dbfce54
commit 38e7af46f6
2 changed files with 162 additions and 130 deletions

View File

@ -7,122 +7,160 @@ from snapshottest import Snapshot
snapshots = Snapshot() snapshots = Snapshot()
snapshots['TestVeml6070::test_disable 1'] = { snapshots['TestVeml6070::test_uva_light_intensity_raw 1'] = [
'readlog': [ (
], 'w',
'writelog': [ 56,
( 6
'56', ),
[ (
6, 'w',
6, 56,
6 6
] ),
) (
] 'w',
} 56,
6
),
(
'r',
57,
18
),
(
'r',
56,
52
),
(
'w',
56,
6
)
]
snapshots['TestVeml6070::test_enable 1'] = { snapshots['TestVeml6070::test_disable 1'] = [
'readlog': [ (
], 'w',
'writelog': [ 56,
( 6
'56', ),
[ (
6, 'w',
6, 56,
6 6
] ),
) (
] 'w',
} 56,
6
)
]
snapshots['TestVeml6070::test_integration_time 1'] = { snapshots['TestVeml6070::test_enable 1'] = [
'readlog': [ (
], 'w',
'writelog': [ 56,
( 6
'56', ),
[ (
2, 'w',
2, 56,
14 6
] ),
) (
] 'w',
} 56,
6
)
]
snapshots['TestVeml6070::test_setup 1'] = { snapshots['TestVeml6070::test_integration_time 1'] = [
'readlog': [ (
], 'w',
'writelog': [ 56,
( 2
'56', ),
[ (
6, 'w',
6 56,
] 2
) ),
] (
} 'w',
56,
14
)
]
snapshots['TestVeml6070::test_uva_light_intensity 1'] = { snapshots['TestVeml6070::test_setup 1'] = [
'readlog': [ (
( 'w',
'56', 56,
[ 6
6, ),
6 (
] 'w',
), 56,
( 6
'57', )
[ ]
1,
1
]
)
],
'writelog': [
(
'56',
[
6,
6,
6,
6,
14,
14,
14
]
)
]
}
snapshots['TestVeml6070::test_uva_light_intensity_raw 1'] = { snapshots['TestVeml6070::test_uva_light_intensity 1'] = [
'readlog': [ (
( 'w',
'56', 56,
[ 6
52 ),
] (
), 'w',
( 56,
'57', 6
[ ),
18 (
] 'w',
) 56,
], 6
'writelog': [ ),
( (
'56', 'r',
[ 57,
6, 1
6, ),
6, (
6 'r',
] 56,
) 6
] ),
} (
'w',
56,
6
),
(
'w',
56,
14
),
(
'w',
56,
14
),
(
'r',
57,
1
),
(
'r',
56,
6
),
(
'w',
56,
14
)
]

View File

@ -16,23 +16,16 @@ import veml6070
# inspired by https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/I2C.py # inspired by https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/I2C.py
class MockSMBus(object): class MockSMBus(object):
def __init__(self, initial_read=None): def __init__(self, initial_read=None):
self._writelog = {} self._log = []
self._readlog = {}
self.initial_read = initial_read or {} self.initial_read = initial_read or {}
def read_byte(self, addr): def read_byte(self, addr):
val = self.initial_read.get(addr).pop(0) val = self.initial_read.get(addr).pop(0)
self._readlog.setdefault(addr, []).append(val) self._log.append(('r', addr, val))
return val return val
def write_byte(self, addr, val): def write_byte(self, addr, val):
self._writelog.setdefault(addr, []).append(val) self._log.append(('w', addr, 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()]
}
# def create_veml6070(**kwargs): # def create_veml6070(**kwargs):
# mockbus = MockSMBus() # mockbus = MockSMBus()
@ -53,8 +46,9 @@ class TestVeml6070(snapshottest.TestCase):
def test_setup(self): def test_setup(self):
mockbus = setup_mockbus() mockbus = setup_mockbus()
veml = veml6070.Veml6070() veml = veml6070.Veml6070()
MOCKED_SMBUS_MODULE.SMBus.assert_called_with(1)
self.assertIsNotNone(veml) self.assertIsNotNone(veml)
self.assertMatchSnapshot(mockbus._get_log()) self.assertMatchSnapshot(mockbus._log)
def test_integration_time(self): def test_integration_time(self):
mockbus = setup_mockbus() mockbus = setup_mockbus()
@ -62,19 +56,19 @@ class TestVeml6070(snapshottest.TestCase):
self.assertEqual(veml.get_integration_time(), veml6070.INTEGRATIONTIME_1_2T) self.assertEqual(veml.get_integration_time(), veml6070.INTEGRATIONTIME_1_2T)
veml.set_integration_time(veml6070.INTEGRATIONTIME_4T) veml.set_integration_time(veml6070.INTEGRATIONTIME_4T)
self.assertEqual(veml.get_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): def test_enable(self):
mockbus = setup_mockbus() mockbus = setup_mockbus()
veml = veml6070.Veml6070() veml = veml6070.Veml6070()
veml.enable() veml.enable()
self.assertMatchSnapshot(mockbus._get_log()) self.assertMatchSnapshot(mockbus._log)
def test_disable(self): def test_disable(self):
mockbus = setup_mockbus() mockbus = setup_mockbus()
veml = veml6070.Veml6070() veml = veml6070.Veml6070()
veml.disable() veml.disable()
self.assertMatchSnapshot(mockbus._get_log()) self.assertMatchSnapshot(mockbus._log)
def test_uva_light_intensity_raw(self): def test_uva_light_intensity_raw(self):
mockbus = setup_mockbus(initial_read={ mockbus = setup_mockbus(initial_read={
@ -83,7 +77,7 @@ class TestVeml6070(snapshottest.TestCase):
}) })
veml = veml6070.Veml6070() veml = veml6070.Veml6070()
self.assertEqual(veml.get_uva_light_intensity_raw(), 0x1234) self.assertEqual(veml.get_uva_light_intensity_raw(), 0x1234)
self.assertMatchSnapshot(mockbus._get_log()) self.assertMatchSnapshot(mockbus._log)
def test_uva_light_intensity(self): def test_uva_light_intensity(self):
mockbus = setup_mockbus(initial_read={ mockbus = setup_mockbus(initial_read={
@ -94,4 +88,4 @@ class TestVeml6070(snapshottest.TestCase):
self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * 0.05625 / 1) self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * 0.05625 / 1)
veml.set_integration_time(veml6070.INTEGRATIONTIME_4T) veml.set_integration_time(veml6070.INTEGRATIONTIME_4T)
self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * 0.05625 / 4) self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * 0.05625 / 4)
self.assertMatchSnapshot(mockbus._get_log()) self.assertMatchSnapshot(mockbus._log)