Skip to content

Commit 6e52888

Browse files
committed
Disable unit testing
1 parent b338e18 commit 6e52888

File tree

121 files changed

+71
-401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+71
-401
lines changed

.github/workflows/workflow.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,3 @@ jobs:
3333
python -m pip install --upgrade pip
3434
pip install tox tox-gh-actions
3535
- run: tox -e docs
36-
37-
test:
38-
name: Test
39-
runs-on: ubuntu-latest
40-
strategy:
41-
matrix:
42-
python-version: ['3.8', '3.9', '3.10', '3.11']
43-
steps:
44-
- uses: actions/checkout@v3
45-
- uses: actions/setup-python@v4
46-
with:
47-
python-version: ${{ matrix.python-version }}
48-
- run: |
49-
python -m pip install --upgrade pip
50-
pip install tox tox-gh-actions
51-
- run: tox -e playback

pslab/serial_handler.py

Lines changed: 10 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ def __init__(
8787
timeout: float = 1.0,
8888
):
8989
self.version = ""
90-
self._log = b""
91-
self._logging = False
9290
self.interface = serial.Serial()
9391

9492
self.send_byte = partial(self._send, size=1)
@@ -264,7 +262,6 @@ def get_version(self) -> str:
264262
self.send_byte(CP.COMMON)
265263
self.send_byte(CP.GET_VERSION)
266264
version = self.interface.readline()
267-
self._write_log(version, "RX")
268265
return version.decode("utf-8")
269266

270267
def get_ack(self) -> int:
@@ -345,9 +342,7 @@ def _receive(self, size: int) -> int:
345342
return retval
346343

347344
def read(self, number_of_bytes: int) -> bytes:
348-
"""Log incoming bytes.
349-
350-
Wrapper for Serial.read().
345+
"""Read bytes from serial port.
351346
352347
Parameters
353348
----------
@@ -359,26 +354,22 @@ def read(self, number_of_bytes: int) -> bytes:
359354
bytes
360355
Bytes read from the serial port.
361356
"""
362-
data = self.interface.read(number_of_bytes)
363-
self._write_log(data, "RX")
364-
return data
365-
366-
def write(self, data: bytes):
367-
"""Log outgoing bytes.
357+
return self.interface.read(number_of_bytes)
368358

369-
Wrapper for Serial.write().
359+
def write(self, data: bytes) -> int:
360+
"""Write bytes to serial port.
370361
371362
Parameters
372363
----------
373364
data : int
374365
Bytes to write to the serial port.
375-
"""
376-
self.interface.write(data)
377-
self._write_log(data, "TX")
378366
379-
def _write_log(self, data: bytes, direction: str):
380-
if self._logging:
381-
self._log += direction.encode() + data + "STOP".encode()
367+
Returns
368+
-------
369+
int
370+
Number of bytes written.
371+
"""
372+
return self.interface.write(data)
382373

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

405396

406-
RECORDED_TRAFFIC = iter([])
407-
"""An iterator returning (request, response) pairs.
408-
409-
The request is checked against data written to the dummy serial port, and if it matches
410-
the response can be read back. Both request and response should be bytes-like.
411-
412-
Intended to be monkey-patched by the calling test module.
413-
"""
414-
415-
416-
class MockHandler(SerialHandler):
417-
"""Mock implementation of :class:`SerialHandler` for testing.
418-
419-
Parameters
420-
----------
421-
Same as :class:`SerialHandler`.
422-
"""
423-
424-
VERSION = "PSLab vMOCK"
425-
426-
def __init__(
427-
self,
428-
port: str = None,
429-
baudrate: int = 1000000,
430-
timeout: float = 1.0,
431-
):
432-
self._in_buffer = b""
433-
super().__init__(port, baudrate, timeout)
434-
435-
@staticmethod
436-
def check_serial_access_permission():
437-
"""See :meth:`SerialHandler.check_serial_access_permission`."""
438-
pass
439-
440-
def connect(
441-
self,
442-
port: str = None,
443-
baudrate: int = 1000000,
444-
timeout: float = 1.0,
445-
):
446-
"""See :meth:`SerialHandler.connect`."""
447-
self.version = self.get_version()
448-
449-
def disconnect(self):
450-
"""See :meth:`SerialHandler.disconnect`."""
451-
pass
452-
453-
def reconnect(
454-
self,
455-
port: str = None,
456-
baudrate: int = None,
457-
timeout: float = None,
458-
):
459-
"""See :meth:`SerialHandler.reconnect`."""
460-
pass
461-
462-
def get_version(self) -> str:
463-
"""Return mock version."""
464-
return self.VERSION
465-
466-
def read(self, number_of_bytes: int) -> bytes:
467-
"""Mimic the behavior of the serial bus by returning recorded RX traffic.
468-
469-
The returned data depends on how :meth:`write` was called prior to calling
470-
:meth:`read`.
471-
472-
See also :meth:`SerialHandler.read`.
473-
"""
474-
read_bytes = self._in_buffer[:number_of_bytes]
475-
self._in_buffer = self._in_buffer[number_of_bytes:]
476-
return read_bytes
477-
478-
def write(self, data: bytes):
479-
"""Add recorded RX data to buffer if written data equals recorded TX data.
480-
481-
See also :meth:`SerialHandler.write`.
482-
"""
483-
tx, rx = next(RECORDED_TRAFFIC)
484-
if tx == data:
485-
self._in_buffer += rx
486-
487-
def wait_for_data(self, timeout: float = 0.2) -> bool:
488-
"""Return True if there is data in buffer, or return False after timeout."""
489-
if self._in_buffer:
490-
return True
491-
else:
492-
time.sleep(timeout)
493-
return False
494-
495-
496397
class ADCBufferMixin:
497398
"""Mixin for classes that need to read or write to the ADC buffer."""
498399

tests/conftest.py

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,11 @@
1-
"""Tests can be run as either unit tests or integration tests.
2-
3-
By default, they are run as unit tests. When running as unit tests, recorded
4-
serial traffic from passing integration tests are played back during the test.
5-
6-
By calling pytest with the --integration flag, the tests will instead be run as
7-
integration tests. In this mode, a real PSLab device must be connected.
8-
Additionally, certain pins must be connected in a specific manner. Refer to the
9-
individual test modules in the tests package.
10-
11-
By calling pytest with the --record flag, the serial traffic generated by the
12-
integration tests will be recorded to JSON files, which are played back during
13-
unit testing. The --record flag implies --integration.
14-
"""
15-
16-
import json
17-
import os
1+
"""Common fixtures for pslab tests."""
182

193
import pytest
204

215
from pslab import serial_handler
226

237

24-
def pytest_addoption(parser):
25-
parser.addoption("--integration", action="store_true", default=False)
26-
parser.addoption("--record", action="store_true", default=False)
27-
28-
29-
@pytest.fixture(scope="module")
30-
def logdir(request):
31-
# First five chars are "test_", last three are ".py".
32-
return os.path.join("tests", "recordings", request.node.name[5:][:-3])
33-
34-
358
@pytest.fixture
36-
def handler(monkeypatch, request, logdir):
37-
"""Return a SerialHandler instance.
38-
39-
When running unit tests, the SerialHandler is a MockHandler.
40-
"""
41-
record = request.config.getoption("--record")
42-
integration = request.config.getoption("--integration") or record
43-
logfile = os.path.join(logdir, request.node.name + ".json")
44-
45-
if integration:
46-
H = serial_handler.SerialHandler()
47-
else:
48-
tx, rx = json.load(open(logfile, "r"))
49-
traffic = ((bytes(t), bytes(r)) for t, r in zip(tx, rx))
50-
monkeypatch.setattr(serial_handler, "RECORDED_TRAFFIC", traffic)
51-
H = serial_handler.MockHandler()
52-
53-
yield H
54-
55-
if record:
56-
log = H._log.split(b"STOP")[:-1]
57-
record_traffic(log, logfile)
58-
59-
60-
def record_traffic(log: list, logfile: str):
61-
"""Record serial traffic to a JSON file.
62-
63-
The file name is the test name + .json.
64-
"""
65-
tx = []
66-
rx = []
67-
68-
for b in log:
69-
direction = b[:2]
70-
data = b[2:]
71-
if direction == b"TX":
72-
tx.append(list(data))
73-
rx.append([])
74-
elif direction == b"RX":
75-
rx[-1] += list(data)
76-
else:
77-
raise ValueError(f"Unknown direction: {direction}")
78-
79-
print([tx, rx])
80-
json.dump([tx, rx], open(logfile, "w"))
9+
def handler():
10+
"""Return a SerialHandler instance."""
11+
return serial_handler.SerialHandler()

tests/recordings/i2c/test_configure.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/recordings/i2c/test_ping_slave.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/recordings/i2c/test_read.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/recordings/i2c/test_read_byte.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/recordings/i2c/test_read_int.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/recordings/i2c/test_read_long.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/recordings/i2c/test_scan.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)