Skip to content

Commit 45c62b4

Browse files
committed
Add test.
1 parent f175641 commit 45c62b4

3 files changed

Lines changed: 44 additions & 5 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ test = [
7171
"syrupy",
7272
"aiohttp", # For HTTPPath tests.
7373
"coiled",
74+
"cloudpickle",
7475
]
7576
typing = ["mypy>=1.9.0,<1.11", "nbqa>=1.8.5"]
7677

@@ -85,9 +86,7 @@ Tracker = "https://github.com/pytask-dev/pytask/issues"
8586
pytask = "pytask:cli"
8687

8788
[tool.uv]
88-
dev-dependencies = [
89-
"tox-uv>=1.7.0", "pygraphviz;platform_system=='Linux'",
90-
]
89+
dev-dependencies = ["tox-uv>=1.7.0", "pygraphviz;platform_system=='Linux'"]
9190

9291
[build-system]
9392
requires = ["hatchling", "hatch_vcs"]

tests/test_execute.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,7 @@ def test_download_file(runner, tmp_path):
985985
from upath import UPath
986986
987987
url = UPath(
988-
"https://gist.githubusercontent.com/tobiasraabe/64c24426d5398cac4b9d37b85ebfaf"
989-
"7c/raw/50c61fa9a5aa0b7d3a7582c4c260b43dabfea720/gistfile1.txt"
988+
"https://gist.githubusercontent.com/tobiasraabe/64c24426d5398cac4b9d37b85ebfaf7c/raw/50c61fa9a5aa0b7d3a7582c4c260b43dabfea720/gistfile1.txt"
990989
)
991990
992991
def task_download_file(path: UPath = url) -> Annotated[str, Path("data.csv")]:

tests/test_nodes.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pickle
44
from pathlib import Path
55

6+
import cloudpickle
67
import pytest
78

89
from pytask import NodeInfo
@@ -126,3 +127,43 @@ def test_hash_of_pickle_node(tmp_path, value, exists, expected):
126127
)
127128
def test_comply_with_protocol(node, protocol, expected):
128129
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

Comments
 (0)