|
13 | 13 |
|
14 | 14 | from mock import call, Mock, patch, PropertyMock
|
15 | 15 |
|
| 16 | +from opc.oxml import CT_Relationships |
16 | 17 | from opc.package import (
|
17 | 18 | OpcPackage, Part, PartFactory, _Relationship, RelationshipCollection,
|
18 | 19 | Unmarshaller
|
@@ -235,6 +236,41 @@ class DescribeRelationshipCollection(object):
|
235 | 236 | def _Relationship_(self, request):
|
236 | 237 | return class_mock('opc.package._Relationship', request)
|
237 | 238 |
|
| 239 | + @pytest.fixture |
| 240 | + def rels(self): |
| 241 | + """ |
| 242 | + Populated RelationshipCollection instance that will exercise the |
| 243 | + rels.xml property. |
| 244 | + """ |
| 245 | + rels = RelationshipCollection('/baseURI') |
| 246 | + rels.add_relationship( |
| 247 | + reltype='http://rt-hyperlink', target='http://some/link', |
| 248 | + rId='rId1', external=True |
| 249 | + ) |
| 250 | + part = Mock(name='part') |
| 251 | + part.partname.relative_ref.return_value = '../media/image1.png' |
| 252 | + rels.add_relationship(reltype='http://rt-image', target=part, |
| 253 | + rId='rId2') |
| 254 | + return rels |
| 255 | + |
| 256 | + @pytest.fixture |
| 257 | + def rels_elm(self, request): |
| 258 | + """ |
| 259 | + Return a rels_elm mock that will be returned from |
| 260 | + CT_Relationships.new() |
| 261 | + """ |
| 262 | + # create rels_elm mock with a .xml property |
| 263 | + rels_elm = Mock(name='rels_elm') |
| 264 | + xml = PropertyMock(name='xml') |
| 265 | + type(rels_elm).xml = xml |
| 266 | + rels_elm.attach_mock(xml, 'xml') |
| 267 | + rels_elm.reset_mock() # to clear attach_mock call |
| 268 | + # patch CT_Relationships to return that rels_elm |
| 269 | + patch_ = patch.object(CT_Relationships, 'new', return_value=rels_elm) |
| 270 | + patch_.start() |
| 271 | + request.addfinalizer(patch_.stop) |
| 272 | + return rels_elm |
| 273 | + |
238 | 274 | def it_has_a_len(self):
|
239 | 275 | rels = RelationshipCollection(None)
|
240 | 276 | assert len(rels) == 0
|
@@ -273,6 +309,21 @@ def it_can_add_a_relationship(self, _Relationship_):
|
273 | 309 | assert rels[0] == rel
|
274 | 310 | assert rel == _Relationship_.return_value
|
275 | 311 |
|
| 312 | + def it_can_compose_rels_xml(self, rels, rels_elm): |
| 313 | + # exercise --------------------- |
| 314 | + rels.xml |
| 315 | + # trace ------------------------ |
| 316 | + print('Actual calls:\n%s' % rels_elm.mock_calls) |
| 317 | + # verify ----------------------- |
| 318 | + expected_rels_elm_calls = [ |
| 319 | + call.add_rel('rId1', 'http://rt-hyperlink', 'http://some/link', |
| 320 | + True), |
| 321 | + call.add_rel('rId2', 'http://rt-image', '../media/image1.png', |
| 322 | + False), |
| 323 | + call.xml() |
| 324 | + ] |
| 325 | + assert rels_elm.mock_calls == expected_rels_elm_calls |
| 326 | + |
276 | 327 |
|
277 | 328 | class DescribeUnmarshaller(object):
|
278 | 329 |
|
|
0 commit comments