File: test_utils.py

package info (click to toggle)
python-npe2 0.7.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 828 kB
  • sloc: python: 7,088; makefile: 19
file content (127 lines) | stat: -rw-r--r-- 4,041 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pytest

from npe2.manifest.contributions import ContributionPoints
from npe2.manifest.schema import PluginManifest
from npe2.manifest.utils import (
    Version,
    deep_update,
    merge_contributions,
    merge_manifests,
)


def test_version():
    v = Version.parse(b"0.1.2")

    assert v == "0.1.2"
    assert v > {"major": 0, "minor": 1, "patch": 0}
    assert v <= (0, 2, 0)
    assert v == Version(0, 1, 2)
    assert list(v) == [0, 1, 2, None, None]
    assert str(v) == "0.1.2"

    with pytest.raises(TypeError):
        assert v == 1.2

    with pytest.raises(ValueError):
        Version.parse("alkfdjs")

    with pytest.raises(TypeError):
        Version.parse(1.2)  # type: ignore


def test_merge_manifests():
    with pytest.raises(ValueError):
        merge_manifests([])

    with pytest.raises(AssertionError) as e:
        merge_manifests([PluginManifest(name="p1"), PluginManifest(name="p2")])
    assert "All manifests must have same name" in str(e.value)

    pm1 = PluginManifest(
        name="plugin",
        contributions={
            "commands": [{"id": "plugin.command", "title": "some writer"}],
            "writers": [{"command": "plugin.command", "layer_types": ["image"]}],
        },
    )
    pm2 = PluginManifest(
        name="plugin",
        contributions={
            "commands": [{"id": "plugin.command", "title": "some reader"}],
            "readers": [{"command": "plugin.command", "filename_patterns": [".tif"]}],
        },
    )
    expected_merge = PluginManifest(
        name="plugin",
        contributions={
            "commands": [
                {"id": "plugin.command", "title": "some writer"},
                {"id": "plugin.command_2", "title": "some reader"},  # no dupes
            ],
            "writers": [{"command": "plugin.command", "layer_types": ["image"]}],
            "readers": [{"command": "plugin.command_2", "filename_patterns": [".tif"]}],
        },
    )

    assert merge_manifests([pm1]) is pm1
    assert merge_manifests([pm1, pm2]) == expected_merge


def test_merge_contributions():
    a = ContributionPoints(
        commands=[
            {"id": "plugin.command", "title": "some writer"},
        ],
        writers=[{"command": "plugin.command", "layer_types": ["image"]}],
    )
    b = ContributionPoints(
        commands=[
            {"id": "plugin.command", "title": "some writer"},
        ],
        writers=[{"command": "plugin.command", "layer_types": ["image"]}],
    )
    c = ContributionPoints(
        commands=[
            {"id": "plugin.command", "title": "some writer"},
        ],
        writers=[{"command": "plugin.command", "layer_types": ["image"]}],
    )
    expected = ContributionPoints(
        commands=[
            {"id": "plugin.command", "title": "some writer"},
            {"id": "plugin.command_2", "title": "some writer"},
            {"id": "plugin.command_3", "title": "some writer"},
        ],
        writers=[
            {"command": "plugin.command", "layer_types": ["image"]},
            {"command": "plugin.command_2", "layer_types": ["image"]},
            {"command": "plugin.command_3", "layer_types": ["image"]},
        ],
    )

    d = ContributionPoints(**merge_contributions((a, b, c)))
    assert d == expected

    # with overwrite, later contributions with matching command ids take precendence.
    e = ContributionPoints(**merge_contributions((a, b, c), overwrite=True))
    expected = ContributionPoints(
        commands=[
            {"id": "plugin.command", "title": "some writer"},
        ],
        writers=[
            {"command": "plugin.command", "layer_types": ["image"]},
        ],
    )
    assert e == a


def test_deep_update():
    a = {"a": {"b": 1, "c": 2}, "e": 2}
    b = {"a": {"d": 4, "c": 3}, "f": 0}
    c = deep_update(a, b, copy=True)
    assert c == {"a": {"b": 1, "d": 4, "c": 3}, "e": 2, "f": 0}
    assert a == {"a": {"b": 1, "c": 2}, "e": 2}

    deep_update(a, b, copy=False)
    assert a == {"a": {"b": 1, "d": 4, "c": 3}, "e": 2, "f": 0}