File: test_pickle.py

package info (click to toggle)
anytree 2.13.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,012 kB
  • sloc: python: 3,966; makefile: 64
file content (28 lines) | stat: -rw-r--r-- 745 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import pickle

from anytree import Node, RenderTree, SymlinkNode


def test_pickle(tmp_path):
    """Pickling Compatibility."""
    root = Node(name="root")
    a = Node(name="a", parent=root)
    b = Node(name="b", parent=a)
    c = SymlinkNode(target=b, parent=a)

    lines = str(RenderTree(root)).splitlines()
    assert lines == [
        "Node('/root')",
        "└── Node('/root/a')",
        "    ├── Node('/root/a/b')",
        "    └── SymlinkNode(Node('/root/a/b'))",
    ]

    filepath = tmp_path / "test.pkl"
    with open(filepath, "wb") as file:
        pickle.dump(root, file)

    with open(filepath, "rb") as file:
        loaded = pickle.load(file)

    assert str(RenderTree(loaded)).splitlines() == lines