Skip to content

Commit f83809f

Browse files
author
Steve Canny
committed
add RelationshipCollection.add_relationship()
1 parent 2a23150 commit f83809f

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

opc/package.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,21 @@ def __new__(cls, partname, content_type, blob):
107107
return Part(partname, content_type, blob)
108108

109109

110+
class _Relationship(object):
111+
"""
112+
Value object for relationship to part.
113+
"""
114+
def __init__(self, rId, reltype, target, baseURI, external=False):
115+
super(_Relationship, self).__init__()
116+
117+
110118
class RelationshipCollection(object):
111119
"""
112120
Collection object for |_Relationship| instances, having list semantics.
113121
"""
114122
def __init__(self, baseURI):
115123
super(RelationshipCollection, self).__init__()
124+
self._baseURI = baseURI
116125
self._rels = []
117126

118127
def __getitem__(self, idx):
@@ -123,6 +132,14 @@ def __len__(self):
123132
"""Implements len() built-in on this object"""
124133
return self._rels.__len__()
125134

135+
def add_relationship(self, reltype, target, rId, external=False):
136+
"""
137+
Return a newly added |_Relationship| instance.
138+
"""
139+
rel = _Relationship(rId, reltype, target, self._baseURI, external)
140+
self._rels.append(rel)
141+
return rel
142+
126143

127144
class Unmarshaller(object):
128145
"""

tests/test_package.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ def it_constructs_a_part_instance(self, Part_):
111111

112112
class DescribeRelationshipCollection(object):
113113

114+
@pytest.fixture
115+
def _Relationship_(self, request):
116+
return class_mock('opc.package._Relationship', request)
117+
114118
def it_has_a_len(self):
115119
rels = RelationshipCollection(None)
116120
assert len(rels) == 0
@@ -125,6 +129,17 @@ def it_supports_indexed_access(self):
125129
except IndexError:
126130
pass
127131

132+
def it_can_add_a_relationship(self, _Relationship_):
133+
baseURI, rId, reltype, target, external = (
134+
'baseURI', 'rId9', 'reltype', 'target', False
135+
)
136+
rels = RelationshipCollection(baseURI)
137+
rel = rels.add_relationship(reltype, target, rId, external)
138+
_Relationship_.assert_called_once_with(rId, reltype, target, baseURI,
139+
external)
140+
assert rels[0] == rel
141+
assert rel == _Relationship_.return_value
142+
128143

129144
class DescribeUnmarshaller(object):
130145

0 commit comments

Comments
 (0)