sangaboard.testing module

Classes that enable unit testing by mocking the serial responses.

class sangaboard.testing.DummySerialDevice[source]

Bases: object

This class exists to allow unit testing without serial port hardware.

It can be passed to an ExtensibleSerialInstrument in place of the serial port name, and will emulate a device using stored responses.

It works on a line-by-line basis, so it will only do anything once it gets a termination character (by default, newline). It tries to give the same interface as pyserial.Serial at least as far as the methods used by ExtensibleSerialInstrument go.

The timeout property will be used to cause delays if there is no response to give - but infinite timeouts raise an exception rather than attempt to do anything clever with threads.

You can set print_buffers to True in order to make it print the input and output buffers each time the “port” is written to or read from.

termination_character = '\n'
responses = ()
unmatched_response = 'error!\n'
timeout = 0
print_buffers = False
read_buffer = b''
write_buffer = b''
isOpen()[source]
write(bytes_buffer)[source]

Accept data being written to the port

This is data being sent out of the serial port, and is stored in the write buffer.

inWaiting()[source]

Return the number of bytes in the input buffer

Note the input buffer is for bytes being read from the port

flushInput()[source]

Flush the input buffer, so there are no previously-arrived bytes when we read a response.

register_response(regular_expression, response_function)[source]

Match a query and respond to it with a function.

See respond_to for details of how this works.

remove_response(regex_to_remove)[source]

Remove a previously registered response

The regular expression string must be exactly the same as the one you are trying to remove.

replace_response(regex, response_function)[source]

Remove a response and replace it with another.

Order is not preserved.

respond_to(message)[source]

Do something in response to a message.

Each time we receive a termination character, we attempt to match the message (which doesn’t include the termination character) with the regular expressions in the responses property. The first one that matches is used.

The regular expression match object’s groups() value is passed to the function, which can then use it to return its response.

readline()[source]

Emulate reading a line from the serial port.

read(size=1)[source]

Emulate reading some bytes from the serial port.

open()[source]

Open the serial port (currently does nothing)

close()[source]

Close the serial port (currently does nothing)

class sangaboard.testing.DummySangaboardDevice(firmware_version='Sangaboard Firmware v0.5.1')[source]

Bases: DummySerialDevice

Create a dummy port, with the minimal responses to work

When the Sangaboard class is initialised, it checks the board, the version, and the optional modules. All of those commands must therefore return reasonable values.

blocking_moves = False
set_blocking_moves(groups)[source]
pwm_channels(_groups)[source]
led_frequency(groups)[source]
led_pwm(groups)[source]
led_cc(groups)[source]