diff options
| author | Sam Scholten | 2025-10-24 11:23:50 +1000 |
|---|---|---|
| committer | Sam Scholten | 2025-10-24 11:23:50 +1000 |
| commit | 42e4e71e7fb32e166fe4821566297b4daf6d9a2a (patch) | |
| tree | 2b754997da3f13654a9dbaf49d8825791a5cf3d7 | |
| parent | 89aab509ccb0a30aa7f02c154a2a91506db7ba6d (diff) | |
| download | transivent-42e4e71e7fb32e166fe4821566297b4daf6d9a2a.tar.gz transivent-42e4e71e7fb32e166fe4821566297b4daf6d9a2a.zip | |
Test: Add tests for path handling in rd() function
- Test absolute filename with data_path (main bug fix scenario)
- Test relative filename with data_path
- Test missing XML sidecar error handling
- Added pytest as dev dependency
| -rw-r--r-- | pyproject.toml | 5 | ||||
| -rw-r--r-- | tests/test_io_path_handling.py | 125 |
2 files changed, 130 insertions, 0 deletions
diff --git a/pyproject.toml b/pyproject.toml index 859c39e..9f01784 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,3 +22,8 @@ build-backend = "setuptools.build_meta" [tool.setuptools.packages.find] where = ["src"] +[dependency-groups] +dev = [ + "pytest>=8.3.5", +] + diff --git a/tests/test_io_path_handling.py b/tests/test_io_path_handling.py new file mode 100644 index 0000000..8c140ca --- /dev/null +++ b/tests/test_io_path_handling.py @@ -0,0 +1,125 @@ +"""Test path handling in io.py, particularly for rd() function.""" + +import os +import tempfile +from pathlib import Path + +import numpy as np +import pytest + +from transivent.io import get_waveform_params, rd + + +def test_rd_with_data_path_and_absolute_filename(): + """ + Test that rd() correctly handles data_path when filename is absolute. + + This tests the fix for the issue where rd() would ignore data_path + if filename was an absolute path, causing XML sidecar lookup to fail. + """ + with tempfile.TemporaryDirectory() as tmpdir: + # Create test data directory + data_dir = Path(tmpdir) / "data" + data_dir.mkdir() + + # Create test files + bin_file = data_dir / "test.Wfm.bin" + xml_file = data_dir / "test.bin" + + # Create a simple binary file with header + # Header: 8 bytes (element_size=4, record_length=1000) + # Data: 1000 float32 values + header = np.array([4, 1000], dtype=np.uint32) + data = np.random.randn(1000).astype(np.float32) + + with open(bin_file, "wb") as f: + header.tofile(f) + data.tofile(f) + + # Create XML sidecar content + xml_content = """<?xml version="1.0" standalone="no"?> +<Database> + <Group Name="ReferenceCurveAttributes"> + <Prop Name="Resolution" Value="5e-08"/> + <Prop Name="ByteOrder" Value="LSB"/> + <Prop Name="SignalFormat" Value="FLOAT"/> + <Prop Name="SignalHardwareRecordLength" Value="1000"/> + </Group> +</Database>""" + + with open(xml_file, "w") as f: + f.write(xml_content) + + # Test 1: rd() with absolute filename and data_path should work + # This is the main test for the fix + time, signal = rd(str(bin_file.absolute()), data_path=str(data_dir)) + + assert len(time) == 1000 + assert len(signal) == 1000 + assert time[1] - time[0] == 5e-08 + + # Test 2: rd() with relative filename and data_path should also work + os.chdir(tmpdir) + time2, signal2 = rd("test.Wfm.bin", data_path="data") + + np.testing.assert_array_equal(time, time2) + np.testing.assert_array_equal(signal, signal2) + + # Test 3: rd() without data_path should work if XML is in same dir + time3, signal3 = rd(str(bin_file)) + + np.testing.assert_array_equal(time, time3) + np.testing.assert_array_equal(signal, signal3) + + +def test_get_waveform_params_with_data_path(): + """Test that get_waveform_params correctly uses data_path.""" + with tempfile.TemporaryDirectory() as tmpdir: + # Create test XML file + xml_file = Path(tmpdir) / "test.bin" + xml_content = """<?xml version="1.0" standalone="no"?> +<Database> + <Group Name="ReferenceCurveAttributes"> + <Prop Name="Resolution" Value="1e-07"/> + <Prop Name="ByteOrder" Value="MSB"/> + <Prop Name="SignalFormat" Value="INT16"/> + </Group> +</Database>""" + + with open(xml_file, "w") as f: + f.write(xml_content) + + # Test with data_path + params = get_waveform_params("test.Wfm.bin", data_path=tmpdir) + + assert params["sampling_interval"] == 1e-07 + assert params["byte_order"] == "MSB" + assert params["signal_format"] == "int16" + + +def test_rd_missing_xml_sidecar(): + """Test that rd() raises appropriate error when XML sidecar is missing.""" + with tempfile.TemporaryDirectory() as tmpdir: + # Create only binary file, no XML + bin_file = Path(tmpdir) / "test.Wfm.bin" + header = np.array([4, 1000], dtype=np.uint32) + data = np.random.randn(1000).astype(np.float32) + + with open(bin_file, "wb") as f: + header.tofile(f) + data.tofile(f) + + # Should raise FileNotFoundError for missing XML + with pytest.raises(FileNotFoundError, match="XML sidecar file not found"): + rd(str(bin_file), data_path=tmpdir) + + # Also should fail without data_path + with pytest.raises(FileNotFoundError, match="XML sidecar file not found"): + rd(str(bin_file)) + + +if __name__ == "__main__": + test_rd_with_data_path_and_absolute_filename() + test_get_waveform_params_with_data_path() + test_rd_missing_xml_sidecar() + print("All tests passed!")
\ No newline at end of file |
