2018-03-14 00:39:10 +01:00
|
|
|
# import unittest
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
import mock
|
|
|
|
|
|
|
|
import snapshottest
|
|
|
|
|
|
|
|
# Note: prepare globally mocked modules first and then load our module
|
|
|
|
MOCKED_SMBUS_MODULE = mock.Mock()
|
|
|
|
sys.modules['smbus'] = MOCKED_SMBUS_MODULE
|
|
|
|
time.sleep = lambda s: None
|
2020-08-07 23:01:51 +02:00
|
|
|
import veml6070 # pylint: disable=wrong-import-position
|
|
|
|
|
2018-03-14 00:39:10 +01:00
|
|
|
|
|
|
|
# inspired by https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/I2C.py
|
2019-12-18 19:57:23 +01:00
|
|
|
class MockSMBus():
|
2018-03-14 00:39:10 +01:00
|
|
|
def __init__(self, initial_read=None):
|
2018-03-14 01:43:24 +01:00
|
|
|
self._log = []
|
2018-03-14 00:39:10 +01:00
|
|
|
self.initial_read = initial_read or {}
|
|
|
|
|
|
|
|
def read_byte(self, addr):
|
|
|
|
val = self.initial_read.get(addr).pop(0)
|
2018-03-14 01:43:24 +01:00
|
|
|
self._log.append(('r', addr, val))
|
2018-03-14 00:39:10 +01:00
|
|
|
return val
|
|
|
|
|
|
|
|
def write_byte(self, addr, val):
|
2018-03-14 01:43:24 +01:00
|
|
|
self._log.append(('w', addr, val))
|
2018-03-14 00:39:10 +01:00
|
|
|
|
2020-08-07 23:01:51 +02:00
|
|
|
|
2018-03-14 00:39:10 +01:00
|
|
|
# def create_veml6070(**kwargs):
|
|
|
|
# mockbus = MockSMBus()
|
|
|
|
# smbus = Mock()
|
|
|
|
# smbus.SMBus.return_value = mockbus
|
|
|
|
# with patch.dict('sys.modules', {'smbus': smbus}):
|
|
|
|
# import veml6070
|
|
|
|
# # Note: our module constants only available in this scope
|
|
|
|
# return (veml6070.Veml6070(**kwargs), mockbus)
|
|
|
|
|
2020-08-07 23:01:51 +02:00
|
|
|
|
2018-03-14 00:39:10 +01:00
|
|
|
def setup_mockbus(**kwargs):
|
|
|
|
mockbus = MockSMBus(**kwargs)
|
|
|
|
MOCKED_SMBUS_MODULE.SMBus.return_value = mockbus
|
|
|
|
return mockbus
|
|
|
|
|
|
|
|
|
2020-08-07 23:01:51 +02:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
class TestVeml6070(snapshottest.TestCase):
|
2018-03-14 00:39:10 +01:00
|
|
|
def test_setup(self):
|
|
|
|
mockbus = setup_mockbus()
|
|
|
|
veml = veml6070.Veml6070()
|
2018-03-14 01:43:24 +01:00
|
|
|
MOCKED_SMBUS_MODULE.SMBus.assert_called_with(1)
|
2018-03-14 00:39:10 +01:00
|
|
|
self.assertIsNotNone(veml)
|
2018-03-14 01:43:24 +01:00
|
|
|
self.assertMatchSnapshot(mockbus._log)
|
2018-03-14 00:39:10 +01:00
|
|
|
|
|
|
|
def test_integration_time(self):
|
|
|
|
mockbus = setup_mockbus()
|
|
|
|
veml = veml6070.Veml6070(integration_time=veml6070.INTEGRATIONTIME_1_2T)
|
|
|
|
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)
|
2018-03-14 01:43:24 +01:00
|
|
|
self.assertMatchSnapshot(mockbus._log)
|
2018-03-14 00:39:10 +01:00
|
|
|
|
|
|
|
def test_enable(self):
|
|
|
|
mockbus = setup_mockbus()
|
|
|
|
veml = veml6070.Veml6070()
|
|
|
|
veml.enable()
|
2018-03-14 01:43:24 +01:00
|
|
|
self.assertMatchSnapshot(mockbus._log)
|
2018-03-14 00:39:10 +01:00
|
|
|
|
|
|
|
def test_disable(self):
|
|
|
|
mockbus = setup_mockbus()
|
|
|
|
veml = veml6070.Veml6070()
|
|
|
|
veml.disable()
|
2018-03-14 01:43:24 +01:00
|
|
|
self.assertMatchSnapshot(mockbus._log)
|
2018-03-14 00:39:10 +01:00
|
|
|
|
|
|
|
def test_uva_light_intensity_raw(self):
|
2020-08-07 23:01:51 +02:00
|
|
|
mockbus = setup_mockbus(initial_read={0x38 + 1: [0x12], 0x38 + 0: [0x34]})
|
2018-03-14 00:39:10 +01:00
|
|
|
veml = veml6070.Veml6070()
|
|
|
|
self.assertEqual(veml.get_uva_light_intensity_raw(), 0x1234)
|
2018-03-14 01:43:24 +01:00
|
|
|
self.assertMatchSnapshot(mockbus._log)
|
2018-03-14 00:39:10 +01:00
|
|
|
|
|
|
|
def test_uva_light_intensity(self):
|
2020-08-07 23:01:51 +02:00
|
|
|
mockbus = setup_mockbus(initial_read={0x38 + 1: [0x01, 0x01], 0x38 + 0: [0x06, 0x06]})
|
2018-03-14 00:39:10 +01:00
|
|
|
veml = veml6070.Veml6070()
|
2020-08-07 23:01:51 +02:00
|
|
|
self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * (0.1 / 0.1125) * 0.05 / 1)
|
2018-03-14 00:39:10 +01:00
|
|
|
veml.set_integration_time(veml6070.INTEGRATIONTIME_4T)
|
2020-08-07 23:01:51 +02:00
|
|
|
self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * (0.1 / 0.1125) * 0.05 / 4)
|
2018-03-14 01:43:24 +01:00
|
|
|
self.assertMatchSnapshot(mockbus._log)
|
2019-09-18 22:30:42 +02:00
|
|
|
|
2019-09-19 01:36:31 +02:00
|
|
|
def test_get_estimated_risk_level(self):
|
2020-08-07 23:01:51 +02:00
|
|
|
mockbus = setup_mockbus(initial_read={0x38 + 1: [0x01, 0x04], 0x38 + 0: [0x06, 0x01]}) # pylint: disable=unused-variable
|
2019-09-19 01:36:31 +02:00
|
|
|
veml = veml6070.Veml6070()
|
|
|
|
intensity = veml.get_uva_light_intensity()
|
|
|
|
self.assertEqual(veml.get_estimated_risk_level(intensity), "low")
|
|
|
|
intensity = veml.get_uva_light_intensity()
|
|
|
|
self.assertEqual(veml.get_estimated_risk_level(intensity), "moderate")
|
2020-08-07 23:01:51 +02:00
|
|
|
# don't assert mockbus._log as we do not care
|
2019-09-19 01:36:31 +02:00
|
|
|
|
2019-09-18 22:30:42 +02:00
|
|
|
def test_get_refresh_time(self):
|
2020-08-07 23:01:51 +02:00
|
|
|
mockbus = setup_mockbus() # pylint: disable=unused-variable
|
2019-09-18 22:30:42 +02:00
|
|
|
veml = veml6070.Veml6070(rset=veml6070.RSET_240K)
|
|
|
|
self.assertEqual(veml.get_refresh_time(), 0.1)
|
|
|
|
veml.set_integration_time(veml6070.INTEGRATIONTIME_1_2T)
|
|
|
|
self.assertEqual(veml.get_refresh_time(), 0.05)
|
|
|
|
veml2 = veml6070.Veml6070(rset=veml6070.RSET_270K)
|
|
|
|
self.assertEqual(veml2.get_refresh_time(), 0.1125)
|
|
|
|
veml3 = veml6070.Veml6070(rset=veml6070.RSET_600K)
|
|
|
|
veml3.set_integration_time(veml6070.INTEGRATIONTIME_2T)
|
|
|
|
self.assertEqual(veml3.get_refresh_time(), 0.5)
|
2019-09-18 22:52:29 +02:00
|
|
|
veml4 = veml6070.Veml6070(rset=480000)
|
|
|
|
self.assertEqual(veml4.get_refresh_time(), 0.2)
|
2019-09-18 22:30:42 +02:00
|
|
|
# don't assert mockbus._log as we do not care
|