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 184 185 186 187 188
|
# pyright: reportPrivateUsage=false
"""Unit test suite for the docx.opc.coreprops module."""
from __future__ import annotations
import datetime as dt
from typing import TYPE_CHECKING, cast
import pytest
from docx.opc.coreprops import CoreProperties
from docx.oxml.parser import parse_xml
if TYPE_CHECKING:
from docx.oxml.coreprops import CT_CoreProperties
class DescribeCoreProperties:
"""Unit-test suite for `docx.opc.coreprops.CoreProperties` objects."""
@pytest.mark.parametrize(
("prop_name", "expected_value"),
[
("author", "python-docx"),
("category", ""),
("comments", ""),
("content_status", "DRAFT"),
("identifier", "GXS 10.2.1ab"),
("keywords", "foo bar baz"),
("language", "US-EN"),
("last_modified_by", "Steve Canny"),
("subject", "Spam"),
("title", "Word Document"),
("version", "1.2.88"),
],
)
def it_knows_the_string_property_values(
self, prop_name: str, expected_value: str, core_properties: CoreProperties
):
actual_value = getattr(core_properties, prop_name)
assert actual_value == expected_value
@pytest.mark.parametrize(
("prop_name", "tagname", "value"),
[
("author", "dc:creator", "scanny"),
("category", "cp:category", "silly stories"),
("comments", "dc:description", "Bar foo to you"),
("content_status", "cp:contentStatus", "FINAL"),
("identifier", "dc:identifier", "GT 5.2.xab"),
("keywords", "cp:keywords", "dog cat moo"),
("language", "dc:language", "GB-EN"),
("last_modified_by", "cp:lastModifiedBy", "Billy Bob"),
("subject", "dc:subject", "Eggs"),
("title", "dc:title", "Dissertation"),
("version", "cp:version", "81.2.8"),
],
)
def it_can_change_the_string_property_values(self, prop_name: str, tagname: str, value: str):
coreProperties = self.coreProperties(tagname="", str_val="")
core_properties = CoreProperties(cast("CT_CoreProperties", parse_xml(coreProperties)))
setattr(core_properties, prop_name, value)
assert core_properties._element.xml == self.coreProperties(tagname, value)
@pytest.mark.parametrize(
("prop_name", "expected_datetime"),
[
("created", dt.datetime(2012, 11, 17, 16, 37, 40, tzinfo=dt.timezone.utc)),
("last_printed", dt.datetime(2014, 6, 4, 4, 28, tzinfo=dt.timezone.utc)),
("modified", None),
],
)
def it_knows_the_date_property_values(
self, prop_name: str, expected_datetime: dt.datetime, core_properties: CoreProperties
):
actual_datetime = getattr(core_properties, prop_name)
assert actual_datetime == expected_datetime
@pytest.mark.parametrize(
("prop_name", "tagname", "value", "str_val", "attrs"),
[
(
"created",
"dcterms:created",
dt.datetime(2001, 2, 3, 4, 5),
"2001-02-03T04:05:00Z",
' xsi:type="dcterms:W3CDTF"',
),
(
"last_printed",
"cp:lastPrinted",
dt.datetime(2014, 6, 4, 4),
"2014-06-04T04:00:00Z",
"",
),
(
"modified",
"dcterms:modified",
dt.datetime(2005, 4, 3, 2, 1),
"2005-04-03T02:01:00Z",
' xsi:type="dcterms:W3CDTF"',
),
],
)
def it_can_change_the_date_property_values(
self, prop_name: str, tagname: str, value: dt.datetime, str_val: str, attrs: str
):
coreProperties = self.coreProperties(tagname="", str_val="")
core_properties = CoreProperties(cast("CT_CoreProperties", parse_xml(coreProperties)))
expected_xml = self.coreProperties(tagname, str_val, attrs)
setattr(core_properties, prop_name, value)
assert core_properties._element.xml == expected_xml
@pytest.mark.parametrize(
("str_val", "expected_value"),
[("42", 42), (None, 0), ("foobar", 0), ("-17", 0), ("32.7", 0)],
)
def it_knows_the_revision_number(self, str_val: str | None, expected_value: int):
tagname, str_val = ("cp:revision", str_val) if str_val else ("", "")
coreProperties = self.coreProperties(tagname, str_val or "")
core_properties = CoreProperties(cast("CT_CoreProperties", parse_xml(coreProperties)))
assert core_properties.revision == expected_value
@pytest.mark.parametrize(("value", "str_val"), [(42, "42")])
def it_can_change_the_revision_number(self, value: int, str_val: str):
coreProperties = self.coreProperties(tagname="", str_val="")
core_properties = CoreProperties(cast("CT_CoreProperties", parse_xml(coreProperties)))
expected_xml = self.coreProperties("cp:revision", str_val)
core_properties.revision = value
assert core_properties._element.xml == expected_xml
# fixtures -------------------------------------------------------
def coreProperties(self, tagname: str, str_val: str, attrs: str = "") -> str:
tmpl = (
"<cp:coreProperties"
' xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"'
' xmlns:dc="http://purl.org/dc/elements/1.1/"'
' xmlns:dcmitype="http://purl.org/dc/dcmitype/"'
' xmlns:dcterms="http://purl.org/dc/terms/"'
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
">%s</cp:coreProperties>\n"
)
if not tagname:
child_element = ""
elif not str_val:
child_element = "\n <%s%s/>\n" % (tagname, attrs)
else:
child_element = "\n <%s%s>%s</%s>\n" % (tagname, attrs, str_val, tagname)
return tmpl % child_element
@pytest.fixture
def core_properties(self):
element = cast(
"CT_CoreProperties",
parse_xml(
b"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
b'\n<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.o'
b'rg/package/2006/metadata/core-properties" xmlns:dc="http://pur'
b'l.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcm'
b'itype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="h'
b'ttp://www.w3.org/2001/XMLSchema-instance">\n'
b" <cp:contentStatus>DRAFT</cp:contentStatus>\n"
b" <dc:creator>python-docx</dc:creator>\n"
b' <dcterms:created xsi:type="dcterms:W3CDTF">2012-11-17T11:07:'
b"40-05:30</dcterms:created>\n"
b" <dc:description/>\n"
b" <dc:identifier>GXS 10.2.1ab</dc:identifier>\n"
b" <dc:language>US-EN</dc:language>\n"
b" <cp:lastPrinted>2014-06-04T04:28:00Z</cp:lastPrinted>\n"
b" <cp:keywords>foo bar baz</cp:keywords>\n"
b" <cp:lastModifiedBy>Steve Canny</cp:lastModifiedBy>\n"
b" <cp:revision>4</cp:revision>\n"
b" <dc:subject>Spam</dc:subject>\n"
b" <dc:title>Word Document</dc:title>\n"
b" <cp:version>1.2.88</cp:version>\n"
b"</cp:coreProperties>\n"
),
)
return CoreProperties(element)
|