/breezy/unstable

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy/unstable

« back to all changes in this revision

Viewing changes to breezy/tests/test_store.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-24 01:39:33 UTC
  • mfrom: (3815.3776.6)
  • Revision ID: jelmer@jelmer.uk-20170524013933-ir4y4tqtrsiz2ka2
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Test Store implementations."""
18
18
 
19
 
from cStringIO import StringIO
20
19
import os
21
20
import gzip
22
21
 
23
 
import bzrlib.errors as errors
24
 
from bzrlib.errors import BzrError
25
 
from bzrlib.store import TransportStore
26
 
from bzrlib.store.text import TextStore
27
 
from bzrlib.store.versioned import VersionedFileStore
28
 
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
29
 
import bzrlib.transactions as transactions
30
 
import bzrlib.transport as transport
31
 
from bzrlib.transport.memory import MemoryTransport
32
 
from bzrlib.weave import WeaveFile
 
22
from .. import errors as errors
 
23
from ..errors import BzrError
 
24
from ..sixish import (
 
25
    BytesIO,
 
26
    )
 
27
from ..store import TransportStore
 
28
from ..store.text import TextStore
 
29
from ..store.versioned import VersionedFileStore
 
30
from . import TestCase, TestCaseInTempDir, TestCaseWithTransport
 
31
from .. import transactions as transactions
 
32
from .. import transport as transport
 
33
from ..transport.memory import MemoryTransport
 
34
from ..weave import WeaveFile
33
35
 
34
36
 
35
37
class TestStores(object):
40
42
        self.assertEqual(f.read(), value)
41
43
 
42
44
    def fill_store(self, store):
43
 
        store.add(StringIO('hello'), 'a')
44
 
        store.add(StringIO('other'), 'b')
45
 
        store.add(StringIO('something'), 'c')
46
 
        store.add(StringIO('goodbye'), '123123')
 
45
        store.add(BytesIO(b'hello'), 'a')
 
46
        store.add(BytesIO(b'other'), 'b')
 
47
        store.add(BytesIO(b'something'), 'c')
 
48
        store.add(BytesIO(b'goodbye'), '123123')
47
49
 
48
50
    def test_copy_all(self):
49
51
        """Test copying"""
50
52
        os.mkdir('a')
51
53
        store_a = self.get_store('a')
52
 
        store_a.add(StringIO('foo'), '1')
 
54
        store_a.add(BytesIO(b'foo'), '1')
53
55
        os.mkdir('b')
54
56
        store_b = self.get_store('b')
55
57
        store_b.copy_all_ids(store_a)
76
78
        """Multiple add with same ID should raise a BzrError"""
77
79
        store = self.get_store()
78
80
        self.fill_store(store)
79
 
        self.assertRaises(BzrError, store.add, StringIO('goodbye'), '123123')
 
81
        self.assertRaises(BzrError, store.add, BytesIO(b'goodbye'), '123123')
80
82
 
81
83
 
82
84
class TestCompressedTextStore(TestCaseInTempDir, TestStores):
88
90
    def test_total_size(self):
89
91
        store = self.get_store(u'.')
90
92
        store.register_suffix('dsc')
91
 
        store.add(StringIO('goodbye'), '123123')
92
 
        store.add(StringIO('goodbye2'), '123123', 'dsc')
 
93
        store.add(BytesIO(b'goodbye'), '123123')
 
94
        store.add(BytesIO(b'goodbye2'), '123123', 'dsc')
93
95
        # these get gzipped - content should be stable
94
96
        self.assertEqual(store.total_size(), (2, 55))
95
97
 
107
109
 
108
110
    def test_add_and_retrieve(self):
109
111
        store = self.get_store()
110
 
        store.add(StringIO('hello'), 'aa')
 
112
        store.add(BytesIO(b'hello'), 'aa')
111
113
        self.assertNotEqual(store.get('aa'), None)
112
114
        self.assertEqual(store.get('aa').read(), 'hello')
113
 
        store.add(StringIO('hello world'), 'bb')
 
115
        store.add(BytesIO(b'hello world'), 'bb')
114
116
        self.assertNotEqual(store.get('bb'), None)
115
117
        self.assertEqual(store.get('bb').read(), 'hello world')
116
118
 
120
122
 
121
123
    def test_adding_fails_when_present(self):
122
124
        my_store = self.get_store()
123
 
        my_store.add(StringIO('hello'), 'aa')
 
125
        my_store.add(BytesIO(b'hello'), 'aa')
124
126
        self.assertRaises(BzrError,
125
 
                          my_store.add, StringIO('hello'), 'aa')
 
127
                          my_store.add, BytesIO(b'hello'), 'aa')
126
128
 
127
129
    def test_total_size(self):
128
130
        store = self.get_store()
129
 
        store.add(StringIO('goodbye'), '123123')
130
 
        store.add(StringIO('goodbye2'), '123123.dsc')
 
131
        store.add(BytesIO(b'goodbye'), '123123')
 
132
        store.add(BytesIO(b'goodbye2'), '123123.dsc')
131
133
        self.assertEqual(store.total_size(), (2, 15))
132
134
        # TODO: Switch the exception form UnlistableStore to
133
135
        #       or make Stores throw UnlistableStore if their
144
146
 
145
147
    def test_total_size(self):
146
148
        store = self.get_store()
147
 
        store.add(StringIO('goodbye'), '123123')
148
 
        store.add(StringIO('goodbye2'), '123123.dsc')
 
149
        store.add(BytesIO(b'goodbye'), '123123')
 
150
        store.add(BytesIO(b'goodbye2'), '123123.dsc')
149
151
        self.assertEqual(store.total_size(), (2, 15))
150
152
        # TODO: Switch the exception form UnlistableStore to
151
153
        #       or make Stores throw UnlistableStore if their
163
165
    def test_get_mixed(self):
164
166
        cs = self.get_store(u'.', compressed=True)
165
167
        s = self.get_store(u'.', compressed=False)
166
 
        cs.add(StringIO('hello there'), 'a')
 
168
        cs.add(BytesIO(b'hello there'), 'a')
167
169
 
168
170
        self.assertPathExists('a.gz')
169
171
        self.assertFalse(os.path.lexists('a'))
175
177
        self.assertEqual(cs.get('a').read(), 'hello there')
176
178
        self.assertEqual(s.get('a').read(), 'hello there')
177
179
 
178
 
        self.assertRaises(BzrError, s.add, StringIO('goodbye'), 'a')
 
180
        self.assertRaises(BzrError, s.add, BytesIO(b'goodbye'), 'a')
179
181
 
180
 
        s.add(StringIO('goodbye'), 'b')
 
182
        s.add(BytesIO(b'goodbye'), 'b')
181
183
        self.assertPathExists('b')
182
184
        self.assertFalse(os.path.lexists('b.gz'))
183
185
        self.assertEqual(open('b').read(), 'goodbye')
187
189
        self.assertEqual(cs.get('b').read(), 'goodbye')
188
190
        self.assertEqual(s.get('b').read(), 'goodbye')
189
191
 
190
 
        self.assertRaises(BzrError, cs.add, StringIO('again'), 'b')
 
192
        self.assertRaises(BzrError, cs.add, BytesIO(b'again'), 'b')
191
193
 
192
194
class MockTransport(transport.Transport):
193
195
    """A fake transport for testing with."""
280
282
                         my_store._relpath('foo', ['bar', 'baz']))
281
283
 
282
284
    def test_add_simple(self):
283
 
        stream = StringIO("content")
 
285
        stream = BytesIO(b"content")
284
286
        my_store = InstrumentedTransportStore(MockTransport())
285
287
        my_store.add(stream, "foo")
286
288
        self.assertEqual([("_add", "foo", stream)], my_store._calls)
287
289
 
288
290
    def test_add_prefixed(self):
289
 
        stream = StringIO("content")
 
291
        stream = BytesIO(b"content")
290
292
        my_store = InstrumentedTransportStore(MockTransport(), True)
291
293
        my_store.add(stream, "foo")
292
294
        self.assertEqual([("_add", "45/foo", stream)], my_store._calls)
293
295
 
294
296
    def test_add_simple_suffixed(self):
295
 
        stream = StringIO("content")
 
297
        stream = BytesIO(b"content")
296
298
        my_store = InstrumentedTransportStore(MockTransport())
297
299
        my_store.register_suffix('dsc')
298
300
        my_store.add(stream, "foo", 'dsc')
299
301
        self.assertEqual([("_add", "foo.dsc", stream)], my_store._calls)
300
302
 
301
303
    def test_add_simple_suffixed(self):
302
 
        stream = StringIO("content")
 
304
        stream = BytesIO(b"content")
303
305
        my_store = InstrumentedTransportStore(MockTransport(), True)
304
306
        my_store.register_suffix('dsc')
305
307
        my_store.add(stream, "foo", 'dsc')
310
312
        my_store = store_class(MemoryTransport(), prefixed,
311
313
                               compressed=compressed)
312
314
        my_store.register_suffix('sig')
313
 
        stream = StringIO("signature")
 
315
        stream = BytesIO(b"signature")
314
316
        my_store.add(stream, "foo", 'sig')
315
 
        stream = StringIO("content")
 
317
        stream = BytesIO(b"content")
316
318
        my_store.add(stream, "foo")
317
 
        stream = StringIO("signature for missing base")
 
319
        stream = BytesIO(b"signature for missing base")
318
320
        my_store.add(stream, "missing", 'sig')
319
321
        return my_store
320
322
 
359
361
    def test___iter__no_suffix(self):
360
362
        my_store = TextStore(MemoryTransport(),
361
363
                             prefixed=False, compressed=False)
362
 
        stream = StringIO("content")
 
364
        stream = BytesIO(b"content")
363
365
        my_store.add(stream, "foo")
364
 
        self.assertEqual(set(['foo']),
 
366
        self.assertEqual({'foo'},
365
367
                         set(my_store.__iter__()))
366
368
 
367
369
    def test___iter__(self):
368
 
        self.assertEqual(set(['foo']),
 
370
        self.assertEqual({'foo'},
369
371
                         set(self.get_populated_store().__iter__()))
370
 
        self.assertEqual(set(['foo']),
 
372
        self.assertEqual({'foo'},
371
373
                         set(self.get_populated_store(True).__iter__()))
372
374
 
373
375
    def test___iter__compressed(self):
374
 
        self.assertEqual(set(['foo']),
 
376
        self.assertEqual({'foo'},
375
377
                         set(self.get_populated_store(
376
378
                             compressed=True).__iter__()))
377
 
        self.assertEqual(set(['foo']),
 
379
        self.assertEqual({'foo'},
378
380
                         set(self.get_populated_store(
379
381
                             True, compressed=True).__iter__()))
380
382
 
388
390
        to_store.register_suffix('sig')
389
391
        to_store.copy_all_ids(from_store)
390
392
        self.assertEqual(1, len(to_store))
391
 
        self.assertEqual(set(['foo']), set(to_store.__iter__()))
 
393
        self.assertEqual({'foo'}, set(to_store.__iter__()))
392
394
        self.assertEqual('content', to_store.get('foo').read())
393
395
        self.assertEqual('signature', to_store.get('foo', 'sig').read())
394
396
        self.assertRaises(KeyError, to_store.get, 'missing', 'sig')