|
| 1 | +"""Tests for SEG-Y spec validation against MDIO templates.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +import pytest |
| 8 | +from segy.schema import HeaderField |
| 9 | +from segy.standards import get_segy_standard |
| 10 | + |
| 11 | +from mdio.converters.segy import _validate_spec_in_template |
| 12 | + |
| 13 | + |
| 14 | +class TestValidateSpecInTemplate: |
| 15 | + """Test cases for _validate_spec_in_template function.""" |
| 16 | + |
| 17 | + def test_validation_passes_with_all_required_fields(self) -> None: |
| 18 | + """Test that validation passes when all required fields are present.""" |
| 19 | + template = MagicMock() |
| 20 | + template._dim_names = ("inline", "crossline", "time") |
| 21 | + template._coord_names = ("cdp_x", "cdp_y") |
| 22 | + |
| 23 | + # SegySpec with all required fields |
| 24 | + spec = get_segy_standard(1.0) |
| 25 | + header_fields = [ |
| 26 | + HeaderField(name="inline", byte=189, format="int32"), |
| 27 | + HeaderField(name="crossline", byte=193, format="int32"), |
| 28 | + HeaderField(name="cdp_x", byte=181, format="int32"), |
| 29 | + HeaderField(name="cdp_y", byte=185, format="int32"), |
| 30 | + ] |
| 31 | + segy_spec = spec.customize(trace_header_fields=header_fields) |
| 32 | + |
| 33 | + # Should not raise any exception |
| 34 | + _validate_spec_in_template(segy_spec, template) |
| 35 | + |
| 36 | + def test_validation_fails_with_missing_fields(self) -> None: |
| 37 | + """Test that validation fails when required fields are missing.""" |
| 38 | + # Template requiring custom fields not in standard spec |
| 39 | + template = MagicMock() |
| 40 | + template.name = "CustomTemplate" |
| 41 | + template._dim_names = ("custom_dim1", "custom_dim2", "time") |
| 42 | + template._coord_names = ("custom_coord_x", "custom_coord_y") |
| 43 | + |
| 44 | + # SegySpec with only one of the required custom fields |
| 45 | + spec = get_segy_standard(1.0) |
| 46 | + header_fields = [ |
| 47 | + HeaderField(name="custom_dim1", byte=189, format="int32"), |
| 48 | + ] |
| 49 | + segy_spec = spec.customize(trace_header_fields=header_fields) |
| 50 | + |
| 51 | + # Should raise ValueError listing the missing fields |
| 52 | + with pytest.raises(ValueError, match=r"Required fields.*not found in.*segy_spec") as exc_info: |
| 53 | + _validate_spec_in_template(segy_spec, template) |
| 54 | + |
| 55 | + error_message = str(exc_info.value) |
| 56 | + assert "custom_dim2" in error_message |
| 57 | + assert "custom_coord_x" in error_message |
| 58 | + assert "custom_coord_y" in error_message |
| 59 | + assert "CustomTemplate" in error_message |
0 commit comments