From 2086f111f2031aa16a9fe6984566595d27bbe3ae Mon Sep 17 00:00:00 2001 From: cn Date: Wed, 14 Mar 2018 00:39:10 +0100 Subject: [PATCH] module: add pytest for unit and snapshot testing with travis-ci --- .gitignore | 3 + .travis.yml | 8 ++ README.md | 3 +- pytest.ini | 2 + requirements.txt | 4 + tests/__init__.py | 0 tests/snapshots/__init__.py | 0 tests/snapshots/snap_test_veml6070.py | 128 ++++++++++++++++++++++++++ tests/test_veml6070.py | 97 +++++++++++++++++++ 9 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 .travis.yml create mode 100644 pytest.ini create mode 100644 requirements.txt create mode 100644 tests/__init__.py create mode 100644 tests/snapshots/__init__.py create mode 100644 tests/snapshots/snap_test_veml6070.py create mode 100644 tests/test_veml6070.py diff --git a/.gitignore b/.gitignore index 0d20b64..47b1686 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ +.cache/ +env/ +__pycache__/ *.pyc diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..dac121d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +--- +sudo: false +language: python +python: + - "2.7" + +script: + - pytest diff --git a/README.md b/README.md index 4330f35..1558bf0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ - # python-veml6070 +[![Build Status](https://travis-ci.org/cmur2/python-veml6070.svg?branch=master)](https://travis-ci.org/cmur2/python-veml6070) + A Python library for accessing the [VEML6070 digital UV light sensor](http://www.vishay.com/docs/84277/veml6070.pdf) from Vishay via `python-smbus` using the I2C interface. Default settings are suitable for Raspberry Pi 2 and 3 and was successfully tested using a [breakout](https://github.com/watterott/VEML6070-Breakout). diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..3fdfe86 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +norecursedirs = env* .* diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..cecf9ea --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +mock==2.0.0 +pylint==1.8.2 +pytest==3.0.6 +snapshottest==0.5.0 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/snapshots/__init__.py b/tests/snapshots/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/snapshots/snap_test_veml6070.py b/tests/snapshots/snap_test_veml6070.py new file mode 100644 index 0000000..a09594c --- /dev/null +++ b/tests/snapshots/snap_test_veml6070.py @@ -0,0 +1,128 @@ +# -*- coding: utf-8 -*- +# snapshottest: v1 - https://goo.gl/zC4yUc +from __future__ import unicode_literals + +from snapshottest import Snapshot + + +snapshots = Snapshot() + +snapshots['TestVeml6070::test_disable 1'] = { + 'readlog': [ + ], + 'writelog': [ + ( + '56', + [ + 6, + 6, + 6 + ] + ) + ] +} + +snapshots['TestVeml6070::test_enable 1'] = { + 'readlog': [ + ], + 'writelog': [ + ( + '56', + [ + 6, + 6, + 6 + ] + ) + ] +} + +snapshots['TestVeml6070::test_integration_time 1'] = { + 'readlog': [ + ], + 'writelog': [ + ( + '56', + [ + 2, + 2, + 14 + ] + ) + ] +} + +snapshots['TestVeml6070::test_setup 1'] = { + 'readlog': [ + ], + 'writelog': [ + ( + '56', + [ + 6, + 6 + ] + ) + ] +} + +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_uva_light_intensity_raw 1'] = { + 'readlog': [ + ( + '56', + [ + 52 + ] + ), + ( + '57', + [ + 18 + ] + ) + ], + 'writelog': [ + ( + '56', + [ + 6, + 6, + 6, + 6 + ] + ) + ] +} diff --git a/tests/test_veml6070.py b/tests/test_veml6070.py new file mode 100644 index 0000000..724a51e --- /dev/null +++ b/tests/test_veml6070.py @@ -0,0 +1,97 @@ + +# 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 +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.initial_read = initial_read or {} + + def read_byte(self, addr): + val = self.initial_read.get(addr).pop(0) + self._readlog.setdefault(addr, []).append(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()] + } + +# 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) + +def setup_mockbus(**kwargs): + mockbus = MockSMBus(**kwargs) + MOCKED_SMBUS_MODULE.SMBus.return_value = mockbus + return mockbus + +class TestVeml6070(snapshottest.TestCase): + + def test_setup(self): + mockbus = setup_mockbus() + veml = veml6070.Veml6070() + self.assertIsNotNone(veml) + self.assertMatchSnapshot(mockbus._get_log()) + + 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) + self.assertMatchSnapshot(mockbus._get_log()) + + def test_enable(self): + mockbus = setup_mockbus() + veml = veml6070.Veml6070() + veml.enable() + self.assertMatchSnapshot(mockbus._get_log()) + + def test_disable(self): + mockbus = setup_mockbus() + veml = veml6070.Veml6070() + veml.disable() + self.assertMatchSnapshot(mockbus._get_log()) + + def test_uva_light_intensity_raw(self): + mockbus = setup_mockbus(initial_read={ + 0x38+1: [0x12], + 0x38+0: [0x34] + }) + veml = veml6070.Veml6070() + self.assertEqual(veml.get_uva_light_intensity_raw(), 0x1234) + self.assertMatchSnapshot(mockbus._get_log()) + + def test_uva_light_intensity(self): + mockbus = setup_mockbus(initial_read={ + 0x38+1: [0x01, 0x01], + 0x38+0: [0x06, 0x06] + }) + veml = veml6070.Veml6070() + 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())