1
0
mirror of https://github.com/cmur2/python-veml6070.git synced 2024-11-14 06:56:16 +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['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
)
]

View File

@ -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)