"""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 = """ """ 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 = """ """ 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!")