def setup_function(function): ws.reset_mock() ws.command.side_effect = None ws.ws2811_init = Mock(return_value=0) ws.ws2811_render = Mock(return_value=0) ws.ws2811_channel_get = Mock(return_value=chan) ws.ws2811_new_ws2811_t = Mock(return_value=leds)
def test_display(): """ SH1106 OLED screen can draw and display an image. """ device = sh1106(serial) serial.reset_mock() recordings = [] def data(data): recordings.append({'data': data}) def command(*cmd): recordings.append({'command': list(cmd)}) serial.command = Mock(side_effect=command, unsafe=True) serial.data = Mock(side_effect=data, unsafe=True) # Use the same drawing primitives as the demo with canvas(device) as draw: primitives(device, draw) serial.data.assert_called() serial.command.assert_called() assert recordings == get_json_data('demo_sh1106')
def test_display(): """ UC1701X LCD screen can draw and display an image. """ device = uc1701x(serial, gpio=Mock()) serial.reset_mock() recordings = [] def data(data): recordings.append({'data': data}) def command(*cmd): recordings.append({'command': list(cmd)}) serial.command = Mock(side_effect=command, unsafe=True) serial.data = Mock(side_effect=data, unsafe=True) # Use the same drawing primitives as the demo with canvas(device) as draw: primitives(device, draw) serial.data.assert_called() serial.command.assert_called() assert recordings == get_reference_data('demo_uc1701x')
def test_cleanup(mock_controller): port = Mock() instance = Mock() instance.get_port = Mock(return_value=port) mock_controller.side_effect = [instance] serial = ftdi_i2c(device='ftdi://dummy', address=0x3C) serial.cleanup() instance.terminate.assert_called_once_with()
def test_init(mock_controller): instance = Mock() instance.get_port = Mock() mock_controller.side_effect = [instance] ftdi_i2c(device='ftdi://dummy', address='0xFF') mock_controller.assert_called_with() instance.configure.assert_called_with('ftdi://dummy') instance.get_port.assert_called_with(0xFF)
def test_command(mock_controller): cmds = [3, 1, 4, 2] port = Mock() instance = Mock() instance.get_port = Mock(return_value=port) mock_controller.side_effect = [instance] serial = ftdi_i2c(device='ftdi://dummy', address=0x3C) serial.command(*cmds) port.write_to.assert_called_once_with(0x00, cmds)
def test_data(mock_controller): data = list(fib(100)) port = Mock() instance = Mock() instance.get_port = Mock(return_value=port) mock_controller.side_effect = [instance] serial = ftdi_i2c(device='ftdi://dummy', address=0x3C) serial.data(data) port.write_to.assert_called_once_with(0x40, data)
def test_get_library_version(): """ :py:func:`luma.core.cmdline.get_library_version` returns the version number for the specified library name. """ lib_name = 'hotscreenz' lib_version = '0.1.2' # set version nr for fake luma library luma_fake_lib = Mock() luma_fake_lib.__version__ = lib_version with patch.dict('sys.modules', {'luma.' + lib_name: luma_fake_lib}): assert cmdline.get_library_version(lib_name) == lib_version
def test_text_space(): """ Draw a space character. """ draw = Mock(unsafe=True) text(draw, (2, 2), " ", fill="white") draw.point.assert_not_called()
def test_display(): device = st7735(serial, gpio=Mock()) serial.reset_mock() recordings = [] def data(data): recordings.append({'data': data}) def command(*cmd): recordings.append({'command': list(cmd)}) serial.command.side_effect = command serial.data.side_effect = data # Use the same drawing primitives as the demo with canvas(device) as draw: primitives(device, draw) assert serial.data.called assert serial.command.called assert recordings == [{ 'command': [42] }, { 'data': [0, 0, 0, 159] }, { 'command': [43] }, { 'data': [0, 0, 0, 127] }, { 'command': [44] }, { 'data': get_reference_data('demo_st7735') }]
def test_create_device_oled(): """ :py:func:`luma.core.cmdline.create_device` supports OLED displays. """ display_name = 'oled1234' display_types = {'oled': [display_name]} class args(test_spi_opts): display = display_name module_mock = Mock() module_mock.oled.device.oled1234.return_value = display_name with patch.dict( 'sys.modules', **{ # mock luma.oled package 'luma': module_mock, 'luma.oled': module_mock, 'luma.oled.device': module_mock }): try: device = cmdline.create_device(args, display_types=display_types) assert device == display_name except ImportError: pytest.skip(rpi_gpio_missing) except error.UnsupportedPlatform as e: # non-rpi platform pytest.skip('{0} ({1})'.format(type(e).__name__, str(e)))
def test_create_device_lcd(): """ :py:func:`luma.core.cmdline.create_device` supports LCD displays. """ display_name = 'lcd1234' display_types = {'lcd': [display_name], 'oled': []} class args(test_spi_opts): display = display_name gpio = 'fake_gpio' backlight_active = 'low' module_mock = Mock() module_mock.lcd.device.lcd1234.return_value = display_name with patch.dict( 'sys.modules', **{ # mock spidev and luma.lcd packages 'fake_gpio': module_mock, 'spidev': module_mock, 'luma': module_mock, 'luma.lcd': module_mock, 'luma.lcd.aux': module_mock, 'luma.lcd.device': module_mock }): device = cmdline.create_device(args, display_types=display_types) assert device == display_name
def test_create_device_emulator(): """ :py:func:`luma.core.cmdline.create_device` supports emulators. """ display_name = 'emulator1234' display_types = { 'emulator': [display_name], 'led_matrix': [], 'lcd': [], 'oled': [] } class args(test_spi_opts): display = display_name module_mock = Mock() module_mock.emulator.device.emulator1234.return_value = display_name with patch.dict( 'sys.modules', **{ # mock spidev and luma.emulator packages 'spidev': module_mock, 'luma': module_mock, 'luma.emulator': module_mock, 'luma.emulator.device': module_mock }): device = cmdline.create_device(args, display_types=display_types) assert device == display_name
def test_pwm_turn_on(): gpio_LIGHT = 18 pwm_mock = Mock() gpio.PWM.return_value = pwm_mock device = backlit_device(serial_interface=noop(), gpio=gpio, gpio_LIGHT=gpio_LIGHT, pwm_frequency=100) gpio.PWM.assert_called_once_with(gpio_LIGHT, 100) gpio.reset_mock() device.backlight(True) pwm_mock.ChangeDutyCycle.assert_called_once_with(100.0)
def test_create_parser(): """ :py:func:`luma.core.cmdline.create_parser` returns an argument parser instance. """ with patch.dict('sys.modules', **{ 'luma.emulator': Mock(), 'luma.emulator.render': Mock(), }): with patch('luma.core.cmdline.get_display_types') as mocka: mocka.return_value = { 'foo': ['a', 'b'], 'bar': ['c', 'd'], 'emulator': ['e', 'f'] } parser = cmdline.create_parser(description='test') args = parser.parse_args(['-f', test_config_file]) assert args.config == test_config_file
def test_unsupported_platform(): e = RuntimeError('Module not imported correctly!') errorgpio = Mock(unsafe=True) errorgpio.setmode.side_effect = e try: backlight(gpio_LIGHT=19, gpio=errorgpio) except luma.core.error.UnsupportedPlatform as ex: assert str(ex) == 'GPIO access not available'
def test_init_84x48(): pcd8544(serial, gpio=Mock()) serial.command.assert_has_calls( [call(33, 20, 176, 32), call(32, 128, 64), call(12)]) # Next 1024 are all data: zero's to clear the RAM # (1024 = 128 * 64 / 8) serial.data.assert_called_once_with([0] * (84 * 48 // 8))
def test_display_fail(): device = neopixel(ws, cascaded=7) ws.reset_mock() ws.ws2811_render = Mock(return_value=-1) with pytest.raises(RuntimeError) as ex: with canvas(device) as draw: draw.rectangle(device.bounding_box, outline="red") assert "ws2811_render failed with code -1" in str(ex.value)
def test_unsupported_platform(): e = RuntimeError('Module not imported correctly!') errorgpio = Mock(unsafe=True) errorgpio.setup.side_effect = e try: backlit_device(serial_interface=noop(), gpio_LIGHT=19, gpio=errorgpio) except luma.core.error.UnsupportedPlatform as ex: assert str(ex) == 'GPIO access not available' else: pytest.fail("Didn't raise exception")
def test_make_serial_spi_alt_gpio(): """ :py:func:`luma.core.cmdline.make_serial.spi` returns an SPI instance when using an alternative GPIO implementation. """ class opts(test_spi_opts): gpio = 'fake_gpio' with patch.dict('sys.modules', **{'fake_gpio': Mock(unsafe=True)}): factory = cmdline.make_serial(opts) with pytest.raises(error.DeviceNotFoundError): factory.spi()
def test_display(): device = pcd8544(serial, gpio=Mock()) serial.reset_mock() # Use the same drawing primitives as the demo with canvas(device) as draw: primitives(device, draw) # Initial command to reset the display serial.command.assert_called_once_with(32, 128, 64) # Next 1024 bytes are data representing the drawn image serial.data.assert_called_once_with(get_reference_data('demo_pcd8544'))
def test_cleanup(mock_controller): gpio = Mock() port = Mock() instance = Mock() instance.get_port = Mock(return_value=port) instance.get_gpio = Mock(return_value=gpio) mock_controller.side_effect = [instance] serial = ftdi_spi(device='ftdi://dummy', bus_speed_hz=16000000, gpio_CS=3, gpio_DC=5, gpio_RST=6) serial.cleanup() instance.terminate.assert_called_once_with()
def test_data(mock_controller): data = list(fib(100)) gpio = Mock() port = Mock() instance = Mock() instance.get_port = Mock(return_value=port) instance.get_gpio = Mock(return_value=gpio) mock_controller.side_effect = [instance] serial = ftdi_spi(device='ftdi://dummy', bus_speed_hz=16000000, gpio_CS=3, gpio_DC=5, gpio_RST=6) serial.data(data) gpio.write.assert_has_calls([call(0x00), call(0x40), call(0x60)]) port.write.assert_called_once_with(data)
def test_command(mock_controller): cmds = [3, 1, 4, 2] gpio = Mock() port = Mock() instance = Mock() instance.get_port = Mock(return_value=port) instance.get_gpio = Mock(return_value=gpio) mock_controller.side_effect = [instance] serial = ftdi_spi(device='ftdi://dummy', bus_speed_hz=16000000, gpio_CS=3, gpio_DC=5, gpio_RST=6) serial.command(*cmds) gpio.write.assert_has_calls([call(0x00), call(0x40), call(0x40)]) port.write.assert_called_once_with(cmds)
def test_mock(): mock = Mock( {"name": "I'm a Mock", "child": Mock({"name": "My parent is a Mock"})}, ["name"] ) assert mock.name == "I'm a Mock" assert mock.rank() == "rank" assert isinstance(mock.child(), Mock) assert mock.child().name() == "My parent is a Mock"
def test_i2c_command_device_not_found_error(): errorbus = Mock(unsafe=True) address = 0x71 cmds = [3, 1, 4, 2] expected_error = OSError() for error_code in [errno.EREMOTEIO, errno.EIO]: expected_error.errno = error_code errorbus.write_i2c_block_data.side_effect = expected_error serial = i2c(bus=errorbus, address=address) with pytest.raises(luma.core.error.DeviceNotFoundError) as ex: serial.command(*cmds) assert str(ex.value) == 'I2C device not found on address: 0x{0:02X}'.format( address)
def test_init_128x64(): st7567(serial, gpio=Mock()) serial.command.assert_has_calls([ call(0xA3), call(0xA1), call(0xC0), call(0xA6), call(0x40), call(0x2F), call(0x22), call(0xAF), call(0x81, 57) ]) # Next 1024 are all data: zeros to clear the RAM # (1024 = 128 * 64 / 8) serial.data.assert_has_calls([call([0] * 128)] * 8)
def test_make_serial_spi_alt_gpio(): """ :py:func:`luma.core.cmdline.make_serial.spi` returns an SPI instance when using an alternative GPIO implementation. """ class opts(test_spi_opts): gpio = 'fake_gpio' with patch.dict('sys.modules', **{'fake_gpio': Mock(unsafe=True)}): try: factory = cmdline.make_serial(opts) assert 'luma.core.interface.serial.spi' in repr(factory.spi()) except ImportError: pytest.skip(spidev_missing) except error.DeviceNotFoundError as e: # non-rpi platform, e.g. ubuntu 64-bit pytest.skip('{0} ({1})'.format(type(e).__name__, str(e)))
def test_init(mock_controller): gpio = Mock() instance = Mock() instance.get_port = Mock() instance.get_gpio = Mock(return_value=gpio) mock_controller.side_effect = [instance] ftdi_spi(device='ftdi://dummy', bus_speed_hz=16000000, gpio_CS=3, gpio_DC=5, gpio_RST=6) mock_controller.assert_called_with(cs_count=1) instance.configure.assert_called_with('ftdi://dummy') instance.get_port.assert_called_with(cs=0, freq=16000000, mode=0) instance.get_gpio.assert_called_with() gpio.set_direction.assert_called_with(0x60, 0x60)
def test_text_char(): """ Draw a text character. """ draw = Mock(unsafe=True) text(draw, (2, 2), "L", font=LCD_FONT, fill="white") draw.point.assert_has_calls([ call((2, 2), fill='white'), call((2, 3), fill='white'), call((2, 4), fill='white'), call((2, 5), fill='white'), call((2, 6), fill='white'), call((2, 7), fill='white'), call((2, 8), fill='white'), call((3, 8), fill='white'), call((4, 8), fill='white'), call((5, 8), fill='white'), call((6, 8), fill='white') ])