74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
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)
|