|
21 | 21 |
|
22 | 22 | import pytest |
23 | 23 |
|
24 | | -from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec |
| 24 | +from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionMap, PartitionSpec |
25 | 25 | from pyiceberg.schema import Schema |
26 | 26 | from pyiceberg.transforms import ( |
27 | 27 | BucketTransform, |
@@ -225,5 +225,63 @@ def test_deserialize_partition_field_v3() -> None: |
225 | 225 | assert field == PartitionField(source_id=1, field_id=1000, transform=TruncateTransform(width=19), name="str_truncate") |
226 | 226 |
|
227 | 227 |
|
228 | | -def test_partition_map() -> None: |
229 | | - pass |
| 228 | +@pytest.fixture |
| 229 | +def specs_set() -> dict[int, PartitionSpec]: |
| 230 | + return { |
| 231 | + 0: UNPARTITIONED_PARTITION_SPEC, |
| 232 | + 1: PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=DayTransform(), name="dayPartition"), spec_id=1), |
| 233 | + 2: PartitionSpec( |
| 234 | + PartitionField(source_id=1, field_id=1000, transform=DayTransform(), name="dayPartition"), |
| 235 | + PartitionField(source_id=2, field_id=1001, transform=IdentityTransform(), name="identityPartition"), |
| 236 | + spec_id=2, |
| 237 | + ), |
| 238 | + } |
| 239 | + |
| 240 | + |
| 241 | +def test_empty_partition_map() -> None: |
| 242 | + specs: dict[int, PartitionSpec] = {UNPARTITIONED_PARTITION_SPEC.spec_id: UNPARTITIONED_PARTITION_SPEC} |
| 243 | + partition_map: PartitionMap[str] = PartitionMap(specs) |
| 244 | + assert partition_map.is_empty() |
| 245 | + assert len(partition_map) == 0 |
| 246 | + assert not partition_map.contains_key(1, Record(1)) |
| 247 | + assert len(partition_map.values()) == 0 |
| 248 | + |
| 249 | + |
| 250 | +def test_size_partition_map(specs_set: dict[int, PartitionSpec]) -> None: |
| 251 | + partition_map: PartitionMap[str] = PartitionMap(specs_set) |
| 252 | + partition_map.put(UNPARTITIONED_PARTITION_SPEC.spec_id, None, "v1") |
| 253 | + partition_map.put(specs_set[1].spec_id, Record("aaa"), "v2") |
| 254 | + partition_map.put(specs_set[1].spec_id, Record("bbb"), "v3") |
| 255 | + partition_map.put(specs_set[2].spec_id, Record("ccc", 2), "v4") |
| 256 | + assert not partition_map.is_empty() |
| 257 | + assert len(partition_map) == 4 |
| 258 | + # assert partition_map.get(UNPARTITIONED_PARTITION_SPEC.spec_id, None) == "v1" |
| 259 | + |
| 260 | + |
| 261 | +def test_put_and_get_partition_map(specs_set: dict[int, PartitionSpec]) -> None: |
| 262 | + partition_map: PartitionMap[str] = PartitionMap(specs_set) |
| 263 | + partition_map.put(UNPARTITIONED_PARTITION_SPEC.spec_id, None, "v1") |
| 264 | + partition_map.put(specs_set[1].spec_id, Record("aaa", 1), "v2") |
| 265 | + assert partition_map.get(UNPARTITIONED_PARTITION_SPEC.spec_id, None) == "v1" |
| 266 | + assert partition_map.get(specs_set[1].spec_id, Record("aaa", 1)) == "v2" |
| 267 | + |
| 268 | + |
| 269 | +def test_values_partition_map(specs_set: dict[int, PartitionSpec]) -> None: |
| 270 | + partition_map: PartitionMap[str] = PartitionMap(specs_set) |
| 271 | + partition_map.put(UNPARTITIONED_PARTITION_SPEC.spec_id, None, "v1") |
| 272 | + partition_map.put(specs_set[1].spec_id, Record("aaa"), "v2") |
| 273 | + partition_map.put(specs_set[1].spec_id, Record("bbb"), "v3") |
| 274 | + partition_map.put(specs_set[2].spec_id, Record("ccc", 2), "v4") |
| 275 | + assert partition_map.values() == ["v1", "v2", "v3", "v4"] |
| 276 | + |
| 277 | + |
| 278 | +def test_compute_if_absent_partition_map(specs_set: dict[int, PartitionSpec]) -> None: |
| 279 | + partition_map: PartitionMap[str] = PartitionMap(specs_set) |
| 280 | + |
| 281 | + result1 = partition_map.compute_if_absent(specs_set[1].spec_id, Record("a"), "v1", lambda: "v1") |
| 282 | + assert result1 == "v1" |
| 283 | + assert partition_map.get(specs_set[1].spec_id, Record("a")) == "v1" |
| 284 | + |
| 285 | + result2 = partition_map.compute_if_absent(specs_set[1].spec_id, Record("a"), "v2", lambda: "v2") |
| 286 | + assert result2 == "v1" |
| 287 | + assert partition_map.get(specs_set[1].spec_id, Record("a")) == "v1" |
0 commit comments