Skip to content

Commit 1d6cf3f

Browse files
author
Steve Canny
committed
add Unmarshaller._unmarshal_parts()
1 parent b8edb94 commit 1d6cf3f

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

opc/package.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def _unmarshal_parts(pkg_reader, part_factory):
6363
*pkg_reader*, keyed by partname. Side-effect is that each part in
6464
*pkg_reader* is constructed using *part_factory*.
6565
"""
66+
parts = {}
67+
for partname, content_type, blob in pkg_reader.iter_sparts():
68+
parts[partname] = part_factory(partname, content_type, blob)
69+
return parts
6670

6771
@staticmethod
6872
def _unmarshal_relationships(pkg_reader, pkg, parts):

tests/test_package.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import pytest
1313

14-
from mock import Mock
14+
from mock import call, Mock
1515

1616
from opc.package import OpcPackage, Unmarshaller
1717

@@ -72,3 +72,25 @@ def it_can_unmarshal_from_a_pkg_reader(self, _unmarshal_parts,
7272
parts)
7373
for part in parts.values():
7474
part._after_unmarshal.assert_called_once_with()
75+
76+
def it_can_unmarshal_parts(self):
77+
# test data --------------------
78+
part_properties = (
79+
('/part/name1.xml', 'app/vnd.contentType_A', '<Part_1/>'),
80+
('/part/name2.xml', 'app/vnd.contentType_B', '<Part_2/>'),
81+
('/part/name3.xml', 'app/vnd.contentType_C', '<Part_3/>'),
82+
)
83+
# mockery ----------------------
84+
pkg_reader = Mock(name='pkg_reader')
85+
pkg_reader.iter_sparts.return_value = part_properties
86+
part_factory = Mock(name='part_factory')
87+
parts = [Mock(name='part1'), Mock(name='part2'), Mock(name='part3')]
88+
part_factory.side_effect = parts
89+
# exercise ---------------------
90+
retval = Unmarshaller._unmarshal_parts(pkg_reader, part_factory)
91+
# verify -----------------------
92+
expected_calls = [call(*p) for p in part_properties]
93+
expected_parts = dict((p[0], parts[idx]) for (idx, p) in
94+
enumerate(part_properties))
95+
assert part_factory.call_args_list == expected_calls
96+
assert retval == expected_parts

0 commit comments

Comments
 (0)