|
4 | 4 | Test suite for opc.pkgreader module |
5 | 5 | """ |
6 | 6 |
|
| 7 | +from __future__ import absolute_import, print_function, unicode_literals |
| 8 | + |
7 | 9 | import pytest |
8 | 10 |
|
9 | 11 | from mock import call, Mock, patch |
10 | 12 |
|
11 | | -from docx.opc.constants import RELATIONSHIP_TARGET_MODE as RTM |
| 13 | +from docx.opc.constants import ( |
| 14 | + CONTENT_TYPE as CT, RELATIONSHIP_TARGET_MODE as RTM |
| 15 | +) |
12 | 16 | from docx.opc.packuri import PackURI |
13 | 17 | from docx.opc.phys_pkg import _ZipPkgReader |
14 | 18 | from docx.opc.pkgreader import ( |
15 | 19 | _ContentTypeMap, PackageReader, _SerializedPart, _SerializedRelationship, |
16 | 20 | _SerializedRelationships |
17 | 21 | ) |
18 | 22 |
|
| 23 | +from .unitdata.types import a_Default, a_Types, an_Override |
19 | 24 | from ..unitutil import ( |
20 | 25 | initializer_mock, class_mock, function_mock, instance_mock, loose_mock, |
21 | 26 | method_mock |
@@ -258,45 +263,13 @@ def _walk_phys_parts(self, request): |
258 | 263 |
|
259 | 264 | class Describe_ContentTypeMap(object): |
260 | 265 |
|
261 | | - def it_can_construct_from_types_xml(self, oxml_fromstring_): |
262 | | - # test data -------------------- |
263 | | - content_types = ( |
264 | | - 'app/vnd.type1', 'app/vnd.type2', 'app/vnd.type3', |
265 | | - 'app/vnd.type4', |
266 | | - ) |
267 | | - content_types_xml = '<DontCare/>' |
268 | | - extensions = ('rels', 'xml') |
269 | | - exts = tuple(['.%s' % extension for extension in extensions]) |
270 | | - partnames = ('/part/name1.xml', '/part/name2.xml') |
271 | | - # mockery ---------------------- |
272 | | - overrides = ( |
273 | | - Mock(name='override_elm_1', partname=partnames[0], |
274 | | - content_type=content_types[0]), |
275 | | - Mock(name='override_elm_2', partname=partnames[1], |
276 | | - content_type=content_types[1]), |
277 | | - ) |
278 | | - defaults = ( |
279 | | - Mock(name='default_elm_1', extension=extensions[0], |
280 | | - content_type=content_types[2]), |
281 | | - Mock(name='default_elm_2', extension=extensions[1], |
282 | | - content_type=content_types[3]), |
283 | | - ) |
284 | | - types_elm = Mock( |
285 | | - name='types_elm', overrides=overrides, defaults=defaults |
| 266 | + def it_can_construct_from_ct_item_xml(self, from_xml_fixture): |
| 267 | + content_types_xml, expected_defaults, expected_overrides = ( |
| 268 | + from_xml_fixture |
286 | 269 | ) |
287 | | - oxml_fromstring_.return_value = types_elm |
288 | | - # exercise --------------------- |
289 | 270 | ct_map = _ContentTypeMap.from_xml(content_types_xml) |
290 | | - # verify ----------------------- |
291 | | - expected_overrides = { |
292 | | - partnames[0]: content_types[0], partnames[1]: content_types[1] |
293 | | - } |
294 | | - expected_defaults = { |
295 | | - exts[0]: content_types[2], exts[1]: content_types[3] |
296 | | - } |
297 | | - oxml_fromstring_.assert_called_once_with(content_types_xml) |
298 | | - assert ct_map._overrides == expected_overrides |
299 | 271 | assert ct_map._defaults == expected_defaults |
| 272 | + assert ct_map._overrides == expected_overrides |
300 | 273 |
|
301 | 274 | def it_matches_overrides(self): |
302 | 275 | # test data -------------------- |
@@ -328,8 +301,44 @@ def it_should_raise_on_key_not_instance_of_PackURI(self): |
328 | 301 | # fixtures --------------------------------------------- |
329 | 302 |
|
330 | 303 | @pytest.fixture |
331 | | - def oxml_fromstring_(self, request): |
332 | | - return function_mock(request, 'docx.opc.pkgreader.oxml_fromstring') |
| 304 | + def from_xml_fixture(self): |
| 305 | + entries = ( |
| 306 | + ('Default', 'xml', CT.XML), |
| 307 | + # ('Default', 'PNG', CT.PNG), |
| 308 | + ('Override', '/ppt/presentation.xml', CT.PML_PRESENTATION_MAIN), |
| 309 | + ) |
| 310 | + content_types_xml = self._xml_from(entries) |
| 311 | + expected_defaults = {} |
| 312 | + expected_overrides = {} |
| 313 | + for entry in entries: |
| 314 | + if entry[0] == 'Default': |
| 315 | + ext = ('.%s' % entry[1]).lower() |
| 316 | + content_type = entry[2] |
| 317 | + expected_defaults[ext] = content_type |
| 318 | + elif entry[0] == 'Override': |
| 319 | + partname, content_type = entry[1:] |
| 320 | + expected_overrides[partname] = content_type |
| 321 | + return content_types_xml, expected_defaults, expected_overrides |
| 322 | + |
| 323 | + def _xml_from(self, entries): |
| 324 | + """ |
| 325 | + Return XML for a [Content_Types].xml based on items in *entries*. |
| 326 | + """ |
| 327 | + types_bldr = a_Types().with_nsdecls() |
| 328 | + for entry in entries: |
| 329 | + if entry[0] == 'Default': |
| 330 | + ext, content_type = entry[1:] |
| 331 | + default_bldr = a_Default() |
| 332 | + default_bldr.with_Extension(ext) |
| 333 | + default_bldr.with_ContentType(content_type) |
| 334 | + types_bldr.with_child(default_bldr) |
| 335 | + elif entry[0] == 'Override': |
| 336 | + partname, content_type = entry[1:] |
| 337 | + override_bldr = an_Override() |
| 338 | + override_bldr.with_PartName(partname) |
| 339 | + override_bldr.with_ContentType(content_type) |
| 340 | + types_bldr.with_child(override_bldr) |
| 341 | + return types_bldr.xml() |
333 | 342 |
|
334 | 343 |
|
335 | 344 | class Describe_SerializedPart(object): |
|
0 commit comments