File: test_pickle.py

package info (click to toggle)
anytree 2.12.1-3.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 872 kB
  • sloc: python: 4,044; makefile: 12
file content (29 lines) | stat: -rw-r--r-- 837 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
29
# -*- coding: utf-8 -*-
import pickle

from anytree import AnyNode, LoopError, Node, NodeMixin, PostOrderIter, PreOrderIter, RenderTree, SymlinkNode, TreeError


def test_pickle(tmp_path):
    """Pickling Compatibilty."""
    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(root)).splitlines() == lines