def __reltype_ordering_mock(self):
     """
     Return RelationshipCollection instance with mocked-up contents
     suitable for testing _reltype_ordering.
     """
     # setup ------------------------
     partnames = ['/ppt/slides/slide4.xml',
                  '/ppt/slideLayouts/slideLayout1.xml',
                  '/ppt/slideMasters/slideMaster1.xml',
                  '/ppt/slides/slide1.xml',
                  '/ppt/presProps.xml']
     part1 = Mock(name='part1')
     part1.partname = partnames[0]
     part2 = Mock(name='part2')
     part2.partname = partnames[1]
     part3 = Mock(name='part3')
     part3.partname = partnames[2]
     part4 = Mock(name='part4')
     part4.partname = partnames[3]
     part5 = Mock(name='part5')
     part5.partname = partnames[4]
     rel1 = _Relationship('rId1', RT_SLIDE,        part1)
     rel2 = _Relationship('rId2', RT_SLIDE_LAYOUT, part2)
     rel3 = _Relationship('rId3', RT_SLIDE_MASTER, part3)
     rel4 = _Relationship('rId4', RT_SLIDE,        part4)
     rel5 = _Relationship('rId5', RT_PRES_PROPS,   part5)
     relationships = _RelationshipCollection()
     relationships._additem(rel1)
     relationships._additem(rel2)
     relationships._additem(rel3)
     relationships._additem(rel4)
     relationships._additem(rel5)
     return (relationships, partnames)
 def test__next_rId_fills_gap(self):
     """_RelationshipCollection._next_rId fills gap in rId sequence"""
     # setup ------------------------
     part1 = _BasePart()
     part2 = _BasePart()
     part3 = _BasePart()
     part4 = _BasePart()
     rel1 = _Relationship('rId1', None, part1)
     rel2 = _Relationship('rId2', None, part2)
     rel3 = _Relationship('rId3', None, part3)
     rel4 = _Relationship('rId4', None, part4)
     cases = (('rId1', (rel2, rel3, rel4)),
              ('rId2', (rel1, rel3, rel4)),
              ('rId3', (rel1, rel2, rel4)),
              ('rId4', (rel1, rel2, rel3)))
     # exercise ---------------------
     expected_rIds = []
     actual_rIds = []
     for expected_rId, rels in cases:
         expected_rIds.append(expected_rId)
         relationships = _RelationshipCollection()
         for rel in rels:
             relationships._additem(rel)
         actual_rIds.append(relationships._next_rId)
     # verify -----------------------
     expected = expected_rIds
     actual = actual_rIds
     msg = "expected rIds %s, got %s" % (expected, actual)
     self.assertEqual(expected, actual, msg)
 def test__additem_raises_on_dup_rId(self):
     """_RelationshipCollection._additem raises on duplicate rId"""
     # setup ------------------------
     part1 = _BasePart()
     part2 = _BasePart()
     rel1 = _Relationship('rId9', None, part1)
     rel2 = _Relationship('rId9', None, part2)
     self.relationships._additem(rel1)
     # verify -----------------------
     with self.assertRaises(ValueError):
         self.relationships._additem(rel2)
 def test__num_value(self):
     """_Relationship._num value is correct"""
     # setup ------------------------
     num = 91
     rId = 'rId%d' % num
     rel = _Relationship(rId, None, None)
     # verify -----------------------
     assert_that(rel._num, is_(equal_to(num)))
 def with_tuple_targets(self, count, reltype):
     for i in range(count):
         rId = self.__next_rId
         partname = self.__next_tuple_partname(reltype)
         target = PartBuilder().with_partname(partname).build()
         rel = _Relationship(rId, reltype, target)
         self.relationships.append(rel)
     return self
 def test__additem_maintains_rId_ordering(self):
     """_RelationshipCollection maintains rId ordering on additem()"""
     # setup ------------------------
     part1 = _BasePart()
     part2 = _BasePart()
     part3 = _BasePart()
     rel1 = _Relationship('rId1', None, part1)
     rel2 = _Relationship('rId2', None, part2)
     rel3 = _Relationship('rId3', None, part3)
     # exercise ---------------------
     self.relationships._additem(rel2)
     self.relationships._additem(rel1)
     self.relationships._additem(rel3)
     # verify -----------------------
     expected = ['rId1', 'rId2', 'rId3']
     actual = [rel._rId for rel in self.relationships]
     msg = "expected ordering %s, got %s" % (expected, actual)
     self.assertEqual(expected, actual, msg)
 def test_reorders_on_partname_change(self):
     """RelationshipCollection reorders on partname change"""
     # setup ------------------------
     partname1 = '/ppt/slides/slide1.xml'
     partname2 = '/ppt/slides/slide2.xml'
     partname3 = '/ppt/slides/slide3.xml'
     part1 = PartBuilder().with_partname(partname1).build()
     part2 = PartBuilder().with_partname(partname2).build()
     rel1 = _Relationship('rId1', RT_SLIDE, part1)
     rel2 = _Relationship('rId2', RT_SLIDE, part2)
     relationships = _RelationshipCollection()
     relationships._reltype_ordering = (RT_SLIDE)
     relationships._additem(rel1)
     relationships._additem(rel2)
     # exercise ---------------------
     part1.partname = partname3
     # verify -----------------------
     expected = [partname2, partname3]
     actual = [rel._target.partname for rel in relationships]
     msg = "expected ordering %s, got %s" % (expected, actual)
     self.assertEqual(expected, actual, msg)
 def test_it_can_find_related_part(self):
     """_RelationshipCollection can find related part"""
     # setup ------------------------
     reltype = RT_CORE_PROPS
     part = Mock(name='part')
     relationship = _Relationship('rId1', reltype, part)
     relationships = _RelationshipCollection()
     relationships._additem(relationship)
     # exercise ---------------------
     retval = relationships.related_part(reltype)
     # verify -----------------------
     assert_that(retval, same_instance(part))
 def test__additem_maintains_reltype_ordering(self):
     """_RelationshipCollection maintains reltype ordering on additem()"""
     # setup ------------------------
     relationships, partnames = self.__reltype_ordering_mock()
     ordering = (RT_SLIDE_MASTER, RT_SLIDE_LAYOUT, RT_SLIDE)
     relationships._reltype_ordering = ordering
     partname = '/ppt/slides/slide2.xml'
     part = Mock(name='new_part')
     part.partname = partname
     rId = relationships._next_rId
     rel = _Relationship(rId, RT_SLIDE, part)
     # exercise ---------------------
     relationships._additem(rel)
     # verify ordering -------------
     expected = [partnames[2], partnames[1], partnames[3],
                 partname, partnames[0], partnames[4]]
     actual = [r._target.partname for r in relationships]
     msg = "expected ordering %s, got %s" % (expected, actual)
     self.assertEqual(expected, actual, msg)
 def test__num_value_on_non_standard_rId(self):
     """_Relationship._num value is correct for non-standard rId"""
     # setup ------------------------
     rel = _Relationship('rIdSm', None, None)
     # verify -----------------------
     assert_that(rel._num, is_(equal_to(9999)))
 def test_constructor_raises_on_bad_rId(self):
     """_Relationship constructor raises on non-standard rId"""
     with self.assertRaises(AssertionError):
         _Relationship('Non-std14', None, None)
 def setUp(self):
     rId = 'rId1'
     reltype = RT_SLIDE
     target_part = None
     self.rel = _Relationship(rId, reltype, target_part)