Skip to content

Commit b8edb94

Browse files
author
Steve Canny
committed
add Unmarshaller.unmarshal()
1 parent 74fab08 commit b8edb94

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

opc/package.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,23 @@ def unmarshal(pkg_reader, pkg, part_factory):
5151
contents of *pkg_reader*, delegating construction of each part to
5252
*part_factory*. Package relationships are added to *pkg*.
5353
"""
54+
parts = Unmarshaller._unmarshal_parts(pkg_reader, part_factory)
55+
Unmarshaller._unmarshal_relationships(pkg_reader, pkg, parts)
56+
for part in parts.values():
57+
part._after_unmarshal()
58+
59+
@staticmethod
60+
def _unmarshal_parts(pkg_reader, part_factory):
61+
"""
62+
Return a dictionary of |Part| instances unmarshalled from
63+
*pkg_reader*, keyed by partname. Side-effect is that each part in
64+
*pkg_reader* is constructed using *part_factory*.
65+
"""
66+
67+
@staticmethod
68+
def _unmarshal_relationships(pkg_reader, pkg, parts):
69+
"""
70+
Add a relationship to the source object corresponding to each of the
71+
relationships in *pkg_reader* with its target_part set to the actual
72+
target part in *parts*.
73+
"""

tests/test_package.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
from mock import Mock
1515

16-
from opc.package import OpcPackage
16+
from opc.package import OpcPackage, Unmarshaller
1717

18-
from .unitutil import class_mock
18+
from .unitutil import class_mock, method_mock
1919

2020

2121
class DescribeOpcPackage(object):
@@ -44,3 +44,31 @@ def it_can_open_a_pkg_file(self, PackageReader_, PartFactory_,
4444
Unmarshaller_.unmarshal.assert_called_once_with(pkg_reader, pkg,
4545
PartFactory_)
4646
assert isinstance(pkg, OpcPackage)
47+
48+
49+
class DescribeUnmarshaller(object):
50+
51+
@pytest.fixture
52+
def _unmarshal_parts(self, request):
53+
return method_mock(Unmarshaller, '_unmarshal_parts', request)
54+
55+
@pytest.fixture
56+
def _unmarshal_relationships(self, request):
57+
return method_mock(Unmarshaller, '_unmarshal_relationships', request)
58+
59+
def it_can_unmarshal_from_a_pkg_reader(self, _unmarshal_parts,
60+
_unmarshal_relationships):
61+
# mockery ----------------------
62+
pkg = Mock(name='pkg')
63+
pkg_reader = Mock(name='pkg_reader')
64+
part_factory = Mock(name='part_factory')
65+
parts = {1: Mock(name='part_1'), 2: Mock(name='part_2')}
66+
_unmarshal_parts.return_value = parts
67+
# exercise ---------------------
68+
Unmarshaller.unmarshal(pkg_reader, pkg, part_factory)
69+
# verify -----------------------
70+
_unmarshal_parts.assert_called_once_with(pkg_reader, part_factory)
71+
_unmarshal_relationships.assert_called_once_with(pkg_reader, pkg,
72+
parts)
73+
for part in parts.values():
74+
part._after_unmarshal.assert_called_once_with()

0 commit comments

Comments
 (0)