17
17
"""Test Store implementation
20
19
from StringIO import StringIO
23
from bzrlib.remotebranch import RemoteStore
24
from bzrlib.store import ImmutableStore, copy_all
22
from bzrlib.store import copy_all
23
from bzrlib.transport.local import LocalTransport
24
from bzrlib.transport import NoSuchFile
25
from bzrlib.store.compressed_text import CompressedTextStore
25
26
from bzrlib.selftest import TestCaseInTempDir
26
27
from bzrlib.errors import BzrError, UnlistableStore
29
class TestStore(TestCaseInTempDir):
30
def fill_store(store):
31
store.add(StringIO('hello'), 'a')
32
store.add(StringIO('other'), 'b')
33
store.add(StringIO('something'), 'c')
34
store.add(StringIO('goodbye'), '123123')
36
def check_equals(tester, store, files, values, permit_failure=False):
37
files = store.get(files, permit_failure=permit_failure)
39
for f, v in zip(files, values):
42
tester.assert_(f is None)
44
tester.assertEquals(f.read(), v)
45
tester.assertEquals(count, len(values))
46
# We need to check to make sure there are no more
47
# files to be returned, I'm using a cheezy way
48
# Convert to a list, and there shouldn't be any left
49
tester.assertEquals(len(list(files)), 0)
51
def test_multiple_add(tester, store):
53
tester.assertRaises(BzrError, store.add, StringIO('goodbye'), '123123')
55
def test_get(tester, store):
58
check_equals(tester, store, ['a'], ['hello'])
59
check_equals(tester, store, ['b', 'c'], ['other', 'something'])
61
# Make sure that requesting a non-existing file fails
62
tester.assertRaises(NoSuchFile, check_equals, tester, store,
64
tester.assertRaises(NoSuchFile, check_equals, tester, store,
65
['a', 'd'], ['hello', None])
66
tester.assertRaises(NoSuchFile, check_equals, tester, store,
67
['d', 'a'], [None, 'hello'])
68
tester.assertRaises(NoSuchFile, check_equals, tester, store,
69
['d', 'd', 'd'], [None, None, None])
70
tester.assertRaises(NoSuchFile, check_equals, tester, store,
71
['a', 'd', 'b'], ['hello', None, 'other'])
73
def test_ignore_get(tester, store):
76
files = store.get(['d'], permit_failure=True)
78
tester.assertEquals(len(files), 1)
79
tester.assert_(files[0] is None)
81
check_equals(tester, store, ['a', 'd'], ['hello', None],
83
check_equals(tester, store, ['d', 'a'], [None, 'hello'],
85
check_equals(tester, store, ['d', 'd'], [None, None],
87
check_equals(tester, store, ['a', 'd', 'b'], ['hello', None, 'other'],
89
check_equals(tester, store, ['a', 'd', 'b'], ['hello', None, 'other'],
91
check_equals(tester, store, ['b', 'd', 'c'], ['other', None, 'something'],
94
def get_compressed_store():
95
t = LocalTransport('.')
96
return CompressedTextStore(t)
98
class TestCompressedTextStore(TestCaseInTempDir):
30
99
def test_multiple_add(self):
31
100
"""Multiple add with same ID should raise a BzrError"""
32
store = ImmutableStore('.')
33
store.add(StringIO('goodbye'), '123123')
34
self.assertRaises(BzrError, store.add, StringIO('goodbye'), '123123')
101
store = get_compressed_store()
102
test_multiple_add(self, store)
105
store = get_compressed_store()
106
test_get(self, store)
108
def test_ignore_get(self):
109
store = get_compressed_store()
110
test_ignore_get(self, store)
36
113
def test_copy_all(self):
37
114
"""Test copying"""
39
store_a = ImmutableStore('a')
116
store_a = CompressedTextStore('a')
40
117
store_a.add('foo', '1')
42
store_b = ImmutableStore('b')
119
store_b = CompressedTextStore('b')
43
120
copy_all(store_a, store_b)
44
121
self.assertEqual(store_a['1'].read(), 'foo')
45
122
self.assertEqual(store_b['1'].read(), 'foo')
46
store_c = RemoteStore('http://example.com/')
47
self.assertRaises(UnlistableStore, copy_all, store_c, store_b)
123
# TODO: Switch the exception form UnlistableStore to
124
# or make Stores throw UnlistableStore if their
125
# Transport doesn't support listing
126
# store_c = RemoteStore('http://example.com/')
127
# self.assertRaises(UnlistableStore, copy_all, store_c, store_b)