|
3 | 3 | import pickle |
4 | 4 | from pathlib import Path |
5 | 5 |
|
| 6 | +import cloudpickle |
6 | 7 | import pytest |
7 | 8 |
|
8 | 9 | from pytask import NodeInfo |
@@ -126,3 +127,43 @@ def test_hash_of_pickle_node(tmp_path, value, exists, expected): |
126 | 127 | ) |
127 | 128 | def test_comply_with_protocol(node, protocol, expected): |
128 | 129 | assert isinstance(node, protocol) is expected |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.unit |
| 133 | +def test_custom_serializer_deserializer_pickle_node(tmp_path): |
| 134 | + """Test that PickleNode correctly uses cloudpickle for de-/serialization.""" |
| 135 | + |
| 136 | + # Define custom serializer and deserializer using cloudpickle |
| 137 | + def custom_serializer(obj, file): |
| 138 | + # Custom serialization logic that adds a wrapper around the data |
| 139 | + cloudpickle.dump({"custom_prefix": obj}, file) |
| 140 | + |
| 141 | + def custom_deserializer(file): |
| 142 | + # Custom deserialization logic that unwraps the data |
| 143 | + data = cloudpickle.load(file) |
| 144 | + return data["custom_prefix"] |
| 145 | + |
| 146 | + # Create test data and path |
| 147 | + test_data = {"key": "value"} |
| 148 | + path = tmp_path.joinpath("custom.pkl") |
| 149 | + |
| 150 | + # Create PickleNode with custom serializer and deserializer |
| 151 | + node = PickleNode( |
| 152 | + name="test", |
| 153 | + path=path, |
| 154 | + serializer=custom_serializer, |
| 155 | + deserializer=custom_deserializer, |
| 156 | + ) |
| 157 | + |
| 158 | + # Test saving with custom serializer |
| 159 | + node.save(test_data) |
| 160 | + |
| 161 | + # Verify custom serialization was used by directly reading the file |
| 162 | + with path.open("rb") as f: |
| 163 | + raw_data = cloudpickle.load(f) |
| 164 | + assert "custom_prefix" in raw_data |
| 165 | + assert raw_data["custom_prefix"] == test_data |
| 166 | + |
| 167 | + # Test loading with custom deserializer |
| 168 | + loaded_data = node.load(is_product=False) |
| 169 | + assert loaded_data == test_data |
0 commit comments