|
| 1 | +"""Unit tests for the Delta model.""" |
| 2 | +import pytest |
| 3 | + |
| 4 | +from deltacat.storage.model.delta import Delta, DeltaLocator |
| 5 | + |
| 6 | + |
| 7 | +class TestDeltaLocator: |
| 8 | + """Tests for DeltaLocator stream_timestamp functionality.""" |
| 9 | + |
| 10 | + def test_stream_timestamp_valid(self): |
| 11 | + """Test that valid millisecond timestamps are accepted.""" |
| 12 | + locator = DeltaLocator.of( |
| 13 | + partition_locator=None, |
| 14 | + stream_position=123, |
| 15 | + stream_timestamp=1705849200000, # Valid milliseconds timestamp |
| 16 | + ) |
| 17 | + assert locator.stream_timestamp == 1705849200000 |
| 18 | + assert locator.stream_position == 123 |
| 19 | + |
| 20 | + def test_stream_timestamp_none(self): |
| 21 | + """Test that None timestamps are accepted.""" |
| 22 | + locator = DeltaLocator.of( |
| 23 | + partition_locator=None, |
| 24 | + stream_position=123, |
| 25 | + stream_timestamp=None, |
| 26 | + ) |
| 27 | + assert locator.stream_timestamp is None |
| 28 | + assert locator.stream_position == 123 |
| 29 | + |
| 30 | + def test_stream_timestamp_invalid_too_small(self): |
| 31 | + """Test that timestamps that are too small are rejected.""" |
| 32 | + locator = DeltaLocator.of() |
| 33 | + with pytest.raises(ValueError, match="stream_timestamp must be unix milliseconds"): |
| 34 | + locator.stream_timestamp = 123 # Invalid - not milliseconds (too small) |
| 35 | + |
| 36 | + def test_stream_timestamp_invalid_too_large(self): |
| 37 | + """Test that timestamps that are too large are rejected.""" |
| 38 | + locator = DeltaLocator.of() |
| 39 | + with pytest.raises(ValueError, match="stream_timestamp must be unix milliseconds"): |
| 40 | + locator.stream_timestamp = 10_000_000_000_000 # Invalid - too large |
| 41 | + |
| 42 | + def test_stream_timestamp_boundary_min(self): |
| 43 | + """Test that the minimum valid timestamp is accepted.""" |
| 44 | + locator = DeltaLocator.of() |
| 45 | + locator.stream_timestamp = 1_000_000_000_000 # Minimum valid |
| 46 | + assert locator.stream_timestamp == 1_000_000_000_000 |
| 47 | + |
| 48 | + def test_stream_timestamp_boundary_max(self): |
| 49 | + """Test that the maximum valid timestamp is accepted.""" |
| 50 | + locator = DeltaLocator.of() |
| 51 | + locator.stream_timestamp = 9_999_999_999_999 # Maximum valid |
| 52 | + assert locator.stream_timestamp == 9_999_999_999_999 |
| 53 | + |
| 54 | + def test_stream_timestamp_persisted_as_camel_case(self): |
| 55 | + """Test that stream_timestamp is stored with camelCase key.""" |
| 56 | + locator = DeltaLocator.of() |
| 57 | + locator.stream_timestamp = 1705849200000 |
| 58 | + assert "streamTimestamp" in locator |
| 59 | + assert locator["streamTimestamp"] == 1705849200000 |
| 60 | + |
| 61 | + |
| 62 | +class TestDelta: |
| 63 | + """Tests for Delta stream_timestamp property.""" |
| 64 | + |
| 65 | + def test_stream_timestamp_delegates_to_locator(self): |
| 66 | + """Test that Delta.stream_timestamp delegates to the locator.""" |
| 67 | + locator = DeltaLocator.of( |
| 68 | + partition_locator=None, |
| 69 | + stream_position=123, |
| 70 | + stream_timestamp=1705849200000, |
| 71 | + ) |
| 72 | + delta = Delta.of( |
| 73 | + locator=locator, |
| 74 | + delta_type=None, |
| 75 | + meta=None, |
| 76 | + properties=None, |
| 77 | + manifest=None, |
| 78 | + ) |
| 79 | + assert delta.stream_timestamp == 1705849200000 |
| 80 | + |
| 81 | + def test_stream_timestamp_returns_none_when_locator_is_none(self): |
| 82 | + """Test that Delta.stream_timestamp returns None when locator is None.""" |
| 83 | + delta = Delta.of( |
| 84 | + locator=None, |
| 85 | + delta_type=None, |
| 86 | + meta=None, |
| 87 | + properties=None, |
| 88 | + manifest=None, |
| 89 | + ) |
| 90 | + assert delta.stream_timestamp is None |
| 91 | + |
| 92 | + def test_stream_timestamp_returns_none_when_timestamp_not_set(self): |
| 93 | + """Test that Delta.stream_timestamp returns None when not set on locator.""" |
| 94 | + locator = DeltaLocator.of( |
| 95 | + partition_locator=None, |
| 96 | + stream_position=123, |
| 97 | + ) |
| 98 | + delta = Delta.of( |
| 99 | + locator=locator, |
| 100 | + delta_type=None, |
| 101 | + meta=None, |
| 102 | + properties=None, |
| 103 | + manifest=None, |
| 104 | + ) |
| 105 | + assert delta.stream_timestamp is None |
0 commit comments