@@ -31,7 +31,8 @@ def close(self):
31
31
32
32
def _assert (self ):
33
33
assert self .was_read
34
-
34
+
35
+
35
36
class DeriveTest (OStream ):
36
37
def __init__ (self , sha , type , size , stream , * args , ** kwargs ):
37
38
self .myarg = kwargs .pop ('myarg' )
@@ -41,6 +42,27 @@ def _assert(self):
41
42
assert self .args
42
43
assert self .myarg
43
44
45
+
46
+ class ZippedStoreShaWriter (Sha1Writer ):
47
+ """Remembers everything someone writes to it"""
48
+ __slots__ = ('buf' , 'zip' )
49
+ def __init__ (self ):
50
+ Sha1Writer .__init__ (self )
51
+ self .buf = StringIO ()
52
+ self .zip = zlib .compressobj (1 ) # fastest
53
+
54
+ def __getattr__ (self , attr ):
55
+ return getattr (self .buf , attr )
56
+
57
+ def write (self , data ):
58
+ alen = Sha1Writer .write (self , data )
59
+ self .buf .write (self .zip .compress (data ))
60
+ return alen
61
+
62
+ def close (self ):
63
+ self .buf .write (self .zip .flush ())
64
+
65
+
44
66
#} END stream utilitiess
45
67
46
68
@@ -68,15 +90,11 @@ def test_streams(self):
68
90
ostream .read (20 )
69
91
assert stream .bytes == 20
70
92
71
- # defaults false
72
- assert not ostream .is_compressed ()
73
-
74
93
# derive with own args
75
94
DeriveTest (sha , Blob .type , s , stream , 'mine' ,myarg = 3 )._assert ()
76
95
77
96
# test istream
78
97
istream = IStream (Blob .type , s , stream )
79
- assert not istream .is_compressed ()
80
98
assert istream .sha == None
81
99
istream .sha = sha
82
100
assert istream .sha == sha
@@ -94,6 +112,10 @@ def test_streams(self):
94
112
istream .stream = None
95
113
assert istream .stream is None
96
114
115
+ assert istream .error is None
116
+ istream .error = Exception ()
117
+ assert isinstance (istream .error , Exception )
118
+
97
119
def _assert_stream_reader (self , stream , cdata , rewind_stream = lambda s : None ):
98
120
"""Make stream tests - the orig_stream is seekable, allowing it to be
99
121
rewound and reused
@@ -218,13 +240,14 @@ def _assert_object_writing(self, db):
218
240
"""General tests to verify object writing, compatible to ObjectDBW
219
241
:note: requires write access to the database"""
220
242
# start in 'dry-run' mode, using a simple sha1 writer
221
- ostreams = (Sha1Writer , None )
243
+ ostreams = (ZippedStoreShaWriter , None )
222
244
for ostreamcls in ostreams :
223
245
for data in self .all_data :
224
246
dry_run = ostreamcls is not None
225
247
ostream = None
226
248
if ostreamcls is not None :
227
249
ostream = ostreamcls ()
250
+ assert isinstance (ostream , Sha1Writer )
228
251
# END create ostream
229
252
230
253
prev_ostream = db .set_ostream (ostream )
@@ -252,6 +275,26 @@ def _assert_object_writing(self, db):
252
275
else :
253
276
self .failUnlessRaises (BadObject , db .info , sha )
254
277
self .failUnlessRaises (BadObject , db .stream , sha )
278
+
279
+ # DIRECT STREAM COPY
280
+ # our data hase been written in object format to the StringIO
281
+ # we pasesd as output stream. No physical database representation
282
+ # was created.
283
+ # Test direct stream copy of object streams, the result must be
284
+ # identical to what we fed in
285
+ ostream .seek (0 )
286
+ istream .stream = ostream
287
+ assert istream .sha is not None
288
+ prev_sha = istream .sha
289
+
290
+ db .set_ostream (ZippedStoreShaWriter ())
291
+ db .store (istream )
292
+ assert istream .sha == prev_sha
293
+ new_ostream = db .ostream ()
294
+
295
+ # note: only works as long our store write uses the same compression
296
+ # level, which is zip
297
+ assert ostream .getvalue () == new_ostream .getvalue ()
255
298
# END for each data set
256
299
# END for each dry_run mode
257
300
0 commit comments