1
+ # pyright: reportPrivateUsage=false
2
+
1
3
"""Unit test suite for the docx.opc.coreprops module."""
2
4
3
- from datetime import datetime
5
+ from __future__ import annotations
6
+
7
+ import datetime as dt
8
+ from typing import TYPE_CHECKING , cast
4
9
5
10
import pytest
6
11
7
12
from docx .opc .coreprops import CoreProperties
8
13
from docx .oxml .parser import parse_xml
9
14
15
+ if TYPE_CHECKING :
16
+ from docx .oxml .coreprops import CT_CoreProperties
17
+
10
18
11
19
class DescribeCoreProperties :
12
- def it_knows_the_string_property_values (self , text_prop_get_fixture ):
13
- core_properties , prop_name , expected_value = text_prop_get_fixture
20
+ """Unit-test suite for `docx.opc.coreprops.CoreProperties` objects."""
21
+
22
+ @pytest .mark .parametrize (
23
+ ("prop_name" , "expected_value" ),
24
+ [
25
+ ("author" , "python-docx" ),
26
+ ("category" , "" ),
27
+ ("comments" , "" ),
28
+ ("content_status" , "DRAFT" ),
29
+ ("identifier" , "GXS 10.2.1ab" ),
30
+ ("keywords" , "foo bar baz" ),
31
+ ("language" , "US-EN" ),
32
+ ("last_modified_by" , "Steve Canny" ),
33
+ ("subject" , "Spam" ),
34
+ ("title" , "Word Document" ),
35
+ ("version" , "1.2.88" ),
36
+ ],
37
+ )
38
+ def it_knows_the_string_property_values (
39
+ self , prop_name : str , expected_value : str , core_properties : CoreProperties
40
+ ):
14
41
actual_value = getattr (core_properties , prop_name )
15
42
assert actual_value == expected_value
16
43
17
- def it_can_change_the_string_property_values (self , text_prop_set_fixture ):
18
- core_properties , prop_name , value , expected_xml = text_prop_set_fixture
19
- setattr (core_properties , prop_name , value )
20
- assert core_properties ._element .xml == expected_xml
21
-
22
- def it_knows_the_date_property_values (self , date_prop_get_fixture ):
23
- core_properties , prop_name , expected_datetime = date_prop_get_fixture
24
- actual_datetime = getattr (core_properties , prop_name )
25
- assert actual_datetime == expected_datetime
44
+ @pytest .mark .parametrize (
45
+ ("prop_name" , "tagname" , "value" ),
46
+ [
47
+ ("author" , "dc:creator" , "scanny" ),
48
+ ("category" , "cp:category" , "silly stories" ),
49
+ ("comments" , "dc:description" , "Bar foo to you" ),
50
+ ("content_status" , "cp:contentStatus" , "FINAL" ),
51
+ ("identifier" , "dc:identifier" , "GT 5.2.xab" ),
52
+ ("keywords" , "cp:keywords" , "dog cat moo" ),
53
+ ("language" , "dc:language" , "GB-EN" ),
54
+ ("last_modified_by" , "cp:lastModifiedBy" , "Billy Bob" ),
55
+ ("subject" , "dc:subject" , "Eggs" ),
56
+ ("title" , "dc:title" , "Dissertation" ),
57
+ ("version" , "cp:version" , "81.2.8" ),
58
+ ],
59
+ )
60
+ def it_can_change_the_string_property_values (self , prop_name : str , tagname : str , value : str ):
61
+ coreProperties = self .coreProperties (tagname = "" , str_val = "" )
62
+ core_properties = CoreProperties (cast ("CT_CoreProperties" , parse_xml (coreProperties )))
26
63
27
- def it_can_change_the_date_property_values (self , date_prop_set_fixture ):
28
- core_properties , prop_name , value , expected_xml = date_prop_set_fixture
29
64
setattr (core_properties , prop_name , value )
30
- assert core_properties ._element .xml == expected_xml
31
-
32
- def it_knows_the_revision_number (self , revision_get_fixture ):
33
- core_properties , expected_revision = revision_get_fixture
34
- assert core_properties .revision == expected_revision
35
-
36
- def it_can_change_the_revision_number (self , revision_set_fixture ):
37
- core_properties , revision , expected_xml = revision_set_fixture
38
- core_properties .revision = revision
39
- assert core_properties ._element .xml == expected_xml
40
65
41
- # fixtures -------------------------------------------------------
66
+ assert core_properties . _element . xml == self . coreProperties ( tagname , value )
42
67
43
- @pytest .fixture (
44
- params = [
45
- ("created" , datetime (2012 , 11 , 17 , 16 , 37 , 40 )),
46
- ("last_printed" , datetime (2014 , 6 , 4 , 4 , 28 )),
68
+ @pytest .mark .parametrize (
69
+ ("prop_name" , "expected_datetime" ),
70
+ [
71
+ ("created" , dt .datetime (2012 , 11 , 17 , 16 , 37 , 40 )),
72
+ ("last_printed" , dt .datetime (2014 , 6 , 4 , 4 , 28 )),
47
73
("modified" , None ),
48
- ]
74
+ ],
49
75
)
50
- def date_prop_get_fixture (self , request , core_properties ):
51
- prop_name , expected_datetime = request .param
52
- return core_properties , prop_name , expected_datetime
76
+ def it_knows_the_date_property_values (
77
+ self , prop_name : str , expected_datetime : dt .datetime , core_properties : CoreProperties
78
+ ):
79
+ actual_datetime = getattr (core_properties , prop_name )
80
+ assert actual_datetime == expected_datetime
53
81
54
- @pytest .fixture (
55
- params = [
82
+ @pytest .mark .parametrize (
83
+ ("prop_name" , "tagname" , "value" , "str_val" , "attrs" ),
84
+ [
56
85
(
57
86
"created" ,
58
87
"dcterms:created" ,
59
- datetime (2001 , 2 , 3 , 4 , 5 ),
88
+ dt . datetime (2001 , 2 , 3 , 4 , 5 ),
60
89
"2001-02-03T04:05:00Z" ,
61
90
' xsi:type="dcterms:W3CDTF"' ,
62
91
),
63
92
(
64
93
"last_printed" ,
65
94
"cp:lastPrinted" ,
66
- datetime (2014 , 6 , 4 , 4 ),
95
+ dt . datetime (2014 , 6 , 4 , 4 ),
67
96
"2014-06-04T04:00:00Z" ,
68
97
"" ,
69
98
),
70
99
(
71
100
"modified" ,
72
101
"dcterms:modified" ,
73
- datetime (2005 , 4 , 3 , 2 , 1 ),
102
+ dt . datetime (2005 , 4 , 3 , 2 , 1 ),
74
103
"2005-04-03T02:01:00Z" ,
75
104
' xsi:type="dcterms:W3CDTF"' ,
76
105
),
77
- ]
106
+ ],
78
107
)
79
- def date_prop_set_fixture (self , request ):
80
- prop_name , tagname , value , str_val , attrs = request .param
81
- coreProperties = self .coreProperties (None , None )
82
- core_properties = CoreProperties (parse_xml (coreProperties ))
108
+ def it_can_change_the_date_property_values (
109
+ self , prop_name : str , tagname : str , value : dt .datetime , str_val : str , attrs : str
110
+ ):
111
+ coreProperties = self .coreProperties (tagname = "" , str_val = "" )
112
+ core_properties = CoreProperties (cast ("CT_CoreProperties" , parse_xml (coreProperties )))
83
113
expected_xml = self .coreProperties (tagname , str_val , attrs )
84
- return core_properties , prop_name , value , expected_xml
85
114
86
- @pytest .fixture (
87
- params = [("42" , 42 ), (None , 0 ), ("foobar" , 0 ), ("-17" , 0 ), ("32.7" , 0 )]
88
- )
89
- def revision_get_fixture (self , request ):
90
- str_val , expected_revision = request .param
91
- tagname = "" if str_val is None else "cp:revision"
92
- coreProperties = self .coreProperties (tagname , str_val )
93
- core_properties = CoreProperties (parse_xml (coreProperties ))
94
- return core_properties , expected_revision
95
-
96
- @pytest .fixture (
97
- params = [
98
- (42 , "42" ),
99
- ]
115
+ setattr (core_properties , prop_name , value )
116
+
117
+ assert core_properties ._element .xml == expected_xml
118
+
119
+ @pytest .mark .parametrize (
120
+ ("str_val" , "expected_value" ),
121
+ [("42" , 42 ), (None , 0 ), ("foobar" , 0 ), ("-17" , 0 ), ("32.7" , 0 )],
100
122
)
101
- def revision_set_fixture (self , request ):
102
- value , str_val = request .param
103
- coreProperties = self .coreProperties (None , None )
104
- core_properties = CoreProperties (parse_xml (coreProperties ))
123
+ def it_knows_the_revision_number (self , str_val : str | None , expected_value : int ):
124
+ tagname , str_val = ("cp:revision" , str_val ) if str_val else ("" , "" )
125
+ coreProperties = self .coreProperties (tagname , str_val or "" )
126
+ core_properties = CoreProperties (cast ("CT_CoreProperties" , parse_xml (coreProperties )))
127
+
128
+ assert core_properties .revision == expected_value
129
+
130
+ @pytest .mark .parametrize (("value" , "str_val" ), [(42 , "42" )])
131
+ def it_can_change_the_revision_number (self , value : int , str_val : str ):
132
+ coreProperties = self .coreProperties (tagname = "" , str_val = "" )
133
+ core_properties = CoreProperties (cast ("CT_CoreProperties" , parse_xml (coreProperties )))
105
134
expected_xml = self .coreProperties ("cp:revision" , str_val )
106
- return core_properties , value , expected_xml
107
135
108
- @pytest .fixture (
109
- params = [
110
- ("author" , "python-docx" ),
111
- ("category" , "" ),
112
- ("comments" , "" ),
113
- ("content_status" , "DRAFT" ),
114
- ("identifier" , "GXS 10.2.1ab" ),
115
- ("keywords" , "foo bar baz" ),
116
- ("language" , "US-EN" ),
117
- ("last_modified_by" , "Steve Canny" ),
118
- ("subject" , "Spam" ),
119
- ("title" , "Word Document" ),
120
- ("version" , "1.2.88" ),
121
- ]
122
- )
123
- def text_prop_get_fixture (self , request , core_properties ):
124
- prop_name , expected_value = request .param
125
- return core_properties , prop_name , expected_value
136
+ core_properties .revision = value
126
137
127
- @pytest .fixture (
128
- params = [
129
- ("author" , "dc:creator" , "scanny" ),
130
- ("category" , "cp:category" , "silly stories" ),
131
- ("comments" , "dc:description" , "Bar foo to you" ),
132
- ("content_status" , "cp:contentStatus" , "FINAL" ),
133
- ("identifier" , "dc:identifier" , "GT 5.2.xab" ),
134
- ("keywords" , "cp:keywords" , "dog cat moo" ),
135
- ("language" , "dc:language" , "GB-EN" ),
136
- ("last_modified_by" , "cp:lastModifiedBy" , "Billy Bob" ),
137
- ("subject" , "dc:subject" , "Eggs" ),
138
- ("title" , "dc:title" , "Dissertation" ),
139
- ("version" , "cp:version" , "81.2.8" ),
140
- ]
141
- )
142
- def text_prop_set_fixture (self , request ):
143
- prop_name , tagname , value = request .param
144
- coreProperties = self .coreProperties (None , None )
145
- core_properties = CoreProperties (parse_xml (coreProperties ))
146
- expected_xml = self .coreProperties (tagname , value )
147
- return core_properties , prop_name , value , expected_xml
138
+ assert core_properties ._element .xml == expected_xml
148
139
149
- # fixture components ---------------------------------------------
140
+ # fixtures ---------- ---------------------------------------------
150
141
151
- def coreProperties (self , tagname , str_val , attrs = "" ):
142
+ def coreProperties (self , tagname : str , str_val : str , attrs : str = "" ) -> str :
152
143
tmpl = (
153
- '<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/'
154
- 'package/2006/metadata/core-properties" xmlns:dc="http://purl.or'
155
- 'g/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype'
156
- '/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://'
157
- 'www.w3.org/2001/XMLSchema-instance">%s</cp:coreProperties>\n '
144
+ "<cp:coreProperties"
145
+ ' xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"'
146
+ ' xmlns:dc="http://purl.org/dc/elements/1.1/"'
147
+ ' xmlns:dcmitype="http://purl.org/dc/dcmitype/"'
148
+ ' xmlns:dcterms="http://purl.org/dc/terms/"'
149
+ ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
150
+ ">%s</cp:coreProperties>\n "
158
151
)
159
152
if not tagname :
160
153
child_element = ""
@@ -166,27 +159,30 @@ def coreProperties(self, tagname, str_val, attrs=""):
166
159
167
160
@pytest .fixture
168
161
def core_properties (self ):
169
- element = parse_xml (
170
- b"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
171
- b'\n <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.o'
172
- b'rg/package/2006/metadata/core-properties" xmlns:dc="http://pur'
173
- b'l.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcm'
174
- b'itype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="h'
175
- b'ttp://www.w3.org/2001/XMLSchema-instance">\n '
176
- b" <cp:contentStatus>DRAFT</cp:contentStatus>\n "
177
- b" <dc:creator>python-docx</dc:creator>\n "
178
- b' <dcterms:created xsi:type="dcterms:W3CDTF">2012-11-17T11:07:'
179
- b"40-05:30</dcterms:created>\n "
180
- b" <dc:description/>\n "
181
- b" <dc:identifier>GXS 10.2.1ab</dc:identifier>\n "
182
- b" <dc:language>US-EN</dc:language>\n "
183
- b" <cp:lastPrinted>2014-06-04T04:28:00Z</cp:lastPrinted>\n "
184
- b" <cp:keywords>foo bar baz</cp:keywords>\n "
185
- b" <cp:lastModifiedBy>Steve Canny</cp:lastModifiedBy>\n "
186
- b" <cp:revision>4</cp:revision>\n "
187
- b" <dc:subject>Spam</dc:subject>\n "
188
- b" <dc:title>Word Document</dc:title>\n "
189
- b" <cp:version>1.2.88</cp:version>\n "
190
- b"</cp:coreProperties>\n "
162
+ element = cast (
163
+ "CT_CoreProperties" ,
164
+ parse_xml (
165
+ b"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
166
+ b'\n <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.o'
167
+ b'rg/package/2006/metadata/core-properties" xmlns:dc="http://pur'
168
+ b'l.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcm'
169
+ b'itype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="h'
170
+ b'ttp://www.w3.org/2001/XMLSchema-instance">\n '
171
+ b" <cp:contentStatus>DRAFT</cp:contentStatus>\n "
172
+ b" <dc:creator>python-docx</dc:creator>\n "
173
+ b' <dcterms:created xsi:type="dcterms:W3CDTF">2012-11-17T11:07:'
174
+ b"40-05:30</dcterms:created>\n "
175
+ b" <dc:description/>\n "
176
+ b" <dc:identifier>GXS 10.2.1ab</dc:identifier>\n "
177
+ b" <dc:language>US-EN</dc:language>\n "
178
+ b" <cp:lastPrinted>2014-06-04T04:28:00Z</cp:lastPrinted>\n "
179
+ b" <cp:keywords>foo bar baz</cp:keywords>\n "
180
+ b" <cp:lastModifiedBy>Steve Canny</cp:lastModifiedBy>\n "
181
+ b" <cp:revision>4</cp:revision>\n "
182
+ b" <dc:subject>Spam</dc:subject>\n "
183
+ b" <dc:title>Word Document</dc:title>\n "
184
+ b" <cp:version>1.2.88</cp:version>\n "
185
+ b"</cp:coreProperties>\n "
186
+ ),
191
187
)
192
188
return CoreProperties (element )
0 commit comments