From 02814f9dfe534230152bb0150dfd3cd7114c684f Mon Sep 17 00:00:00 2001 From: tligui_y Date: Fri, 25 Jul 2025 10:47:09 +0200 Subject: [PATCH] Add tests/test_utils_channels.py --- tests/test_utils_channels.py | 73 ++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/test_utils_channels.py diff --git a/tests/test_utils_channels.py b/tests/test_utils_channels.py new file mode 100644 index 000000000..e0370dacf --- /dev/null +++ b/tests/test_utils_channels.py @@ -0,0 +1,73 @@ +import pytest +import sys +import os +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +from slic.utils.channels import * +from unittest.mock import patch + +import tempfile +import warnings + + +CHANNELS_FILE_TEXT_1 = """\ +# Main experiment channels +channel_1 +channel_2 # analog input +chan ne ls_3 + +another : channel_6 + +channel_1 # duplicate, should be ignored + CHN_LS4 +# channel_5 is deprecated +""" + +CHANNELS_FILE_TEXT_2 = """\ +# Additional DAQ channels with edge cases + channel_6 +channel_7#commented_inline +channel_8 # this is a valid channel +channel_6 +# All lines after are comments only +# + # fully commented out +channel_9 + +# Special cases: whitespace, tabs, upper/lower + CHANNEL_10 +\tchannel_11 +""" + +# ----------- TESTS ------------ + +def test_load_channels_and_channels_class_with_professional_names(): + + test_cases = [ + (CHANNELS_FILE_TEXT_1, ['CHN_LS4', 'another : channel_6', 'chan ne ls_3', 'channel_1', 'channel_2']), + (CHANNELS_FILE_TEXT_2, [ + 'CHANNEL_10', + 'channel_11', + 'channel_6', + 'channel_7', + 'channel_8', + 'channel_9', + ]), + ] + for file_text, expected in test_cases: + with tempfile.NamedTemporaryFile('w+', delete=False) as tmpfile: + tmpfile.write(file_text) + tmpfile_path = tmpfile.name + try: + # Test load_channels function + result_func = load_channels(tmpfile_path) + assert result_func == expected + + # Test Channels class : including DeprecationWarning + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + result_class = Channels(tmpfile_path) + assert list(result_class) == expected + dep_warnings = [warn for warn in w if issubclass(warn.category, DeprecationWarning)] + assert len(dep_warnings) == 1 + finally: + os.remove(tmpfile_path)