From 9373fe3530e125e64fc8177b47c243a0d9793c96 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio <35803280+lobis@users.noreply.github.com> Date: Mon, 3 Jul 2023 10:40:31 +0200 Subject: [PATCH] Include logging in tests (#27) * improved logging * include logging in test --- src/hvps/commands/caen/__init__.py | 9 ++++++++- src/hvps/devices/caen/caen.py | 2 ++ src/hvps/devices/hvps.py | 7 +++++++ tests/test_caen_devices.py | 28 ++++++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/hvps/commands/caen/__init__.py b/src/hvps/commands/caen/__init__.py index 105f1e7..63bfa75 100644 --- a/src/hvps/commands/caen/__init__.py +++ b/src/hvps/commands/caen/__init__.py @@ -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() diff --git a/src/hvps/devices/caen/caen.py b/src/hvps/devices/caen/caen.py index 0047085..ad24ada 100644 --- a/src/hvps/devices/caen/caen.py +++ b/src/hvps/devices/caen/caen.py @@ -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] diff --git a/src/hvps/devices/hvps.py b/src/hvps/devices/hvps.py index cf561f6..2549671 100644 --- a/src/hvps/devices/hvps.py +++ b/src/hvps/devices/hvps.py @@ -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: diff --git a/tests/test_caen_devices.py b/tests/test_caen_devices.py index 13575e3..dbe1d1e 100644 --- a/tests/test_caen_devices.py +++ b/tests/test_caen_devices.py @@ -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