<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import pytest <EOL> from pptx . opc . constants import RELATIONSHIP_TYPE as RT <EOL> from pptx . opc . oxml import CT_Relationships <EOL> from pptx . opc . package import Part , _Relationship , RelationshipCollection <EOL> from pptx . opc . packuri import PackURI <EOL> from . . unitutil . mock import ( <EOL> call , class_mock , instance_mock , loose_mock , Mock , patch , PropertyMock <EOL> ) <EOL> class Describe_Relationship ( object ) : <EOL> def it_remembers_construction_values ( self ) : <EOL> rId = '<STR_LIT>' <EOL> reltype = '<STR_LIT>' <EOL> target = Mock ( name = '<STR_LIT>' ) <EOL> external = False <EOL> rel = _Relationship ( rId , reltype , target , None , external ) <EOL> assert rel . rId == rId <EOL> assert rel . reltype == reltype <EOL> assert rel . target_part == target <EOL> assert rel . is_external == external <EOL> def it_should_raise_on_target_part_access_on_external_rel ( self ) : <EOL> rel = _Relationship ( None , None , None , None , external = True ) <EOL> with pytest . raises ( ValueError ) : <EOL> rel . target_part <EOL> def it_should_have_target_ref_for_external_rel ( self ) : <EOL> rel = _Relationship ( None , None , '<STR_LIT:target>' , None , external = True ) <EOL> assert rel . target_ref == '<STR_LIT:target>' <EOL> def it_should_have_relative_ref_for_internal_rel ( self ) : <EOL> """<STR_LIT>""" <EOL> part = Mock ( name = '<STR_LIT>' , partname = PackURI ( '<STR_LIT>' ) ) <EOL> baseURI = '<STR_LIT>' <EOL> rel = _Relationship ( None , None , part , baseURI ) <EOL> assert rel . target_ref == '<STR_LIT>' <EOL> class DescribeRelationshipCollection ( object ) : <EOL> def it_also_has_dict_style_get_rel_by_rId ( self , rels_with_known_rel ) : <EOL> rels , rId , known_rel = rels_with_known_rel <EOL> assert rels [ rId ] == known_rel <EOL> def it_should_raise_on_failed_lookup_by_rId ( self , rels ) : <EOL> with pytest . raises ( KeyError ) : <EOL> rels [ '<STR_LIT>' ] <EOL> def it_has_a_len ( self , rels ) : <EOL> assert len ( rels ) == <NUM_LIT:0> <EOL> def it_can_add_a_relationship ( self , _Relationship_ ) : <EOL> baseURI , rId , reltype , target , is_external = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:target>' , False <EOL> ) <EOL> rels = RelationshipCollection ( baseURI ) <EOL> rel = rels . add_relationship ( reltype , target , rId , is_external ) <EOL> _Relationship_ . assert_called_once_with ( <EOL> rId , reltype , target , baseURI , is_external <EOL> ) <EOL> assert rels [ rId ] == rel <EOL> assert rel == _Relationship_ . return_value <EOL> def it_can_add_a_relationship_if_not_found ( <EOL> self , rels_with_matching_rel_ , rels_with_missing_rel_ ) : <EOL> rels , reltype , part , matching_rel = rels_with_matching_rel_ <EOL> assert rels . get_or_add ( reltype , part ) == matching_rel <EOL> rels , reltype , part , new_rel = rels_with_missing_rel_ <EOL> assert rels . get_or_add ( reltype , part ) == new_rel <EOL> def it_knows_the_next_available_rId ( self , rels_with_rId_gap ) : <EOL> rels , expected_next_rId = rels_with_rId_gap <EOL> next_rId = rels . _next_rId <EOL> assert next_rId == expected_next_rId <EOL> def it_can_find_a_related_part_by_reltype ( <EOL> self , rels_with_target_known_by_reltype ) : <EOL> rels , reltype , known_target_part = rels_with_target_known_by_reltype <EOL> part = rels . part_with_reltype ( reltype ) <EOL> assert part is known_target_part <EOL> def it_can_find_a_related_part_by_rId ( self , rels_with_known_target_part ) : <EOL> rels , rId , known_target_part = rels_with_known_target_part <EOL> part = rels . related_parts [ rId ] <EOL> assert part is known_target_part <EOL> def it_raises_KeyError_on_part_with_rId_not_found ( self , rels ) : <EOL> with pytest . raises ( KeyError ) : <EOL> rels . related_parts [ '<STR_LIT>' ] <EOL> def it_can_compose_rels_xml ( self , rels_with_known_rels , rels_elm ) : <EOL> rels_with_known_rels . xml <EOL> rels_elm . assert_has_calls ( <EOL> [ <EOL> call . add_rel ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , True <EOL> ) , <EOL> call . add_rel ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , False <EOL> ) , <EOL> call . xml ( ) <EOL> ] , <EOL> any_order = True <EOL> ) <EOL> @ pytest . fixture <EOL> def _Relationship_ ( self , request ) : <EOL> return class_mock ( request , '<STR_LIT>' ) <EOL> @ pytest . fixture <EOL> def rel ( self , _rId , _reltype , _target_part , _baseURI ) : <EOL> return _Relationship ( _rId , _reltype , _target_part , _baseURI ) <EOL> @ pytest . fixture <EOL> def rels ( self , _baseURI ) : <EOL> return RelationshipCollection ( _baseURI ) <EOL> @ pytest . fixture <EOL> def rels_elm ( self , request ) : <EOL> """<STR_LIT>""" <EOL> rels_elm = Mock ( name = '<STR_LIT>' ) <EOL> xml = PropertyMock ( name = '<STR_LIT>' ) <EOL> type ( rels_elm ) . xml = xml <EOL> rels_elm . attach_mock ( xml , '<STR_LIT>' ) <EOL> rels_elm . reset_mock ( ) <EOL> patch_ = patch . object ( CT_Relationships , '<STR_LIT>' , return_value = rels_elm ) <EOL> patch_ . start ( ) <EOL> request . addfinalizer ( patch_ . stop ) <EOL> return rels_elm <EOL> @ pytest . fixture <EOL> def rels_with_known_rel ( self , rels , _rId , rel ) : <EOL> rels [ _rId ] = rel <EOL> return rels , _rId , rel <EOL> @ pytest . fixture <EOL> def rels_with_known_rels ( self ) : <EOL> """<STR_LIT>""" <EOL> rels = RelationshipCollection ( '<STR_LIT>' ) <EOL> rels . add_relationship ( <EOL> reltype = '<STR_LIT>' , target = '<STR_LIT>' , <EOL> rId = '<STR_LIT>' , is_external = True <EOL> ) <EOL> part = Mock ( name = '<STR_LIT>' ) <EOL> part . partname . relative_ref . return_value = '<STR_LIT>' <EOL> rels . add_relationship ( reltype = '<STR_LIT>' , target = part , <EOL> rId = '<STR_LIT>' ) <EOL> return rels <EOL> @ pytest . fixture <EOL> def rels_with_known_target_part ( self , rels , _rel_with_known_target_part ) : <EOL> rel , rId , target_part = _rel_with_known_target_part <EOL> rels . add_relationship ( None , target_part , rId ) <EOL> return rels , rId , target_part <EOL> @ pytest . fixture <EOL> def rels_with_matching_rel_ ( self , request , rels ) : <EOL> matching_reltype_ = instance_mock ( <EOL> request , str , name = '<STR_LIT>' <EOL> ) <EOL> matching_part_ = instance_mock ( <EOL> request , Part , name = '<STR_LIT>' <EOL> ) <EOL> matching_rel_ = instance_mock ( <EOL> request , _Relationship , name = '<STR_LIT>' , <EOL> reltype = matching_reltype_ , target_part = matching_part_ , <EOL> is_external = False <EOL> ) <EOL> rels [ <NUM_LIT:1> ] = matching_rel_ <EOL> return rels , matching_reltype_ , matching_part_ , matching_rel_ <EOL> @ pytest . fixture <EOL> def rels_with_missing_rel_ ( self , request , rels , _Relationship_ ) : <EOL> missing_reltype_ = instance_mock ( <EOL> request , str , name = '<STR_LIT>' <EOL> ) <EOL> missing_part_ = instance_mock ( <EOL> request , Part , name = '<STR_LIT>' <EOL> ) <EOL> new_rel_ = instance_mock ( <EOL> request , _Relationship , name = '<STR_LIT>' , <EOL> reltype = missing_reltype_ , target_part = missing_part_ , <EOL> is_external = False <EOL> ) <EOL> _Relationship_ . return_value = new_rel_ <EOL> return rels , missing_reltype_ , missing_part_ , new_rel_ <EOL> @ pytest . fixture <EOL> def rels_with_rId_gap ( self , request , rels ) : <EOL> rel_with_rId1 = instance_mock ( <EOL> request , _Relationship , name = '<STR_LIT>' , rId = '<STR_LIT>' <EOL> ) <EOL> rel_with_rId3 = instance_mock ( <EOL> request , _Relationship , name = '<STR_LIT>' , rId = '<STR_LIT>' <EOL> ) <EOL> rels [ '<STR_LIT>' ] = rel_with_rId1 <EOL> rels [ '<STR_LIT>' ] = rel_with_rId3 <EOL> return rels , '<STR_LIT>' <EOL> @ pytest . fixture <EOL> def rels_with_target_known_by_reltype ( <EOL> self , rels , _rel_with_target_known_by_reltype ) : <EOL> rel , reltype , target_part = _rel_with_target_known_by_reltype <EOL> rels [ <NUM_LIT:1> ] = rel <EOL> return rels , reltype , target_part <EOL> @ pytest . fixture <EOL> def _baseURI ( self ) : <EOL> return '<STR_LIT>' <EOL> @ pytest . fixture <EOL> def _rel_with_known_target_part ( <EOL> self , _rId , _reltype , _target_part , _baseURI ) : <EOL> rel = _Relationship ( _rId , _reltype , _target_part , _baseURI ) <EOL> return rel , _rId , _target_part <EOL> @ pytest . fixture <EOL> def _rel_with_target_known_by_reltype ( <EOL> self , _rId , _reltype , _target_part , _baseURI ) : <EOL> rel = _Relationship ( _rId , _reltype , _target_part , _baseURI ) <EOL> return rel , _reltype , _target_part <EOL> @ pytest . fixture <EOL> def _reltype ( self ) : <EOL> return RT . SLIDE <EOL> @ pytest . fixture <EOL> def _rId ( self ) : <EOL> return '<STR_LIT>' <EOL> @ pytest . fixture <EOL> def _target_part ( self , request ) : <EOL> return loose_mock ( request ) </s>
