def test_close_after_read(): test_file, _ = scenarios.single_segment_with_one_channel().values temp_file = test_file.get_tempfile(delete=False) try: temp_file.file.close() tdms_data = TdmsFile.read(temp_file.name) tdms_data.close() finally: os.remove(temp_file.name)
def test_lazily_read_raw_channel_data_slice(): """Test reading raw channel data lazily""" test_file, expected_data = scenarios.single_segment_with_one_channel().values with test_file.get_tempfile() as temp_file: with TdmsFile.open(temp_file.file) as tdms_file: for ((group, channel), expected_data) in expected_data.items(): actual_data = tdms_file[group][channel].read_data(offset=1, length=2, scaled=False) assert actual_data.dtype == expected_data.dtype compare_arrays(actual_data, expected_data[1:3])
def test_multiple_close_after_open(): test_file, _ = scenarios.single_segment_with_one_channel().values temp_file = test_file.get_tempfile(delete=False) try: temp_file.file.close() with TdmsFile.open(temp_file.name) as tdms_data: tdms_data.close() tdms_data.close() finally: os.remove(temp_file.name)
def test_read_raw_channel_data(): """Test reading raw channel data""" test_file, expected_data = scenarios.single_segment_with_one_channel().values with test_file.get_tempfile() as temp_file: tdms_file = TdmsFile.read(temp_file.file) for ((group, channel), expected_data) in expected_data.items(): actual_data = tdms_file[group][channel].read_data(scaled=False) assert actual_data.dtype == expected_data.dtype compare_arrays(actual_data, expected_data)
def test_debug_logging(caplog): """ Test loading a file with debug logging enabled """ test_file, expected_data = scenarios.single_segment_with_one_channel().values log_manager.set_level(logging.DEBUG) _ = test_file.load() assert "Reading metadata for object /'group'/'channel1' with index header 0x00000014" in caplog.text assert "Object data type: Int32" in caplog.text
def test_invalid_length_in_read_data_throws(): """ Exception is thrown when reading a subset of data with an invalid length """ test_file, expected_data = scenarios.single_segment_with_one_channel().values group, channel = list(expected_data.keys())[0] with test_file.get_tempfile() as temp_file: with TdmsFile.open(temp_file.file) as tdms_file: with pytest.raises(ValueError) as exc_info: tdms_file[group][channel].read_data(0, -5) assert "length must be non-negative" in str(exc_info.value)
def test_read_data_after_close_throws(): """ Trying to read after opening and closing without reading data should throw """ test_file, expected_data = scenarios.single_segment_with_one_channel().values group, channel = list(expected_data.keys())[0] with test_file.get_tempfile() as temp_file: with TdmsFile.open(temp_file.file) as tdms_file: pass with pytest.raises(RuntimeError) as exc_info: tdms_file[group][channel].read_data() assert "Cannot read data after the underlying TDMS reader is closed" in str(exc_info.value)
def test_read_data_after_open_in_read_mode_throws(): """ Trying to read channel data after reading all data initially should throw """ test_file, expected_data = scenarios.single_segment_with_one_channel( ).values group, channel = list(expected_data.keys())[0] with test_file.get_tempfile() as temp_file: tdms_file = TdmsFile.read(temp_file.file) with pytest.raises(RuntimeError) as exc_info: tdms_file[group][channel].read_data() assert "Cannot read data after the underlying TDMS reader is closed" in str( exc_info.value)
def test_read_file_passed_as_pathlib_path(): """ Test reading a file when using a pathlib Path object """ import pathlib test_file, expected_data = scenarios.single_segment_with_one_channel().values with test_file.get_tempfile_with_index() as tdms_file_path_str: tdms_file_path = pathlib.Path(tdms_file_path_str) tdms_file = TdmsFile.read(tdms_file_path) for ((group, channel), expected_channel_data) in expected_data.items(): channel_obj = tdms_file[group][channel] compare_arrays(channel_obj.data, expected_channel_data)
def test_lazily_read_channel_data_with_file_path(): """Test reading channel data lazily after initialising with a file path """ test_file, expected_data = scenarios.single_segment_with_one_channel().values temp_file = test_file.get_tempfile(delete=False) try: temp_file.file.close() with TdmsFile.open(temp_file.name) as tdms_file: for ((group, channel), expected_data) in expected_data.items(): actual_data = tdms_file[group][channel].read_data() assert actual_data.dtype == expected_data.dtype compare_arrays(actual_data, expected_data) finally: os.remove(temp_file.name)
def test_access_data_property_after_opening_throws(): """ Accessing the data property after opening without reading data should throw """ test_file, expected_data = scenarios.single_segment_with_one_channel().values group, channel = list(expected_data.keys())[0] with test_file.get_tempfile() as temp_file: with TdmsFile.open(temp_file.file) as tdms_file: with pytest.raises(RuntimeError) as exc_info: _ = tdms_file[group][channel].data assert "Channel data has not been read" in str(exc_info.value) with pytest.raises(RuntimeError) as exc_info: _ = tdms_file[group][channel].raw_data assert "Channel data has not been read" in str(exc_info.value) with pytest.raises(RuntimeError) as exc_info: _ = tdms_file[group][channel].raw_scaler_data assert "Channel data has not been read" in str(exc_info.value)
def test_memory_released_when_tdms_file_out_of_scope(): """ Tests that when a TDMS file object goes out of scope, TDMS channels and their data are also freed. This ensures there are no circular references between a TDMS file and its channels, which would mean the GC is needed to free these objects. """ test_file, expected_data = scenarios.single_segment_with_one_channel().values with test_file.get_tempfile() as temp_file: tdms_data = TdmsFile.read(temp_file.file) chan = tdms_data['group']['channel1'] chan_ref = weakref.ref(chan) data_ref = weakref.ref(chan.data) raw_data_ref = weakref.ref(chan.raw_data) del tdms_data del chan assert raw_data_ref() is None assert data_ref() is None assert chan_ref() is None