99
1010"""Test suite for opc.phys_pkg module."""
1111
12+ try :
13+ from io import BytesIO # Python 3
14+ except ImportError :
15+ from StringIO import StringIO as BytesIO
16+
1217import hashlib
1318
14- from zipfile import ZIP_DEFLATED
19+ from zipfile import ZIP_DEFLATED , ZipFile
1520
1621from opc .packuri import PACKAGE_URI , PackURI
1722from opc .phys_pkg import (
@@ -110,6 +115,12 @@ def it_returns_none_when_part_has_no_rels_xml(self, phys_reader):
110115
111116class DescribeZipPkgWriter (object ):
112117
118+ @pytest .fixture
119+ def pkg_file (self , request ):
120+ pkg_file = BytesIO ()
121+ request .addfinalizer (pkg_file .close )
122+ return pkg_file
123+
113124 def it_opens_pkg_file_zip_on_construction (self , ZipFile_ ):
114125 pkg_file = Mock (name = 'pkg_file' )
115126 ZipPkgWriter (pkg_file )
@@ -124,3 +135,19 @@ def it_can_be_closed(self, ZipFile_):
124135 zip_pkg_writer .close ()
125136 # verify -----------------------
126137 zipf .close .assert_called_once_with ()
138+
139+ def it_can_write_a_blob (self , pkg_file ):
140+ # setup ------------------------
141+ pack_uri = PackURI ('/part/name.xml' )
142+ blob = '<BlobbityFooBlob/>' .encode ('utf-8' )
143+ # exercise ---------------------
144+ pkg_writer = PhysPkgWriter (pkg_file )
145+ pkg_writer .write (pack_uri , blob )
146+ pkg_writer .close ()
147+ # verify -----------------------
148+ written_blob_sha1 = hashlib .sha1 (blob ).hexdigest ()
149+ zipf = ZipFile (pkg_file , 'r' )
150+ retrieved_blob = zipf .read (pack_uri .membername )
151+ zipf .close ()
152+ retrieved_blob_sha1 = hashlib .sha1 (retrieved_blob ).hexdigest ()
153+ assert retrieved_blob_sha1 == written_blob_sha1
0 commit comments