| 1 | """Test configuration.\r |
| 2 | These allow the mocking of various Python modules\r |
| 3 | that might otherwise have runtime side-effects.\r |
| 4 | """\r |
| 5 | import sys\r |
| 6 | import mock\r |
| 7 | import pytest\r |
| 8 | from i2cdevice import MockSMBus\r |
| 9 | \r |
| 10 | \r |
| 11 | class SMBusFakeDevice(MockSMBus):\r |
| 12 | def __init__(self, i2c_bus):\r |
| 13 | MockSMBus.__init__(self, i2c_bus)\r |
| 14 | self.regs[0x00:0x01] = 0x0f, 0x00\r |
| 15 | \r |
| 16 | \r |
| 17 | @pytest.fixture(scope='function', autouse=False)\r |
| 18 | def GPIO():\r |
| 19 | """Mock RPi.GPIO module."""\r |
| 20 | GPIO = mock.MagicMock()\r |
| 21 | # Fudge for Python < 37 (possibly earlier)\r |
| 22 | sys.modules['RPi'] = mock.Mock()\r |
| 23 | sys.modules['RPi'].GPIO = GPIO\r |
| 24 | sys.modules['RPi.GPIO'] = GPIO\r |
| 25 | yield GPIO\r |
| 26 | del sys.modules['RPi']\r |
| 27 | del sys.modules['RPi.GPIO']\r |
| 28 | \r |
| 29 | \r |
| 30 | @pytest.fixture(scope='function', autouse=False)\r |
| 31 | def spidev():\r |
| 32 | """Mock spidev module."""\r |
| 33 | spidev = mock.MagicMock()\r |
| 34 | sys.modules['spidev'] = spidev\r |
| 35 | yield spidev\r |
| 36 | del sys.modules['spidev']\r |
| 37 | \r |
| 38 | \r |
| 39 | @pytest.fixture(scope='function', autouse=False)\r |
| 40 | def smbus():\r |
| 41 | """Mock smbus module."""\r |
| 42 | smbus = mock.MagicMock()\r |
| 43 | smbus.SMBus = SMBusFakeDevice\r |
| 44 | sys.modules['smbus'] = smbus\r |
| 45 | yield smbus\r |
| 46 | del sys.modules['smbus']\r |
| 47 | \r |
| 48 | \r |
| 49 | @pytest.fixture(scope='function', autouse=False)\r |
| 50 | def atexit():\r |
| 51 | """Mock atexit module."""\r |
| 52 | atexit = mock.MagicMock()\r |
| 53 | sys.modules['atexit'] = atexit\r |
| 54 | yield atexit\r |
| 55 | del sys.modules['atexit']\r |
| 56 | \r |
| 57 | \r |
| 58 | @pytest.fixture(scope='function', autouse=False)\r |
| 59 | def sounddevice():\r |
| 60 | """Mock sounddevice module."""\r |
| 61 | sounddevice = mock.MagicMock()\r |
| 62 | sys.modules['sounddevice'] = sounddevice\r |
| 63 | yield sounddevice\r |
| 64 | del sys.modules['sounddevice']\r |
| 65 | \r |
| 66 | \r |
| 67 | @pytest.fixture(scope='function', autouse=False)\r |
| 68 | def numpy():\r |
| 69 | """Mock numpy module."""\r |
| 70 | numpy = mock.MagicMock()\r |
| 71 | sys.modules['numpy'] = numpy\r |
| 72 | yield numpy\r |
| 73 | del sys.modules['numpy']\r |