[go: up one dir, main page]

File: test_ftdi_i2c.py

package info (click to toggle)
luma.core 2.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,040 kB
  • sloc: python: 6,186; makefile: 204
file content (72 lines) | stat: -rw-r--r-- 2,095 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2019-2020 Richard Hull and contributors
# See LICENSE.rst for details.

"""
Tests for the :py:class:`luma.core.interface.serial.ftdi_i2c` class.
"""

import pytest
from unittest.mock import Mock, patch
from luma.core.interface.serial import ftdi_i2c
from helpers import fib
import luma.core.error


@patch('pyftdi.i2c.I2cController')
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)


@patch('pyftdi.i2c.I2cController')
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)


@patch('pyftdi.i2c.I2cController')
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)


@patch('pyftdi.i2c.I2cController')
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()


@patch('pyftdi.i2c.I2cController')
def test_init_device_address_error(mock_controller):
    address = 'foo'
    with pytest.raises(luma.core.error.DeviceAddressError) as ex:
        ftdi_i2c(device='ftdi://dummy', address=address)
    assert str(ex.value) == f'I2C device address invalid: {address}'