<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> try : <EOL> from io import BytesIO <EOL> except ImportError : <EOL> from StringIO import StringIO as BytesIO <EOL> import hashlib <EOL> import pytest <EOL> from zipfile import ZIP_DEFLATED , ZipFile <EOL> from docx . opc . exceptions import PackageNotFoundError <EOL> from docx . opc . packuri import PACKAGE_URI , PackURI <EOL> from docx . opc . phys_pkg import ( <EOL> _DirPkgReader , PhysPkgReader , PhysPkgWriter , _ZipPkgReader , _ZipPkgWriter <EOL> ) <EOL> from . . unitutil . file import absjoin , test_file_dir <EOL> from . . unitutil . mock import class_mock , loose_mock , Mock <EOL> test_docx_path = absjoin ( test_file_dir , '<STR_LIT>' ) <EOL> dir_pkg_path = absjoin ( test_file_dir , '<STR_LIT>' ) <EOL> zip_pkg_path = test_docx_path <EOL> class DescribeDirPkgReader ( object ) : <EOL> def it_is_used_by_PhysPkgReader_when_pkg_is_a_dir ( self ) : <EOL> phys_reader = PhysPkgReader ( dir_pkg_path ) <EOL> assert isinstance ( phys_reader , _DirPkgReader ) <EOL> def it_doesnt_mind_being_closed_even_though_it_doesnt_need_it ( <EOL> self , dir_reader ) : <EOL> dir_reader . close ( ) <EOL> def it_can_retrieve_the_blob_for_a_pack_uri ( self , dir_reader ) : <EOL> pack_uri = PackURI ( '<STR_LIT>' ) <EOL> blob = dir_reader . blob_for ( pack_uri ) <EOL> sha1 = hashlib . sha1 ( blob ) . hexdigest ( ) <EOL> assert sha1 == '<STR_LIT>' <EOL> def it_can_get_the_content_types_xml ( self , dir_reader ) : <EOL> sha1 = hashlib . sha1 ( dir_reader . content_types_xml ) . hexdigest ( ) <EOL> assert sha1 == '<STR_LIT>' <EOL> def it_can_retrieve_the_rels_xml_for_a_source_uri ( self , dir_reader ) : <EOL> rels_xml = dir_reader . rels_xml_for ( PACKAGE_URI ) <EOL> sha1 = hashlib . sha1 ( rels_xml ) . hexdigest ( ) <EOL> assert sha1 == '<STR_LIT>' <EOL> def it_returns_none_when_part_has_no_rels_xml ( self , dir_reader ) : <EOL> partname = PackURI ( '<STR_LIT>' ) <EOL> rels_xml = dir_reader . rels_xml_for ( partname ) <EOL> assert rels_xml is None <EOL> @ pytest . fixture <EOL> def pkg_file_ ( self , request ) : <EOL> return loose_mock ( request ) <EOL> @ pytest . fixture ( scope = '<STR_LIT:class>' ) <EOL> def dir_reader ( self ) : <EOL> return _DirPkgReader ( dir_pkg_path ) <EOL> class DescribePhysPkgReader ( object ) : <EOL> def it_raises_when_pkg_path_is_not_a_package ( self ) : <EOL> with pytest . raises ( PackageNotFoundError ) : <EOL> PhysPkgReader ( '<STR_LIT>' ) <EOL> class DescribeZipPkgReader ( object ) : <EOL> def it_is_used_by_PhysPkgReader_when_pkg_is_a_zip ( self ) : <EOL> phys_reader = PhysPkgReader ( zip_pkg_path ) <EOL> assert isinstance ( phys_reader , _ZipPkgReader ) <EOL> def it_is_used_by_PhysPkgReader_when_pkg_is_a_stream ( self ) : <EOL> with open ( zip_pkg_path , '<STR_LIT:rb>' ) as stream : <EOL> phys_reader = PhysPkgReader ( stream ) <EOL> assert isinstance ( phys_reader , _ZipPkgReader ) <EOL> def it_opens_pkg_file_zip_on_construction ( self , ZipFile_ , pkg_file_ ) : <EOL> _ZipPkgReader ( pkg_file_ ) <EOL> ZipFile_ . assert_called_once_with ( pkg_file_ , '<STR_LIT:r>' ) <EOL> def it_can_be_closed ( self , ZipFile_ ) : <EOL> zipf = ZipFile_ . return_value <EOL> zip_pkg_reader = _ZipPkgReader ( None ) <EOL> zip_pkg_reader . close ( ) <EOL> zipf . close . assert_called_once_with ( ) <EOL> def it_can_retrieve_the_blob_for_a_pack_uri ( self , phys_reader ) : <EOL> pack_uri = PackURI ( '<STR_LIT>' ) <EOL> blob = phys_reader . blob_for ( pack_uri ) <EOL> sha1 = hashlib . sha1 ( blob ) . hexdigest ( ) <EOL> assert sha1 == '<STR_LIT>' <EOL> def it_has_the_content_types_xml ( self , phys_reader ) : <EOL> sha1 = hashlib . sha1 ( phys_reader . content_types_xml ) . hexdigest ( ) <EOL> assert sha1 == '<STR_LIT>' <EOL> def it_can_retrieve_rels_xml_for_source_uri ( self , phys_reader ) : <EOL> rels_xml = phys_reader . rels_xml_for ( PACKAGE_URI ) <EOL> sha1 = hashlib . sha1 ( rels_xml ) . hexdigest ( ) <EOL> assert sha1 == '<STR_LIT>' <EOL> def it_returns_none_when_part_has_no_rels_xml ( self , phys_reader ) : <EOL> partname = PackURI ( '<STR_LIT>' ) <EOL> rels_xml = phys_reader . rels_xml_for ( partname ) <EOL> assert rels_xml is None <EOL> @ pytest . fixture ( scope = '<STR_LIT:class>' ) <EOL> def phys_reader ( self , request ) : <EOL> phys_reader = _ZipPkgReader ( zip_pkg_path ) <EOL> request . addfinalizer ( phys_reader . close ) <EOL> return phys_reader <EOL> @ pytest . fixture <EOL> def pkg_file_ ( self , request ) : <EOL> return loose_mock ( request ) <EOL> class DescribeZipPkgWriter ( object ) : <EOL> def it_is_used_by_PhysPkgWriter_unconditionally ( self , tmp_docx_path ) : <EOL> phys_writer = PhysPkgWriter ( tmp_docx_path ) <EOL> assert isinstance ( phys_writer , _ZipPkgWriter ) <EOL> def it_opens_pkg_file_zip_on_construction ( self , ZipFile_ ) : <EOL> pkg_file = Mock ( name = '<STR_LIT>' ) <EOL> _ZipPkgWriter ( pkg_file ) <EOL> ZipFile_ . assert_called_once_with ( <EOL> pkg_file , '<STR_LIT:w>' , compression = ZIP_DEFLATED <EOL> ) <EOL> def it_can_be_closed ( self , ZipFile_ ) : <EOL> zipf = ZipFile_ . return_value <EOL> zip_pkg_writer = _ZipPkgWriter ( None ) <EOL> zip_pkg_writer . close ( ) <EOL> zipf . close . assert_called_once_with ( ) <EOL> def it_can_write_a_blob ( self , pkg_file ) : <EOL> pack_uri = PackURI ( '<STR_LIT>' ) <EOL> blob = '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) <EOL> pkg_writer = PhysPkgWriter ( pkg_file ) <EOL> pkg_writer . write ( pack_uri , blob ) <EOL> pkg_writer . close ( ) <EOL> written_blob_sha1 = hashlib . sha1 ( blob ) . hexdigest ( ) <EOL> zipf = ZipFile ( pkg_file , '<STR_LIT:r>' ) <EOL> retrieved_blob = zipf . read ( pack_uri . membername ) <EOL> zipf . close ( ) <EOL> retrieved_blob_sha1 = hashlib . sha1 ( retrieved_blob ) . hexdigest ( ) <EOL> assert retrieved_blob_sha1 == written_blob_sha1 <EOL> @ pytest . fixture <EOL> def pkg_file ( self , request ) : <EOL> pkg_file = BytesIO ( ) <EOL> request . addfinalizer ( pkg_file . close ) <EOL> return pkg_file <EOL> @ pytest . fixture <EOL> def tmp_docx_path ( tmpdir ) : <EOL> return str ( tmpdir . join ( '<STR_LIT>' ) ) <EOL> @ pytest . fixture <EOL> def ZipFile_ ( request ) : <EOL> return class_mock ( request , '<STR_LIT>' ) </s>
