Skip to content

Commit c55a03f

Browse files
author
Steve Canny
committed
add PackageReader._srels_for()
1 parent 26c2871 commit c55a03f

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

opc/pkgreader.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def _srels_for(phys_reader, source_uri):
6767
Return |_SerializedRelationshipCollection| instance populated with
6868
relationships for source identified by *source_uri*.
6969
"""
70+
rels_xml = phys_reader.rels_xml_for(source_uri)
71+
return _SerializedRelationshipCollection.load_from_xml(
72+
source_uri.baseURI, rels_xml)
7073

7174
@staticmethod
7275
def _walk_phys_parts(phys_reader, srels, visited_partnames=None):
@@ -111,3 +114,17 @@ class _SerializedPart(object):
111114
"""
112115
def __init__(self, partname, content_type, blob, srels):
113116
super(_SerializedPart, self).__init__()
117+
118+
119+
class _SerializedRelationshipCollection(object):
120+
"""
121+
Read-only sequence of |_SerializedRelationship| instances corresponding
122+
to the relationships item XML passed to constructor.
123+
"""
124+
@staticmethod
125+
def load_from_xml(baseURI, rels_item_xml):
126+
"""
127+
Return |_SerializedRelationshipCollection| instance loaded with the
128+
relationships contained in *rels_item_xml*. Returns an empty
129+
collection if *rels_item_xml* is |None|.
130+
"""

tests/test_pkgreader.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def PhysPkgReader_(self, request):
4343
def _SerializedPart_(self, request):
4444
return class_mock('opc.pkgreader._SerializedPart', request)
4545

46+
@pytest.fixture
47+
def _SerializedRelationshipCollection_(self, request):
48+
return class_mock('opc.pkgreader._SerializedRelationshipCollection',
49+
request)
50+
4651
@pytest.fixture
4752
def _srels_for(self, request):
4853
return method_mock(PackageReader, '_srels_for', request)
@@ -155,3 +160,18 @@ def it_can_walk_phys_pkg_parts(self, _srels_for):
155160
(partname_3, part_3_blob, part_3_srels),
156161
]
157162
assert generated_tuples == expected_tuples
163+
164+
def it_can_retrieve_srels_for_a_source_uri(
165+
self, _SerializedRelationshipCollection_):
166+
# mockery ----------------------
167+
phys_reader = Mock(name='phys_reader')
168+
source_uri = Mock(name='source_uri')
169+
rels_xml = phys_reader.rels_xml_for.return_value
170+
load_from_xml = _SerializedRelationshipCollection_.load_from_xml
171+
srels = load_from_xml.return_value
172+
# exercise ---------------------
173+
retval = PackageReader._srels_for(phys_reader, source_uri)
174+
# verify -----------------------
175+
phys_reader.rels_xml_for.assert_called_once_with(source_uri)
176+
load_from_xml.assert_called_once_with(source_uri.baseURI, rels_xml)
177+
assert retval == srels

0 commit comments

Comments
 (0)