Beispiel #1
0
    def test_setup_io_then_setup_i2c_override(self):
        self.usb_iss.setup_io(
            io1_type=defs.IOType.OUTPUT_LOW,
            io2_type=defs.IOType.OUTPUT_HIGH,
            io3_type=defs.IOType.ANALOGUE_INPUT,
            io4_type=defs.IOType.DIGITAL_INPUT)

        assert_that(self.driver.write_cmd,
                    called_with(0x5A, [
                        0x02,
                        0x00,
                        defs.IOType.DIGITAL_INPUT.value << 6 |
                        defs.IOType.ANALOGUE_INPUT.value << 4 |
                        defs.IOType.OUTPUT_HIGH.value << 2 |
                        defs.IOType.OUTPUT_LOW.value]))

        self.usb_iss.setup_i2c(
            io1_type=defs.IOType.DIGITAL_INPUT,
            io2_type=defs.IOType.ANALOGUE_INPUT)

        assert_that(self.driver.write_cmd,
                    called_with(0x5A, [
                        0x02,
                        defs.Mode.I2C_H_400KHZ.value,
                        defs.IOType.ANALOGUE_INPUT.value << 2 |
                        defs.IOType.DIGITAL_INPUT.value]))
Beispiel #2
0
    def test_receive(self):
        self.driver.read.side_effect = [[0xFF, 0x1E, 0x02], [0x48, 0x69]]

        data = self.serial.receive()

        assert_that(data, is_([0x48, 0x69]))
        assert_that(self.driver.read, called_with(3))
        assert_that(self.driver.read, called_with(2))
Beispiel #3
0
    def test_get_rx_count(self):
        self.driver.read.side_effect = [[0xFF, 0x1E, 0x02], [0x48, 0x69]]

        rx_count = self.serial.get_rx_count()

        assert_that(rx_count, is_(2))
        assert_that(self.driver.read, called_with(3))
        assert_that(self.driver.read, called_with(2))
Beispiel #4
0
def test_writes_payload_when_started(protocol: Protocol, transport: Transport,
                                     loop: Loop) -> None:
    data = b'somedata'
    header = b'20 text/gemini; charset=utf-8\r\n'
    res = Response(data=data)
    res._start(transport, protocol, loop)
    assert_that(transport.write, called_with(header))
    assert_that(transport.write, called_with(data))
    assert_that(transport.close, called_once())
Beispiel #5
0
    def test_receive_after_delay(self):
        self.driver.read.side_effect = [
            [0xFF, 0x1E, 0x00],
            [0xFF, 0x1E, 0x00],
            [0xFF, 0x1E, 0x02],
            [0x48, 0x69],
        ] + EMPTY_READS

        data = self.serial.receive()

        assert_that(data, is_([0x48, 0x69]))
        assert_that(self.driver.read, called_with(3))
        assert_that(self.driver.read, called_with(2))
Beispiel #6
0
    def test_receive(self):
        self.driver.read.side_effect = [[0xFF, 0x1E, 0x02], [0x48, 0x69]
                                        ] + EMPTY_READS
        start_time = self.current_time

        data = self.serial.receive()

        assert_that(data, is_([0x48, 0x69]))
        assert_that(self.driver.read, called_with(3))
        assert_that(self.driver.read, called_with(2))

        elapsed_time = self.current_time - start_time
        assert_that(elapsed_time, greater_than(timedelta(milliseconds=100)))
        assert_that(elapsed_time, less_than(timedelta(milliseconds=200)))
Beispiel #7
0
def test_called_with_ok_multi():
    mock = Mock()
    mock('baz')
    mock('foo', 5)
    mock('bar', 5)

    assert_that(mock, called_with('foo', instance_of(int)))
Beispiel #8
0
def test_called_with_description(desc):
    expected = ("Mock called a value greater than <0> times with " +
                "('foo', an instance of int, )")
    matcher = called_with('foo', instance_of(int))

    matcher.describe_to(desc)
    assert_that(str(desc), equal_to(expected))
Beispiel #9
0
def test_called_with_ok_kwargs():
    matcher = called_with(x='foo', y=instance_of(int))
    mock = Mock()
    mock(x='foo', y=5)

    ok = matcher.matches(mock)
    assert_that(ok, equal_to(True))
Beispiel #10
0
def test_called_with_ok():
    matcher = called_with('foo', instance_of(int))
    mock = Mock()
    mock('foo', 5)

    ok = matcher.matches(mock)
    assert_that(ok, equal_to(True))
Beispiel #11
0
def test_called_with_description(desc):
    expected = ("Mock called a value greater than <0> times with " +
                "('foo', an instance of int, )")
    matcher = called_with('foo', instance_of(int))

    matcher.describe_to(desc)
    assert_that(str(desc), equal_to(expected))
Beispiel #12
0
def test_called_with_ok():
    matcher = called_with('foo', instance_of(int))
    mock = Mock()
    mock('foo', 5)

    ok = matcher.matches(mock)
    assert_that(ok, equal_to(True))
Beispiel #13
0
def test_called_with_ok_kwargs():
    matcher = called_with(x='foo', y=instance_of(int))
    mock = Mock()
    mock(x='foo', y=5)

    ok = matcher.matches(mock)
    assert_that(ok, equal_to(True))
Beispiel #14
0
def test_called_with_failed(desc):
    expected = "argument 1: was 'bar'"
    matcher = called_with('foo', instance_of(int))
    mock = Mock()
    mock('foo', 'bar')

    ok = matcher.matches(mock, desc)
    assert_that(ok, equal_to(False))
    assert_that(str(desc), equal_to(expected))
Beispiel #15
0
def test_called_with_failed(desc):
    expected = "argument 1: was 'bar'"
    matcher = called_with('foo', instance_of(int))
    mock = Mock()
    mock('foo', 'bar')

    ok = matcher.matches(mock, desc)
    assert_that(ok, equal_to(False))
    assert_that(str(desc), equal_to(expected))
Beispiel #16
0
def test_called_with_kwarg_failed(desc):
    expected = "value for 'foo' was 'baz'"
    matcher = called_with(foo='bar')
    mock = Mock()
    mock(foo='baz')

    ok = matcher.matches(mock, desc)
    assert_that(ok, equal_to(False))
    assert_that(str(desc), equal_to(expected))
Beispiel #17
0
def test_called_with_extra_arg(desc):
    expected = "1 extra arguments"
    matcher = called_with('spam')
    mock = Mock()
    mock('spam', 'bar')

    ok = matcher.matches(mock, desc)
    assert_that(ok, equal_to(False))
    assert_that(str(desc), equal_to(expected))
Beispiel #18
0
def test_called_with_extra_kwarg(desc):
    expected = "extra keyword argument(s) 'foo' given"
    matcher = called_with('spam')
    mock = Mock()
    mock('spam', foo='bar')

    ok = matcher.matches(mock, desc)
    assert_that(ok, equal_to(False))
    assert_that(str(desc), equal_to(expected))
Beispiel #19
0
def test_called_with_kwarg_failed(desc):
    expected = "value for 'foo' was 'baz'"
    matcher = called_with(foo='bar')
    mock = Mock()
    mock(foo='baz')

    ok = matcher.matches(mock, desc)
    assert_that(ok, equal_to(False))
    assert_that(str(desc), equal_to(expected))
Beispiel #20
0
def test_called_with_extra_kwarg(desc):
    expected = "extra keyword argument(s) 'foo' given"
    matcher = called_with('spam')
    mock = Mock()
    mock('spam', foo='bar')

    ok = matcher.matches(mock, desc)
    assert_that(ok, equal_to(False))
    assert_that(str(desc), equal_to(expected))
Beispiel #21
0
def test_called_with_extra_arg(desc):
    expected = "1 extra arguments"
    matcher = called_with('spam')
    mock = Mock()
    mock('spam', 'bar')

    ok = matcher.matches(mock, desc)
    assert_that(ok, equal_to(False))
    assert_that(str(desc), equal_to(expected))
Beispiel #22
0
def test_closes_when_started_with_error(protocol: Protocol,
                                        transport: Transport,
                                        loop: Loop) -> None:
    header = b'59 BAD_REQUEST\r\n'
    res = Response(status=Status.BAD_REQUEST)
    res._start(transport, protocol, loop)
    assert_that(transport.write, called_with(header))
    assert_that(transport.close, called_once())
    res.write(b'more data')
Beispiel #23
0
    def test_setup_io_then_change_io_defaults(self):
        self.usb_iss.setup_io(
            io1_type=defs.IOType.OUTPUT_LOW,
            io2_type=defs.IOType.OUTPUT_HIGH,
            io3_type=defs.IOType.ANALOGUE_INPUT,
            io4_type=defs.IOType.DIGITAL_INPUT)

        assert_that(self.driver.write_cmd,
                    called_with(0x5A, [0x02, 0x00, 0xB4]))

        self.usb_iss.change_io()

        assert_that(self.driver.write_cmd,
                    called_with(0x5A, [
                        0x02,
                        0x10,
                        defs.IOType.DIGITAL_INPUT.value << 6 |
                        defs.IOType.ANALOGUE_INPUT.value << 4 |
                        defs.IOType.OUTPUT_HIGH.value << 2 |
                        defs.IOType.OUTPUT_LOW.value]))
Beispiel #24
0
def test_called_with_failed():
    expected = '''
Expected: Mock called with a sequence containing ['foo', an instance of int], () None times
     but: was called with <('foo', 'bar')>, <{}>
'''
    mock = Mock()
    mock('foo', 'bar')

    with assert_raises(AssertionError) as e:
        assert_that(mock, called_with('foo', instance_of(int)))

    assert_that(str(e.exception), equal_to(expected))
Beispiel #25
0
    def test_setup_i2c_defaults_then_change_io_partial(self):
        self.usb_iss.setup_i2c()

        assert_that(self.driver.write_cmd,
                    called_with(0x5A, [
                        0x02,
                        defs.Mode.I2C_H_400KHZ.value,
                        defs.IOType.DIGITAL_INPUT.value << 2 |
                        defs.IOType.DIGITAL_INPUT.value]))

        self.usb_iss.change_io(
            io1_type=defs.IOType.OUTPUT_LOW,
            io4_type=defs.IOType.OUTPUT_HIGH)

        assert_that(self.driver.write_cmd,
                    called_with(0x5A, [
                        0x02,
                        0x10,
                        defs.IOType.OUTPUT_HIGH.value << 6 |
                        defs.IOType.DIGITAL_INPUT.value << 4 |
                        defs.IOType.DIGITAL_INPUT.value << 2 |
                        defs.IOType.OUTPUT_LOW.value]))
Beispiel #26
0
    def test_setup_spi(self):
        test_matrix = [
            (3000, 1),
            (500, 11),
            (23, 255),
        ]

        for (clk_khz, divisor) in test_matrix:
            self.usb_iss.setup_spi(
                spi_mode=defs.SPIMode.TX_IDLE_TO_ACTIVE_IDLE_HIGH,
                clock_khz=clk_khz)

            assert_that(self.driver.write_cmd,
                        called_with(0x5A, [0x02, 0x93, divisor]))
Beispiel #27
0
    def test_setup_i2c_serial(self):
        test_matrix = [
            (20, False, 0x21),
            (50, False, 0x31),
            (100, False, 0x41),
            (400, False, 0x51),
            (100, True, 0x61),
            (400, True, 0x71),
            (1000, True, 0x81),
        ]

        for (clk_khz, use_i2c_hardware, i2c_mode) in test_matrix:
            self.usb_iss.setup_i2c_serial(clock_khz=clk_khz,
                                          use_i2c_hardware=use_i2c_hardware)

            assert_that(self.driver.write_cmd,
                        called_with(0x5A, [0x02, i2c_mode, 0x01, 0x37]))
Beispiel #28
0
    def test_setup_i2c(self):
        test_matrix = [
            (20, False, 0x20),
            (50, False, 0x30),
            (100, False, 0x40),
            (400, False, 0x50),
            (100, True, 0x60),
            (400, True, 0x70),
            (1000, True, 0x80),
        ]

        for (clk_khz, use_i2c_hardware, i2c_mode) in test_matrix:
            self.usb_iss.setup_i2c(clock_khz=clk_khz,
                                   use_i2c_hardware=use_i2c_hardware,
                                   io1_type=defs.IOType.OUTPUT_LOW,
                                   io2_type=defs.IOType.OUTPUT_HIGH)

            assert_that(self.driver.write_cmd,
                        called_with(0x5A, [0x02, i2c_mode, 0x04]))
Beispiel #29
0
    def test_setup_i2c_serial_baud_rates(self):
        test_matrix = [
            (300, [0x27, 0x0F]),
            (1200, [0x09, 0xC3]),
            (2400, [0x04, 0xE1]),
            (9600, [0x01, 0x37]),
            (19200, [0x00, 0x9B]),
            (38400, [0x00, 0x4D]),
            (57600, [0x00, 0x33]),
            (115200, [0x00, 0x19]),
        ]

        for (baud_rate, divisor) in test_matrix:
            self.usb_iss.setup_i2c_serial(baud_rate=baud_rate)

            assert_that(
                self.driver.write_cmd,
                called_with(0x5A, [0x02, defs.Mode.I2C_H_400KHZ.value | 0x01] +
                            divisor))
Beispiel #30
0
    def test_setup_serial_baud_rates(self):
        test_matrix = [
            (300, [0x27, 0x0F]),
            (1200, [0x09, 0xC3]),
            (2400, [0x04, 0xE1]),
            (9600, [0x01, 0x37]),
            (19200, [0x00, 0x9B]),
            (38400, [0x00, 0x4D]),
            (57600, [0x00, 0x33]),
            (115200, [0x00, 0x19]),
        ]

        for (baud_rate, divisor) in test_matrix:
            self.usb_iss.setup_serial(baud_rate=baud_rate,
                                      io3_type=defs.IOType.OUTPUT_LOW,
                                      io4_type=defs.IOType.OUTPUT_HIGH)

            assert_that(self.driver.write_cmd,
                        called_with(0x5A, [0x02, 0x01] + divisor + [0x40]))
Beispiel #31
0
def test_called_with_ok_kwargs():
    mock = Mock()
    mock(x='foo', y=5)

    assert_that(mock, called_with(x='foo', y=instance_of(int)))