| 1 |
import pickle |
| 2 |
|
| 3 |
from anytree import Node, RenderTree, SymlinkNode |
| 4 |
|
| 5 |
|
| 6 |
def test_pickle(tmp_path): |
| 7 |
"""Pickling Compatibility.""" |
| 8 |
root = Node(name="root") |
| 9 |
a = Node(name="a", parent=root) |
| 10 |
b = Node(name="b", parent=a) |
| 11 |
c = SymlinkNode(target=b, parent=a) |
| 12 |
|
| 13 |
lines = str(RenderTree(root)).splitlines() |
| 14 |
assert lines == [ |
| 15 |
"Node('/root')", |
| 16 |
"└── Node('/root/a')", |
| 17 |
" ├── Node('/root/a/b')", |
| 18 |
" └── SymlinkNode(Node('/root/a/b'))", |
| 19 |
] |
| 20 |
|
| 21 |
filepath = tmp_path / "test.pkl" |
| 22 |
with open(filepath, "wb") as file: |
| 23 |
pickle.dump(root, file) |
| 24 |
|
| 25 |
with open(filepath, "rb") as file: |
| 26 |
loaded = pickle.load(file) |
| 27 |
|
| 28 |
assert str(RenderTree(loaded)).splitlines() == lines |