File: test_classes.py

package info (click to toggle)
pydot 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,820 kB
  • sloc: python: 2,727; sh: 12; makefile: 8
file content (72 lines) | stat: -rw-r--r-- 1,835 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# SPDX-FileCopyrightText: 2024 pydot contributors
#
# SPDX-License-Identifier: MIT

"""Unit testing of `pydot.classes`."""

import pytest

import pydot
from pydot.classes import FrozenDict


def test_FrozenDict_create(objdict):
    fd = FrozenDict(objdict)
    assert isinstance(fd, FrozenDict)
    assert isinstance(fd["attributes"], FrozenDict)
    assert isinstance(fd["nodes"], FrozenDict)
    assert isinstance(fd["nodes"]["3"], tuple)
    assert isinstance(fd["nodes"]["16"], tuple)

    fd2 = FrozenDict((("a", 1), ("b", 2)), obj_dict={"frozen": fd})
    assert isinstance(fd2["obj_dict"], FrozenDict)
    assert len(fd2) == 3
    assert fd == fd2["obj_dict"]["frozen"]


def test_FrozenDict_modify(objdict):
    fd = FrozenDict(objdict)

    with pytest.raises(AttributeError):
        fd.clear()

    with pytest.raises(AttributeError):
        fd["nodes"] = None

    with pytest.raises(AttributeError):
        fd["nodes"].update({"12": [{"name": "12"}]})

    with pytest.raises(AttributeError):
        fd.popitem()

    with pytest.raises(AttributeError):
        fd.pop("nodes")

    with pytest.raises(AttributeError):
        fd.setdefault("edges", None)

    with pytest.raises(AttributeError):
        del fd["nodes"]


def test_FrozenDict_compare():
    dict_in = {
        "red": "first",
        "green": "second",
        "blue": "third",
    }
    fd1 = FrozenDict(dict_in)
    fd2 = FrozenDict(dict(sorted(dict_in.items())))
    assert hash(fd1) == hash(tuple(dict_in.items()))
    assert hash(fd2) != hash(tuple(dict_in.items()))
    assert fd1 != fd2

    assert fd1 == dict_in
    assert fd2 == dict_in  # Unfortunate fallback to dict.__eq__


def test_frozendict_deprecation(objdict):
    with pytest.warns(DeprecationWarning):
        fd = pydot.frozendict(objdict)

    assert isinstance(fd, FrozenDict)