From d2a7c1434adb95d253e59ea5f25832151f828257 Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Wed, 21 Jun 2017 14:40:52 +0100 Subject: [PATCH] Fix SPI tests when spidev is installed Doh! SPI tests fail when the spidev package is installed (which it normally isn't in my virtualenv) because hardware SPI always takes precedence and the mock SPI stuff only replaces software SPI. --- tests/test_spi.py | 17 +++-- tests/test_spi_devices.py | 142 +++++++++++++++++++++----------------- 2 files changed, 89 insertions(+), 70 deletions(-) diff --git a/tests/test_spi.py b/tests/test_spi.py index 5159109..7b514bf 100644 --- a/tests/test_spi.py +++ b/tests/test_spi.py @@ -70,7 +70,8 @@ def test_spi_software_params(): assert isinstance(device, LocalPiSoftwareSPI) with Device._pin_factory.spi(select_pin=6, shared=True) as device: assert isinstance(device, LocalPiSoftwareSPIShared) - with patch('gpiozero.devices.Device._pin_factory', NativeFactory()): + with patch('gpiozero.devices.Device._pin_factory', NativeFactory()), \ + patch('gpiozero.pins.local.SpiDev', None): # Clear out the old factory's pins cache (this is only necessary # because we're being very naughty switching out pin factories) Device._pin_factory.pins.clear() @@ -136,7 +137,9 @@ def test_spi_software_read(): super(SPISlave, self).on_start() for i in range(10): self.tx_word(i) - with SPISlave(11, 10, 9, 8) as slave, Device._pin_factory.spi() as master: + with patch('gpiozero.pins.local.SpiDev', None), \ + SPISlave(11, 10, 9, 8) as slave, \ + Device._pin_factory.spi() as master: assert master.read(3) == [0, 1, 2] assert master.read(6) == [0, 1, 2, 3, 4, 5] slave.clock_phase = True @@ -145,7 +148,9 @@ def test_spi_software_read(): assert master.read(6) == [0, 1, 2, 3, 4, 5] def test_spi_software_write(): - with MockSPIDevice(11, 10, 9, 8) as test_device, Device._pin_factory.spi() as master: + with patch('gpiozero.pins.local.SpiDev', None), \ + MockSPIDevice(11, 10, 9, 8) as test_device, \ + Device._pin_factory.spi() as master: master.write([0]) assert test_device.rx_word() == 0 master.write([2, 0]) @@ -154,7 +159,8 @@ def test_spi_software_write(): assert test_device.rx_word() == 257 def test_spi_software_clock_mode(): - with Device._pin_factory.spi() as master: + with patch('gpiozero.pins.local.SpiDev', None), \ + Device._pin_factory.spi() as master: assert master.clock_mode == 0 assert not master.clock_polarity assert not master.clock_phase @@ -171,7 +177,8 @@ def test_spi_software_clock_mode(): master.clock_mode = 5 def test_spi_software_attr(): - with Device._pin_factory.spi() as master: + with patch('gpiozero.pins.local.SpiDev', None), \ + Device._pin_factory.spi() as master: assert not master.lsb_first assert not master.select_high assert master.bits_per_word == 8 diff --git a/tests/test_spi_devices.py b/tests/test_spi_devices.py index 6c876ad..4f45171 100644 --- a/tests/test_spi_devices.py +++ b/tests/test_spi_devices.py @@ -9,6 +9,7 @@ str = type('') import sys import pytest +from mock import patch from collections import namedtuple try: from math import isclose @@ -247,89 +248,100 @@ def differential_mcp_test(mock, pot, pos_channel, neg_channel, bits, full=False) def test_MCP3001(): - mock = MockMCP3001(11, 10, 9, 8) - with MCP3001() as pot: - differential_mcp_test(mock, pot, 0, 1, 10) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3001(11, 10, 9, 8) + with MCP3001() as pot: + differential_mcp_test(mock, pot, 0, 1, 10) def test_MCP3002(): - mock = MockMCP3002(11, 10, 9, 8) - with pytest.raises(ValueError): - MCP3002(channel=5) - with MCP3002(channel=1) as pot: - single_mcp_test(mock, pot, 1, 10) - with MCP3002(channel=1, differential=True) as pot: - differential_mcp_test(mock, pot, 1, 0, 10) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3002(11, 10, 9, 8) + with pytest.raises(ValueError): + MCP3002(channel=5) + with MCP3002(channel=1) as pot: + single_mcp_test(mock, pot, 1, 10) + with MCP3002(channel=1, differential=True) as pot: + differential_mcp_test(mock, pot, 1, 0, 10) def test_MCP3004(): - mock = MockMCP3004(11, 10, 9, 8) - with pytest.raises(ValueError): - MCP3004(channel=5) - with MCP3004(channel=3) as pot: - single_mcp_test(mock, pot, 3, 10) - with MCP3004(channel=3, differential=True) as pot: - differential_mcp_test(mock, pot, 3, 2, 10) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3004(11, 10, 9, 8) + with pytest.raises(ValueError): + MCP3004(channel=5) + with MCP3004(channel=3) as pot: + single_mcp_test(mock, pot, 3, 10) + with MCP3004(channel=3, differential=True) as pot: + differential_mcp_test(mock, pot, 3, 2, 10) def test_MCP3008(): - mock = MockMCP3008(11, 10, 9, 8) - with pytest.raises(ValueError): - MCP3008(channel=9) - with MCP3008(channel=0) as pot: - single_mcp_test(mock, pot, 0, 10) - with MCP3008(channel=0, differential=True) as pot: - differential_mcp_test(mock, pot, 0, 1, 10) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3008(11, 10, 9, 8) + with pytest.raises(ValueError): + MCP3008(channel=9) + with MCP3008(channel=0) as pot: + single_mcp_test(mock, pot, 0, 10) + with MCP3008(channel=0, differential=True) as pot: + differential_mcp_test(mock, pot, 0, 1, 10) def test_MCP3201(): - mock = MockMCP3201(11, 10, 9, 8) - with MCP3201() as pot: - differential_mcp_test(mock, pot, 0, 1, 12) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3201(11, 10, 9, 8) + with MCP3201() as pot: + differential_mcp_test(mock, pot, 0, 1, 12) def test_MCP3202(): - mock = MockMCP3202(11, 10, 9, 8) - with pytest.raises(ValueError): - MCP3202(channel=5) - with MCP3202(channel=1) as pot: - single_mcp_test(mock, pot, 1, 12) - with MCP3202(channel=1, differential=True) as pot: - differential_mcp_test(mock, pot, 1, 0, 12) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3202(11, 10, 9, 8) + with pytest.raises(ValueError): + MCP3202(channel=5) + with MCP3202(channel=1) as pot: + single_mcp_test(mock, pot, 1, 12) + with MCP3202(channel=1, differential=True) as pot: + differential_mcp_test(mock, pot, 1, 0, 12) def test_MCP3204(): - mock = MockMCP3204(11, 10, 9, 8) - with pytest.raises(ValueError): - MCP3204(channel=5) - with MCP3204(channel=1) as pot: - single_mcp_test(mock, pot, 1, 12) - with MCP3204(channel=1, differential=True) as pot: - differential_mcp_test(mock, pot, 1, 0, 12) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3204(11, 10, 9, 8) + with pytest.raises(ValueError): + MCP3204(channel=5) + with MCP3204(channel=1) as pot: + single_mcp_test(mock, pot, 1, 12) + with MCP3204(channel=1, differential=True) as pot: + differential_mcp_test(mock, pot, 1, 0, 12) def test_MCP3208(): - mock = MockMCP3208(11, 10, 9, 8) - with pytest.raises(ValueError): - MCP3208(channel=9) - with MCP3208(channel=7) as pot: - single_mcp_test(mock, pot, 7, 12) - with MCP3208(channel=7, differential=True) as pot: - differential_mcp_test(mock, pot, 7, 6, 12) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3208(11, 10, 9, 8) + with pytest.raises(ValueError): + MCP3208(channel=9) + with MCP3208(channel=7) as pot: + single_mcp_test(mock, pot, 7, 12) + with MCP3208(channel=7, differential=True) as pot: + differential_mcp_test(mock, pot, 7, 6, 12) def test_MCP3301(): - mock = MockMCP3301(11, 10, 9, 8) - with MCP3301() as pot: - differential_mcp_test(mock, pot, 0, 1, 12, full=True) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3301(11, 10, 9, 8) + with MCP3301() as pot: + differential_mcp_test(mock, pot, 0, 1, 12, full=True) def test_MCP3302(): - mock = MockMCP3302(11, 10, 9, 8) - with pytest.raises(ValueError): - MCP3302(channel=4) - with MCP3302(channel=0) as pot: - single_mcp_test(mock, pot, 0, 12) - with MCP3302(channel=0, differential=True) as pot: - differential_mcp_test(mock, pot, 0, 1, 12, full=True) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3302(11, 10, 9, 8) + with pytest.raises(ValueError): + MCP3302(channel=4) + with MCP3302(channel=0) as pot: + single_mcp_test(mock, pot, 0, 12) + with MCP3302(channel=0, differential=True) as pot: + differential_mcp_test(mock, pot, 0, 1, 12, full=True) def test_MCP3304(): - mock = MockMCP3304(11, 10, 9, 8) - with pytest.raises(ValueError): - MCP3304(channel=9) - with MCP3304(channel=5) as pot: - single_mcp_test(mock, pot, 5, 12) - with MCP3304(channel=5, differential=True) as pot: - differential_mcp_test(mock, pot, 5, 4, 12, full=True) + with patch('gpiozero.pins.local.SpiDev', None): + mock = MockMCP3304(11, 10, 9, 8) + with pytest.raises(ValueError): + MCP3304(channel=9) + with MCP3304(channel=5) as pot: + single_mcp_test(mock, pot, 5, 12) + with MCP3304(channel=5, differential=True) as pot: + differential_mcp_test(mock, pot, 5, 4, 12, full=True)