Skip to content

Commit 5be946c

Browse files
author
Steve Canny
committed
add _SerializedRelationshipCollctn.load_from_xml()
1 parent e7289f1 commit 5be946c

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

opc/pkgreader.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
(OPC) package.
1313
"""
1414

15+
from opc.oxml import oxml_fromstring
1516
from opc.packuri import PACKAGE_URI
1617
from opc.phys_pkg import PhysPkgReader
1718

@@ -116,15 +117,33 @@ def __init__(self, partname, content_type, blob, srels):
116117
super(_SerializedPart, self).__init__()
117118

118119

120+
class _SerializedRelationship(object):
121+
"""
122+
Value object representing a serialized relationship in an OPC package.
123+
Serialized, in this case, means any target part is referred to via its
124+
partname rather than a direct link to an in-memory |Part| object.
125+
"""
126+
127+
119128
class _SerializedRelationshipCollection(object):
120129
"""
121130
Read-only sequence of |_SerializedRelationship| instances corresponding
122131
to the relationships item XML passed to constructor.
123132
"""
133+
def __init__(self):
134+
super(_SerializedRelationshipCollection, self).__init__()
135+
self._srels = []
136+
124137
@staticmethod
125138
def load_from_xml(baseURI, rels_item_xml):
126139
"""
127140
Return |_SerializedRelationshipCollection| instance loaded with the
128141
relationships contained in *rels_item_xml*. Returns an empty
129142
collection if *rels_item_xml* is |None|.
130143
"""
144+
srels = _SerializedRelationshipCollection()
145+
if rels_item_xml is not None:
146+
rels_elm = oxml_fromstring(rels_item_xml)
147+
for rel_elm in rels_elm.Relationship:
148+
srels._srels.append(_SerializedRelationship(baseURI, rel_elm))
149+
return srels

tests/test_pkgreader.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
from mock import call, Mock, patch
1515

1616
from opc.phys_pkg import ZipPkgReader
17-
from opc.pkgreader import _ContentTypeMap, PackageReader
17+
from opc.pkgreader import (
18+
_ContentTypeMap, PackageReader, _SerializedRelationshipCollection
19+
)
1820

1921
from .unitutil import class_mock, initializer_mock, method_mock
2022

@@ -175,3 +177,36 @@ def it_can_retrieve_srels_for_a_source_uri(
175177
phys_reader.rels_xml_for.assert_called_once_with(source_uri)
176178
load_from_xml.assert_called_once_with(source_uri.baseURI, rels_xml)
177179
assert retval == srels
180+
181+
182+
class Describe_SerializedRelationshipCollection(object):
183+
184+
@pytest.fixture
185+
def oxml_fromstring(self, request):
186+
_patch = patch('opc.pkgreader.oxml_fromstring')
187+
request.addfinalizer(_patch.stop)
188+
return _patch.start()
189+
190+
@pytest.fixture
191+
def _SerializedRelationship_(self, request):
192+
return class_mock('opc.pkgreader._SerializedRelationship', request)
193+
194+
def it_can_load_from_xml(self, oxml_fromstring, _SerializedRelationship_):
195+
# mockery ----------------------
196+
baseURI, rels_item_xml, rel_elm_1, rel_elm_2 = (
197+
Mock(name='baseURI'), Mock(name='rels_item_xml'),
198+
Mock(name='rel_elm_1'), Mock(name='rel_elm_2'),
199+
)
200+
rels_elm = Mock(name='rels_elm', Relationship=[rel_elm_1, rel_elm_2])
201+
oxml_fromstring.return_value = rels_elm
202+
# exercise ---------------------
203+
srels = _SerializedRelationshipCollection.load_from_xml(
204+
baseURI, rels_item_xml)
205+
# verify -----------------------
206+
expected_calls = [
207+
call(baseURI, rel_elm_1),
208+
call(baseURI, rel_elm_2),
209+
]
210+
oxml_fromstring.assert_called_once_with(rels_item_xml)
211+
assert _SerializedRelationship_.call_args_list == expected_calls
212+
assert isinstance(srels, _SerializedRelationshipCollection)

0 commit comments

Comments
 (0)