1+ import sys
2+
3+ import mock
4+ import pytest
5+
6+
7+ # MockSMbus borrowed from https://github.com/pimoroni/i2cdevice-python
8+ class MockSMBus :
9+ def __init__ (self , i2c_bus , default_registers = None ):
10+ self .regs = [0 for _ in range (255 )]
11+ if default_registers is not None :
12+ for index in default_registers .keys ():
13+ self .regs [index ] = default_registers .get (index )
14+
15+ def write_i2c_block_data (self , i2c_address , register , values ):
16+ self .regs [register :register + len (values )] = values
17+
18+ def read_i2c_block_data (self , i2c_address , register , length ):
19+ return self .regs [register :register + length ]
20+
21+ def read_byte_data (self , i2c_address , register ):
22+ return self .read_i2c_block_data (i2c_address , register , 1 )[0 ]
23+
24+ def write_byte_data (self , i2c_address , register , value ):
25+ self .write_i2c_block_data (i2c_address , register , [value ])
26+
27+
28+ @pytest .fixture (scope = "function" , autouse = True )
29+ def cleanup ():
30+ yield
31+ del sys .modules ["cap1xxx" ]
32+
33+
34+ @pytest .fixture (scope = "function" )
35+ def smbus2 ():
36+ sys .modules ["smbus2" ] = mock .MagicMock ()
37+ yield sys .modules ["smbus2" ]
38+ del sys .modules ["smbus2" ]
39+
40+
41+ @pytest .fixture (scope = "function" )
42+ def gpio ():
43+ sys .modules ["RPi" ] = mock .Mock ()
44+ sys .modules ["RPi.GPIO" ] = mock .MagicMock ()
45+ yield
46+ del sys .modules ["RPi" ]
47+ del sys .modules ["RPi.GPIO" ]
0 commit comments