9
9
10
10
"""Test suite for opc.phys_pkg module."""
11
11
12
+ try :
13
+ from io import BytesIO # Python 3
14
+ except ImportError :
15
+ from StringIO import StringIO as BytesIO
16
+
12
17
import hashlib
13
18
14
- from zipfile import ZIP_DEFLATED
19
+ from zipfile import ZIP_DEFLATED , ZipFile
15
20
16
21
from opc .packuri import PACKAGE_URI , PackURI
17
22
from opc .phys_pkg import (
@@ -110,6 +115,12 @@ def it_returns_none_when_part_has_no_rels_xml(self, phys_reader):
110
115
111
116
class DescribeZipPkgWriter (object ):
112
117
118
+ @pytest .fixture
119
+ def pkg_file (self , request ):
120
+ pkg_file = BytesIO ()
121
+ request .addfinalizer (pkg_file .close )
122
+ return pkg_file
123
+
113
124
def it_opens_pkg_file_zip_on_construction (self , ZipFile_ ):
114
125
pkg_file = Mock (name = 'pkg_file' )
115
126
ZipPkgWriter (pkg_file )
@@ -124,3 +135,19 @@ def it_can_be_closed(self, ZipFile_):
124
135
zip_pkg_writer .close ()
125
136
# verify -----------------------
126
137
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