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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
# stdlib
from textwrap import dedent
# 3rd party
import pytest
# this package
from dist_meta.metadata_mapping import MetadataEmitter, MetadataMapping
def test_instantiation():
MetadataMapping()
def test_headermapping():
h = MetadataMapping()
# Setitem
h["foo"] = "bar"
assert dict(h) == {"foo": "bar"}
h["Foo"] = "bar"
assert dict(h) == {"foo": "bar", "Foo": "bar"}
assert h.keys() == ["foo", "Foo"]
h["Foo"] = "baz"
assert dict(h) == {"foo": "bar", "Foo": "bar"}
assert h.keys() == ["foo", "Foo", "Foo"]
assert h.values() == ["bar", "bar", "baz"]
assert h.items() == [("foo", "bar"), ("Foo", "bar"), ("Foo", "baz")]
assert h.get_all("foo") == ["bar", "bar", "baz"]
assert list(iter(h)) == ["foo", "Foo", "Foo"]
assert repr(h) == "<MetadataMapping({'foo': 'bar', 'Foo': 'bar', 'Foo': 'baz'})>"
h.replace("foo", "BAR")
assert h.items() == [("foo", "BAR"), ("Foo", "bar"), ("Foo", "baz")]
assert h.get_all("foo") == ["BAR", "bar", "baz"]
h.replace("foo", "bar")
# getitem
assert h["foo"] == "bar"
assert h["Foo"] == "bar"
assert "bar" not in h
assert 42 not in h
with pytest.raises(KeyError, match="bar"):
h["bar"] # pylint: disable=pointless-statement
# len
assert len(h) == 3
# delitem
h["bar"] = "baz"
del h["Foo"]
assert dict(h) == {"bar": "baz"}
assert repr(h) == "<MetadataMapping({'bar': 'baz'})>"
assert "Foo" not in h
assert "foo" not in h
def test_get_default():
h = MetadataMapping()
assert h.get("foo", 42) == 42
assert h.get_all("foo", 42) == 42
assert h.get("foo") is None
assert h.get_all("foo") is None
def test_metadata_emitter():
h = MetadataMapping()
h["Metadata-Version"] = "2.1"
h["Name"] = "cawdrey"
h["Version"] = "0.4.2"
h["Platform"] = "Windows"
h["Platform"] = "macOS"
h["Platform"] = "Linux"
output = MetadataEmitter(h)
output.add_single("Metadata-Version")
assert str(output) == "Metadata-Version: 2.1"
output.add_single("namE")
assert str(output) == "Metadata-Version: 2.1\nnamE: cawdrey"
output.add_single("Version")
output.add_single("Platform")
assert str(output) == "Metadata-Version: 2.1\nnamE: cawdrey\nVersion: 0.4.2\nPlatform: Windows"
output.add_multiple("Platform")
assert str(output) == dedent(
"""\
Metadata-Version: 2.1
namE: cawdrey
Version: 0.4.2
Platform: Windows
Platform: Windows
Platform: macOS
Platform: Linux"""
)
output.add_body("This is the body\n\nIt can have multiple lines\n\t\tand indents")
assert str(output) == dedent(
"""\
Metadata-Version: 2.1
namE: cawdrey
Version: 0.4.2
Platform: Windows
Platform: Windows
Platform: macOS
Platform: Linux
This is the body
It can have multiple lines
\t\tand indents
"""
)
def test_replace():
msg = MetadataMapping()
msg["First"] = "One"
msg["Second"] = "Two"
msg["Third"] = "Three"
assert msg.keys() == ["First", "Second", "Third"]
assert msg.values() == ["One", "Two", "Three"]
msg.replace("Second", "Twenty")
assert msg.keys() == ["First", "Second", "Third"]
assert msg.values() == ["One", "Twenty", "Three"]
msg["First"] = "Eleven"
msg.replace("First", "One Hundred")
assert msg.keys() == ["First", "Second", "Third", "First"]
assert msg.values() == ["One Hundred", "Twenty", "Three", "Eleven"]
with pytest.raises(KeyError, match="Fourth"):
msg.replace("Fourth", "Missing")
def test_values():
msg = MetadataMapping()
msg["From"] = "foo@bar.com"
msg["To"] = "bob"
msg["Subject"] = "Hello World"
assert msg.values() == [
"foo@bar.com",
"bob",
"Hello World",
]
def test_items():
msg = MetadataMapping()
msg["From"] = "foo@bar.com"
msg["To"] = "bob"
msg["Subject"] = "Hello World"
assert msg.items() == [
("From", "foo@bar.com"),
("To", "bob"),
("Subject", "Hello World"),
]
def test_get_all():
msg = MetadataMapping()
msg["From"] = "foo@bar.com"
msg["To"] = "bob"
msg["Subject"] = "Hello World"
msg["From"] = "Alan"
assert msg.get_all("From", ["foo@bar.com", "Alan"])
|