From 3599889c8d15a9990067186dba07bb608e5ea908 Mon Sep 17 00:00:00 2001 From: Simon Albinsson Date: Thu, 12 Apr 2018 20:48:26 +0000 Subject: [PATCH] Test: adds support for testing i2c reads --- tests/mock_smbus2.py | 15 +++++++++++++-- tests/test_sgp.py | 11 ++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/mock_smbus2.py b/tests/mock_smbus2.py index 151000a..6df03e3 100644 --- a/tests/mock_smbus2.py +++ b/tests/mock_smbus2.py @@ -14,11 +14,22 @@ answers = { (0x20, 0x15): add_crc([0x00,0x00])*2 ,#GET_BASELINE Invalid, should be more realistic data.. (0x20, 0x1e): None,#SET_BASELINE } + class MockSMBus: def __init__(s): s.status=None def i2c_rdwr(s,*msgs): - print(i2c_msg.read) - + for m in msgs: + if m.flags == 1: + s._process_read(m) + else: + s._process_write(m) + def _process_read(s,msg): + if s.status == None: + raise AssertionError("tired to read before write") + for i in range(len(s.status)): + msg.buf[i]=chr(s.status[i]) + def _process_write(s,msg): + s.status = answers[tuple(msg)] diff --git a/tests/test_sgp.py b/tests/test_sgp.py index d58d945..66f382f 100644 --- a/tests/test_sgp.py +++ b/tests/test_sgp.py @@ -2,10 +2,19 @@ from .context import sgp30 from mock_smbus2 import MockSMBus import unittest -class BasicTestSuite(unittest.TestCase): + +class TestConstructor(unittest.TestCase): """Basic test cases.""" def setUp(self): self.bus = MockSMBus() def test_init(self): b=sgp30.sgp30.Sgp30(self.bus) b.init_sgp() + +class SimpleTest(unittest.TestCase): + """Basic test cases.""" + def setUp(self): + self.bus = MockSMBus() + self.sgp=sgp30.sgp30.Sgp30(self.bus) + def test_read(self): + self.sgp.read_measurements()