Skip to content

Commit

Permalink
Disable unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bessman committed Aug 22, 2024
1 parent b338e18 commit 7cc9691
Show file tree
Hide file tree
Showing 121 changed files with 71 additions and 401 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,3 @@ jobs:
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- run: tox -e docs

test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- run: tox -e playback
119 changes: 10 additions & 109 deletions pslab/serial_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ def __init__(
timeout: float = 1.0,
):
self.version = ""
self._log = b""
self._logging = False
self.interface = serial.Serial()

self.send_byte = partial(self._send, size=1)
Expand Down Expand Up @@ -264,7 +262,6 @@ def get_version(self) -> str:
self.send_byte(CP.COMMON)
self.send_byte(CP.GET_VERSION)
version = self.interface.readline()
self._write_log(version, "RX")
return version.decode("utf-8")

def get_ack(self) -> int:
Expand Down Expand Up @@ -345,9 +342,7 @@ def _receive(self, size: int) -> int:
return retval

def read(self, number_of_bytes: int) -> bytes:
"""Log incoming bytes.
Wrapper for Serial.read().
"""Read bytes from serial port.
Parameters
----------
Expand All @@ -359,26 +354,22 @@ def read(self, number_of_bytes: int) -> bytes:
bytes
Bytes read from the serial port.
"""
data = self.interface.read(number_of_bytes)
self._write_log(data, "RX")
return data

def write(self, data: bytes):
"""Log outgoing bytes.
return self.interface.read(number_of_bytes)

Wrapper for Serial.write().
def write(self, data: bytes) -> int:
"""Write bytes to serial port.
Parameters
----------
data : int
Bytes to write to the serial port.
"""
self.interface.write(data)
self._write_log(data, "TX")
def _write_log(self, data: bytes, direction: str):
if self._logging:
self._log += direction.encode() + data + "STOP".encode()
Returns
-------
int
Number of bytes written.
"""
return self.interface.write(data)

def wait_for_data(self, timeout: float = 0.2) -> bool:
"""Wait for :timeout: seconds or until there is data in the input buffer.
Expand All @@ -403,96 +394,6 @@ def wait_for_data(self, timeout: float = 0.2) -> bool:
return False


RECORDED_TRAFFIC = iter([])
"""An iterator returning (request, response) pairs.
The request is checked against data written to the dummy serial port, and if it matches
the response can be read back. Both request and response should be bytes-like.
Intended to be monkey-patched by the calling test module.
"""


class MockHandler(SerialHandler):
"""Mock implementation of :class:`SerialHandler` for testing.
Parameters
----------
Same as :class:`SerialHandler`.
"""

VERSION = "PSLab vMOCK"

def __init__(
self,
port: str = None,
baudrate: int = 1000000,
timeout: float = 1.0,
):
self._in_buffer = b""
super().__init__(port, baudrate, timeout)

@staticmethod
def check_serial_access_permission():
"""See :meth:`SerialHandler.check_serial_access_permission`."""
pass

def connect(
self,
port: str = None,
baudrate: int = 1000000,
timeout: float = 1.0,
):
"""See :meth:`SerialHandler.connect`."""
self.version = self.get_version()

def disconnect(self):
"""See :meth:`SerialHandler.disconnect`."""
pass

def reconnect(
self,
port: str = None,
baudrate: int = None,
timeout: float = None,
):
"""See :meth:`SerialHandler.reconnect`."""
pass

def get_version(self) -> str:
"""Return mock version."""
return self.VERSION

def read(self, number_of_bytes: int) -> bytes:
"""Mimic the behavior of the serial bus by returning recorded RX traffic.
The returned data depends on how :meth:`write` was called prior to calling
:meth:`read`.
See also :meth:`SerialHandler.read`.
"""
read_bytes = self._in_buffer[:number_of_bytes]
self._in_buffer = self._in_buffer[number_of_bytes:]
return read_bytes

def write(self, data: bytes):
"""Add recorded RX data to buffer if written data equals recorded TX data.
See also :meth:`SerialHandler.write`.
"""
tx, rx = next(RECORDED_TRAFFIC)
if tx == data:
self._in_buffer += rx

def wait_for_data(self, timeout: float = 0.2) -> bool:
"""Return True if there is data in buffer, or return False after timeout."""
if self._in_buffer:
return True
else:
time.sleep(timeout)
return False


class ADCBufferMixin:
"""Mixin for classes that need to read or write to the ADC buffer."""

Expand Down
77 changes: 4 additions & 73 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,11 @@
"""Tests can be run as either unit tests or integration tests.
By default, they are run as unit tests. When running as unit tests, recorded
serial traffic from passing integration tests are played back during the test.
By calling pytest with the --integration flag, the tests will instead be run as
integration tests. In this mode, a real PSLab device must be connected.
Additionally, certain pins must be connected in a specific manner. Refer to the
individual test modules in the tests package.
By calling pytest with the --record flag, the serial traffic generated by the
integration tests will be recorded to JSON files, which are played back during
unit testing. The --record flag implies --integration.
"""

import json
import os
"""Common fixtures for pslab tests."""

import pytest

from pslab import serial_handler


def pytest_addoption(parser):
parser.addoption("--integration", action="store_true", default=False)
parser.addoption("--record", action="store_true", default=False)


@pytest.fixture(scope="module")
def logdir(request):
# First five chars are "test_", last three are ".py".
return os.path.join("tests", "recordings", request.node.name[5:][:-3])


@pytest.fixture
def handler(monkeypatch, request, logdir):
"""Return a SerialHandler instance.
When running unit tests, the SerialHandler is a MockHandler.
"""
record = request.config.getoption("--record")
integration = request.config.getoption("--integration") or record
logfile = os.path.join(logdir, request.node.name + ".json")

if integration:
H = serial_handler.SerialHandler()
else:
tx, rx = json.load(open(logfile, "r"))
traffic = ((bytes(t), bytes(r)) for t, r in zip(tx, rx))
monkeypatch.setattr(serial_handler, "RECORDED_TRAFFIC", traffic)
H = serial_handler.MockHandler()

yield H

if record:
log = H._log.split(b"STOP")[:-1]
record_traffic(log, logfile)


def record_traffic(log: list, logfile: str):
"""Record serial traffic to a JSON file.
The file name is the test name + .json.
"""
tx = []
rx = []

for b in log:
direction = b[:2]
data = b[2:]
if direction == b"TX":
tx.append(list(data))
rx.append([])
elif direction == b"RX":
rx[-1] += list(data)
else:
raise ValueError(f"Unknown direction: {direction}")

print([tx, rx])
json.dump([tx, rx], open(logfile, "w"))
def handler():
"""Return a SerialHandler instance."""
return serial_handler.SerialHandler()
1 change: 0 additions & 1 deletion tests/recordings/i2c/test_configure.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_ping_slave.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_read.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_read_byte.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_read_int.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_read_long.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_scan.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_start_slave.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_status.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_stop_slave.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_write.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_write_byte.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_write_int.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/i2c/test_write_long.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/logic_analyzer/test_count_pulses.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/logic_analyzer/test_get_states.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/logic_analyzer/test_measure_interval.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/logic_analyzer/test_stop.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/motor/test_get_angle.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/motor/test_set_angle.json

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/multimeter/test_measure_capacitance.json

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/multimeter/test_measure_resistance.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/multimeter/test_measure_voltage.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/recordings/multimeter/test_voltmeter_autorange.json

This file was deleted.

Loading

0 comments on commit 7cc9691

Please sign in to comment.