Skip to content

Commit

Permalink
Include logging in tests (#27)
Browse files Browse the repository at this point in the history
* improved logging

* include logging in test
  • Loading branch information
lobis authored Jul 3, 2023
1 parent 8fee1f7 commit 9373fe3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/hvps/commands/caen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ def _write_command(
command: bytes,
response: bool = True,
) -> str | None:
logger.debug(f"Send command: {command}")
logger.debug(f"Sending command: {command}")
if not ser.is_open:
logger.error("Serial port is not open")
raise serial.SerialException("Serial port is not open")

ser.write(command)
if not response:
logger.warning(
"Calling _write_command without expecting a response. Manual readout of the response is required."
)
return None

response = ser.readline()
Expand Down
2 changes: 2 additions & 0 deletions src/hvps/devices/caen/caen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

class Caen(Hvps):
def module(self, module: int = 0) -> Module:
self._logger.debug(f"Getting module {module}")
validate_board_number(module)
if module not in self._modules:
self._logger.debug(f"Creating module {module}")
self._modules[module] = Module(self._serial, self._logger, module)
return self._modules[module]
7 changes: 7 additions & 0 deletions src/hvps/devices/hvps.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def __init__(
)
self._logger.setLevel(logging_level)

formatter = logging.Formatter(
"%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
self._logger.addHandler(stream_handler)

self._modules: Dict[int, Module] = {}

if port is None and connect:
Expand Down
28 changes: 26 additions & 2 deletions tests/test_caen_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,39 @@
import pytest


def test_caen_module():
caen = Caen(connect=False)
def test_caen_init(caplog):
caplog.set_level("DEBUG")

Caen(connect=False)

assert caplog.text == ""

caen = Caen(connect=False, logging_level="DEBUG")

assert caen.baudrate == 115200
assert "Using baud rate 115200" in caplog.text
assert "Using port " in caplog.text
assert "Using timeout " in caplog.text


def test_caen_module(caplog):
caplog.set_level("DEBUG")

caen = Caen(connect=False, logging_level="DEBUG")

# for CAEN, modules are dynamically created
[caen.module(i) for i in range(0, 32)]

assert "Getting module 0" in caplog.text

# check exception takes place
with pytest.raises(Exception):
caen.module(-1)

with pytest.raises(Exception):
caen.module(35)


def test_caen_channel():
# TODO: implement
pass

0 comments on commit 9373fe3

Please sign in to comment.