|
| 1 | +""" |
| 2 | +Test to document the expected behavior of URI path handling in the generated FMU C++ code. |
| 3 | +
|
| 4 | +This test documents the fix for the URI path parsing bug where the formatOnnxPath function |
| 5 | +in onnxFmu.cpp was incorrectly stripping 8 characters instead of 7 when removing the |
| 6 | +"file://" prefix from URIs. |
| 7 | +
|
| 8 | +The actual formatOnnxPath function is in C++ and gets compiled into the FMU binary, |
| 9 | +so this test serves as documentation of the expected behavior and can be used for |
| 10 | +regression testing if the logic is ever ported to Python. |
| 11 | +""" |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | + |
| 16 | +class TestUriPathHandling: |
| 17 | + """ |
| 18 | + Document expected URI path handling behavior. |
| 19 | + |
| 20 | + These tests describe how the formatOnnxPath C++ function should handle |
| 21 | + file:// URI prefixes on different operating systems. |
| 22 | + """ |
| 23 | + |
| 24 | + @pytest.mark.parametrize( |
| 25 | + "input_uri,expected_path", |
| 26 | + [ |
| 27 | + # Unix absolute paths with file:// prefix (7 characters) |
| 28 | + ("file:///tmp/extracted_fmu/resources/model.onnx", "/tmp/extracted_fmu/resources/model.onnx"), |
| 29 | + ("file:///home/user/fmu/model.onnx", "/home/user/fmu/model.onnx"), |
| 30 | + ("file:///var/lib/simulation/model.onnx", "/var/lib/simulation/model.onnx"), |
| 31 | + |
| 32 | + # Windows paths (for documentation - actual behavior may vary on Windows) |
| 33 | + # Note: Windows file URIs typically use file:///C:/path format |
| 34 | + ("file:///C:/Users/test/model.onnx", "/C:/Users/test/model.onnx"), |
| 35 | + |
| 36 | + # Paths without file:// prefix should remain unchanged |
| 37 | + ("/tmp/path/model.onnx", "/tmp/path/model.onnx"), |
| 38 | + ("C:/Users/test/model.onnx", "C:/Users/test/model.onnx"), |
| 39 | + ], |
| 40 | + ) |
| 41 | + def test_uri_path_transformation_expected_behavior(self, input_uri: str, expected_path: str) -> None: |
| 42 | + """ |
| 43 | + Document the expected behavior of URI to path transformation. |
| 44 | + |
| 45 | + The C++ formatOnnxPath function should: |
| 46 | + 1. Check if path starts with "file://" (7 characters) |
| 47 | + 2. If yes, strip exactly 7 characters to get the actual path |
| 48 | + 3. This preserves the leading "/" in Unix absolute paths |
| 49 | + |
| 50 | + Before the fix: |
| 51 | + - Checked for "file:///" (8 characters) |
| 52 | + - Stripped 8 characters |
| 53 | + - Result: "/tmp/path" became "tmp/path" (missing leading /) |
| 54 | + |
| 55 | + After the fix: |
| 56 | + - Checks for "file://" (7 characters) |
| 57 | + - Strips 7 characters |
| 58 | + - Result: "/tmp/path" stays "/tmp/path" (leading / preserved) |
| 59 | + """ |
| 60 | + # This is a documentation test - the actual implementation is in C++ |
| 61 | + # If this logic is ever ported to Python, this test can validate it |
| 62 | + |
| 63 | + def simulate_format_onnx_path(path: str) -> str: |
| 64 | + """Simulate the C++ formatOnnxPath behavior after the fix.""" |
| 65 | + if path.startswith("file://"): |
| 66 | + # Strip exactly 7 characters for "file://" |
| 67 | + return path[7:] |
| 68 | + return path |
| 69 | + |
| 70 | + result = simulate_format_onnx_path(input_uri) |
| 71 | + assert result == expected_path, ( |
| 72 | + f"Path transformation failed:\n" |
| 73 | + f" Input: {input_uri}\n" |
| 74 | + f" Expected: {expected_path}\n" |
| 75 | + f" Got: {result}\n" |
| 76 | + ) |
| 77 | + |
| 78 | + def test_buggy_behavior_should_not_occur(self) -> None: |
| 79 | + """ |
| 80 | + Verify that the buggy behavior (stripping 8 characters) is not present. |
| 81 | + |
| 82 | + The bug was: |
| 83 | + - Input: "file:///tmp/path/model.onnx" |
| 84 | + - Buggy output: "tmp/path/model.onnx" (missing leading /) |
| 85 | + - Correct output: "/tmp/path/model.onnx" (leading / preserved) |
| 86 | + """ |
| 87 | + input_uri = "file:///tmp/path/model.onnx" |
| 88 | + |
| 89 | + # Simulate the FIXED behavior (strip 7 chars) |
| 90 | + def fixed_behavior(path: str) -> str: |
| 91 | + if path.startswith("file://"): |
| 92 | + return path[7:] # Strip "file://" |
| 93 | + return path |
| 94 | + |
| 95 | + # Simulate the BUGGY behavior (strip 8 chars) |
| 96 | + def buggy_behavior(path: str) -> str: |
| 97 | + if path.startswith("file:///"): # Checks for 8 chars |
| 98 | + return path[8:] # Strips 8 chars - BUG! |
| 99 | + return path |
| 100 | + |
| 101 | + fixed_result = fixed_behavior(input_uri) |
| 102 | + buggy_result = buggy_behavior(input_uri) |
| 103 | + |
| 104 | + # The fixed result should have the leading slash |
| 105 | + assert fixed_result == "/tmp/path/model.onnx", "Fixed behavior should preserve leading /" |
| 106 | + |
| 107 | + # The buggy result would be missing the leading slash |
| 108 | + assert buggy_result == "tmp/path/model.onnx", "Buggy behavior strips the leading /" |
| 109 | + |
| 110 | + # Verify they are different |
| 111 | + assert fixed_result != buggy_result, "Fixed and buggy behaviors should differ" |
| 112 | + |
| 113 | + # Verify the fixed version is an absolute path |
| 114 | + assert fixed_result.startswith("/"), "Fixed result should be an absolute path on Unix" |
0 commit comments