Skip to content

Commit 2853e3d

Browse files
author
Steve Canny
committed
opc: add reltype to PartFactory.__new__()
1 parent 92938c0 commit 2853e3d

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

docx/opc/package.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class PartFactory(object):
322322
part_type_for = {}
323323
default_part_type = Part
324324

325-
def __new__(cls, partname, content_type, blob, package):
325+
def __new__(cls, partname, content_type, reltype, blob, package):
326326
PartClass = cls._part_cls_for(content_type)
327327
return PartClass.load(partname, content_type, blob, package)
328328

@@ -488,7 +488,7 @@ def _unmarshal_parts(pkg_reader, package, part_factory):
488488
parts = {}
489489
for partname, content_type, reltype, blob in pkg_reader.iter_sparts():
490490
parts[partname] = part_factory(
491-
partname, content_type, blob, package
491+
partname, content_type, reltype, blob, package
492492
)
493493
return parts
494494

tests/opc/test_package.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
)
1919
from docx.opc.pkgreader import PackageReader
2020

21-
# from ..oxml.unitdata.text import an_hlinkClick, an_rPr
2221
from ..unitutil import (
2322
cls_attr_mock, class_mock, instance_mock, loose_mock, method_mock
2423
)
@@ -385,10 +384,10 @@ class DescribePartFactory(object):
385384
def it_constructs_custom_part_type_for_registered_content_types(
386385
self, part_args_, CustomPartClass_, part_of_custom_type_):
387386
# fixture ----------------------
388-
partname, content_type, pkg, blob = part_args_
387+
partname, content_type, reltype, pkg, blob = part_args_
389388
# exercise ---------------------
390389
PartFactory.part_type_for[content_type] = CustomPartClass_
391-
part = PartFactory(partname, content_type, pkg, blob)
390+
part = PartFactory(partname, content_type, reltype, pkg, blob)
392391
# verify -----------------------
393392
CustomPartClass_.load.assert_called_once_with(
394393
partname, content_type, pkg, blob
@@ -397,8 +396,8 @@ def it_constructs_custom_part_type_for_registered_content_types(
397396

398397
def it_constructs_part_using_default_class_when_no_custom_registered(
399398
self, part_args_2_, DefaultPartClass_, part_of_default_type_):
400-
partname, content_type, pkg, blob = part_args_2_
401-
part = PartFactory(partname, content_type, pkg, blob)
399+
partname, content_type, reltype, pkg, blob = part_args_2_
400+
part = PartFactory(partname, content_type, reltype, pkg, blob)
402401
DefaultPartClass_.load.assert_called_once_with(
403402
partname, content_type, pkg, blob
404403
)
@@ -432,17 +431,19 @@ def DefaultPartClass_(self, request, part_of_default_type_):
432431
def part_args_(self, request):
433432
partname_ = PackURI('/foo/bar.xml')
434433
content_type_ = 'content/type'
434+
reltype_ = 'reltype1'
435435
pkg_ = instance_mock(request, OpcPackage, name="pkg_")
436436
blob_ = b'blob_'
437-
return partname_, content_type_, pkg_, blob_
437+
return partname_, content_type_, reltype_, pkg_, blob_
438438

439439
@pytest.fixture
440440
def part_args_2_(self, request):
441441
partname_2_ = PackURI('/bar/foo.xml')
442442
content_type_2_ = 'foobar/type'
443+
reltype_2_ = 'reltype2'
443444
pkg_2_ = instance_mock(request, OpcPackage, name="pkg_2_")
444445
blob_2_ = b'blob_2_'
445-
return partname_2_, content_type_2_, pkg_2_, blob_2_
446+
return partname_2_, content_type_2_, reltype_2_, pkg_2_, blob_2_
446447

447448

448449
class Describe_Relationship(object):
@@ -625,10 +626,11 @@ def it_can_unmarshal_from_a_pkg_reader(
625626

626627
def it_can_unmarshal_parts(
627628
self, pkg_reader_, pkg_, part_factory_, parts_dict_, partnames_,
628-
content_types_, blobs_):
629+
content_types_, reltypes_, blobs_):
629630
# fixture ----------------------
630631
partname_, partname_2_ = partnames_
631632
content_type_, content_type_2_ = content_types_
633+
reltype_, reltype_2_ = reltypes_
632634
blob_, blob_2_ = blobs_
633635
# exercise ---------------------
634636
parts = Unmarshaller._unmarshal_parts(
@@ -637,8 +639,8 @@ def it_can_unmarshal_parts(
637639
# verify -----------------------
638640
assert (
639641
part_factory_.call_args_list == [
640-
call(partname_, content_type_, blob_, pkg_),
641-
call(partname_2_, content_type_2_, blob_2_, pkg_)
642+
call(partname_, content_type_, reltype_, blob_, pkg_),
643+
call(partname_2_, content_type_2_, reltype_2_, blob_2_, pkg_)
642644
]
643645
)
644646
assert parts == parts_dict_
@@ -751,6 +753,7 @@ def _unmarshal_relationships(self, request):
751753
return method_mock(request, Unmarshaller, '_unmarshal_relationships')
752754

753755

756+
# from ..oxml.unitdata.text import an_hlinkClick, an_rPr
754757
# from ..unitutil import (
755758
# absjoin, class_mock, cls_attr_mock, instance_mock, loose_mock,
756759
# method_mock, test_file_dir

0 commit comments

Comments
 (0)