<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 <NAME> and contributors
# See LICENSE.rst for details.

"""
Tests for the :py:mod:`luma.core.serial` module.
"""

import errno

try:
    from unittest.mock import patch, call, Mock
except ImportError:
    from mock import patch, call, Mock

import pytest
import smbus2
from luma.core.serial import i2c, spi
import luma.core.error

smbus = Mock(unsafe=True)
spidev = Mock(unsafe=True)
gpio = Mock(unsafe=True)


def setup_function(function):
    smbus.reset_mock()
    spidev.reset_mock()
    gpio.reset_mock()
    gpio.BCM = 1
    gpio.RST = 2
    gpio.DC = 3
    gpio.OUT = 4
    gpio.HIGH = 5
    gpio.LOW = 6


def fib(n):
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b


# I2C

def test_i2c_init_device_not_found():
    port = 200
    address = 0x710
    with pytest.raises(luma.core.error.DeviceNotFoundError) as ex:
        i2c(port=port, address=address)
    assert str(ex.value) == 'I2C device not found: /dev/i2c-{}'.format(port)


def test_i2c_init_device_permission_error():
    with pytest.raises(luma.core.error.DevicePermissionError) as ex:
        i2c()
    assert str(ex.value) == 'I2C device permission denied: /dev/i2c-1'


def test_i2c_init_device_address_error():
    address = 'foo'
    with pytest.raises(luma.core.error.DeviceAddressError) as ex:
        i2c(address=address)
    assert str(ex.value) == 'I2C device address invalid: {}'.format(address)


def test_i2c_init_no_bus():
    with patch.object(smbus2.SMBus, 'open') as mock:
        i2c(port=2, address=0x71)
    mock.assert_called_once_with(2)


def test_i2c_init_bus_provided():
    i2c(bus=smbus, address=0x71)
    smbus.open.assert_not_called()


def test_i2c_command():
    cmds = [3, 1, 4, 2]
    serial = i2c(bus=smbus, address=0x83)
    serial.command(*cmds)
    smbus.write_i2c_block_data.assert_called_once_with(0x83, 0x00, cmds)


def test_i2c_command_device_not_found_error():
    errorbus = Mock(unsafe=True)
    address = 0x71
    cmds = [3, 1, 4, 2]
    expected_error = OSError()

    with patch.object(errorbus, 'write_i2c_block_data') as broken_command:
        for error_code in [errno.EREMOTEIO, errno.EIO]:
            expected_error.errno = error_code
            broken_command.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: {}'.format(
                address)


def test_i2c_data():
    data = list(fib(10))
    serial = i2c(bus=smbus, address=0x21)
    serial.data(data)
    smbus.write_i2c_block_data.assert_called_once_with(0x21, 0x40, data)


def test_i2c_data_chunked():
    data = list(fib(100))
    serial = i2c(bus=smbus, address=0x66)
    serial.data(data)
    calls = [call(0x66, 0x40, data[i:i + 32]) for i in range(0, 100, 32)]
    smbus.write_i2c_block_data.assert_has_calls(calls)


def test_i2c_cleanup():
    serial = i2c(bus=smbus, address=0x9F)
    serial.cleanup()
    smbus.close.assert_called_once_with()


# SPI

def verify_spi_init(port, device, bus_speed_hz=8000000, dc=24, rst=25):
    spidev.open.assert_called_once_with(port, device)
    assert spidev.max_speed_hz == bus_speed_hz
    gpio.setmode.assert_called_once_with(gpio.BCM)
    gpio.setup.assert_has_calls([call(dc, gpio.OUT), call(rst, gpio.OUT)])


def test_spi_init():
    spi(gpio=gpio, spi=spidev, port=5, device=2, bus_speed_hz=16000000, bcm_DC=17, bcm_RST=11)
    verify_spi_init(5, 2, 16000000, 17, 11)
    gpio.output.assert_has_calls([
        call(11, gpio.LOW),
        call(11, gpio.HIGH)
    ])


def test_spi_init_invalid_bus_speed():
    with pytest.raises(AssertionError):
        spi(gpio=gpio, spi=spidev, port=5, device=2, bus_speed_hz=942312)


def test_spi_command():
    cmds = [3, 1, 4, 2]
    serial = spi(gpio=gpio, spi=spidev, port=9, device=1)
    serial.command(*cmds)
    verify_spi_init(9, 1)
    gpio.output.assert_has_calls([call(25, gpio.HIGH), call(24, gpio.LOW)])
    spidev.writebytes.assert_called_once_with(cmds)


def test_spi_data():
    data = list(fib(100))
    serial = spi(gpio=gpio, spi=spidev, port=9, device=1)
    serial.data(data)
    verify_spi_init(9, 1)
    gpio.output.assert_has_calls([call(25, gpio.HIGH), call(24, gpio.HIGH)])
    spidev.writebytes.assert_called_once_with(data)


def test_spi_cleanup():
    serial = spi(gpio=gpio, spi=spidev, port=9, device=1)
    serial.cleanup()
    verify_spi_init(9, 1)
    spidev.close.assert_called_once_with()
    gpio.cleanup.assert_called_once_with()


def test_spi_init_device_not_found():
    import spidev
    with pytest.raises(luma.core.error.DeviceNotFoundError) as ex:
        spi(gpio=gpio, spi=spidev.SpiDev())
    assert str(ex.value) == 'SPI device not found'


def test_spi_unsupported_gpio_platform():
    with pytest.raises(luma.core.error.UnsupportedPlatform) as ex:
        spi(spi=spidev, port=9, device=1)
    assert str(ex.value) == 'GPIO access not available'<commit_msg>fix tests for rpi (#47)<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 <NAME> and contributors
# See LICENSE.rst for details.

"""
Tests for the :py:mod:`luma.core.serial` module.
"""

import errno

try:
    from unittest.mock import patch, call, Mock
except ImportError:
    from mock import patch, call, Mock

import pytest
import smbus2
from luma.core.serial import i2c, spi
import luma.core.error

smbus = Mock(unsafe=True)
spidev = Mock(unsafe=True)
gpio = Mock(unsafe=True)


def setup_function(function):
    smbus.reset_mock()
    spidev.reset_mock()
    gpio.reset_mock()
    gpio.BCM = 1
    gpio.RST = 2
    gpio.DC = 3
    gpio.OUT = 4
    gpio.HIGH = 5
    gpio.LOW = 6


def fib(n):
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b


# I2C

def test_i2c_init_device_not_found():
    port = 200
    address = 0x710
    with pytest.raises(luma.core.error.DeviceNotFoundError) as ex:
        i2c(port=port, address=address)
    assert str(ex.value) == 'I2C device not found: /dev/i2c-{}'.format(port)


def test_i2c_init_device_permission_error():
    port = 1
    try:
        i2c(port=port)

    except luma.core.error.DevicePermissionError as ex:
        # permission error: device exists but no permission
        assert str(ex) == 'I2C device permission denied: /dev/i2c-{}'.format(
            port)


def test_i2c_init_device_address_error():
    address = 'foo'
    with pytest.raises(luma.core.error.DeviceAddressError) as ex:
        i2c(address=address)
    assert str(ex.value) == 'I2C device address invalid: {}'.format(address)


def test_i2c_init_no_bus():
    with patch.object(smbus2.SMBus, 'open') as mock:
        i2c(port=2, address=0x71)
    mock.assert_called_once_with(2)


def test_i2c_init_bus_provided():
    i2c(bus=smbus, address=0x71)
    smbus.open.assert_not_called()


def test_i2c_command():
    cmds = [3, 1, 4, 2]
    serial = i2c(bus=smbus, address=0x83)
    serial.command(*cmds)
    smbus.write_i2c_block_data.assert_called_once_with(0x83, 0x00, cmds)


def test_i2c_command_device_not_found_error():
    errorbus = Mock(unsafe=True)
    address = 0x71
    cmds = [3, 1, 4, 2]
    expected_error = OSError()

    with patch.object(errorbus, 'write_i2c_block_data') as broken_command:
        for error_code in [errno.EREMOTEIO, errno.EIO]:
            expected_error.errno = error_code
            broken_command.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: {}'.format(
                address)


def test_i2c_data():
    data = list(fib(10))
    serial = i2c(bus=smbus, address=0x21)
    serial.data(data)
    smbus.write_i2c_block_data.assert_called_once_with(0x21, 0x40, data)


def test_i2c_data_chunked():
    data = list(fib(100))
    serial = i2c(bus=smbus, address=0x66)
    serial.data(data)
    calls = [call(0x66, 0x40, data[i:i + 32]) for i in range(0, 100, 32)]
    smbus.write_i2c_block_data.assert_has_calls(calls)


def test_i2c_cleanup():
    serial = i2c(bus=smbus, address=0x9F)
    serial.cleanup()
    smbus.close.assert_called_once_with()


# SPI

def verify_spi_init(port, device, bus_speed_hz=8000000, dc=24, rst=25):
    spidev.open.assert_called_once_with(port, device)
    assert spidev.max_speed_hz == bus_speed_hz
    gpio.setmode.assert_called_once_with(gpio.BCM)
    gpio.setup.assert_has_calls([call(dc, gpio.OUT), call(rst, gpio.OUT)])


def test_spi_init():
    spi(gpio=gpio, spi=spidev, port=5, device=2, bus_speed_hz=16000000, bcm_DC=17, bcm_RST=11)
    verify_spi_init(5, 2, 16000000, 17, 11)
    gpio.output.assert_has_calls([
        call(11, gpio.LOW),
        call(11, gpio.HIGH)
    ])


def test_spi_init_invalid_bus_speed():
    with pytest.raises(AssertionError):
        spi(gpio=gpio, spi=spidev, port=5, device=2, bus_speed_hz=942312)


def test_spi_command():
    cmds = [3, 1, 4, 2]
    serial = spi(gpio=gpio, spi=spidev, port=9, device=1)
    serial.command(*cmds)
    verify_spi_init(9, 1)
    gpio.output.assert_has_calls([call(25, gpio.HIGH), call(24, gpio.LOW)])
    spidev.writebytes.assert_called_once_with(cmds)


def test_spi_data():
    data = list(fib(100))
    serial = spi(gpio=gpio, spi=spidev, port=9, device=1)
    serial.data(data)
    verify_spi_init(9, 1)
    gpio.output.assert_has_calls([call(25, gpio.HIGH), call(24, gpio.HIGH)])
    spidev.writebytes.assert_called_once_with(data)


def test_spi_cleanup():
    serial = spi(gpio=gpio, spi=spidev, port=9, device=1)
    serial.cleanup()
    verify_spi_init(9, 1)
    spidev.close.assert_called_once_with()
    gpio.cleanup.assert_called_once_with()


def test_spi_init_device_not_found():
    import spidev
    port = 1234
    with pytest.raises(luma.core.error.DeviceNotFoundError) as ex:
        spi(gpio=gpio, spi=spidev.SpiDev(), port=port)
    assert str(ex.value) == 'SPI device not found'


def test_spi_unsupported_gpio_platform():
    try:
        spi(spi=spidev, port=9, device=1)
    except luma.core.error.UnsupportedPlatform as ex:
        assert str(ex) == 'GPIO access not available'
{'fraction_non_alphanumeric': 0.12433785996340171, 'fraction_numerical': 0.038331888664162574, 'mean_word_length': 3.584547461368653, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3473910', 'n_tokens_mistral': 4281, 'n_tokens_neox': 4024, 'n_words': 764}