/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2005-2011, 2016 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
16
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
17
"""Test Store implementations."""
18
1185.1.46 by Robert Collins
Aarons branch --basis patch
19
import os
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
20
import gzip
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
21
6686.2.1 by Jelmer Vernooij
Move breezy.store to breezy.plugins.weave_fmt, its only user.
22
from ... import errors as errors
23
from ...errors import BzrError
24
from ...sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
25
    BytesIO,
26
    )
6686.2.1 by Jelmer Vernooij
Move breezy.store to breezy.plugins.weave_fmt, its only user.
27
from .store import TransportStore
28
from .store.text import TextStore
29
from .store.versioned import VersionedFileStore
30
from ...tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
31
from ... import transactions
32
from ... import transport
33
from ...transport.memory import MemoryTransport
6670.4.18 by Jelmer Vernooij
Merge move-store.
34
from ...bzr.weave import WeaveFile
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
35
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
36
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
37
class TestStores(object):
1185.16.146 by Martin Pool
Fix up assert with sideeffects in CompressedTextStore._copy_one
38
    """Mixin template class that provides some common tests for stores"""
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
39
40
    def check_content(self, store, fileid, value):
1442.1.35 by Robert Collins
convert all users of __getitem__ into TransportStores to use .get instead
41
        f = store.get(fileid)
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
42
        self.assertEqual(f.read(), value)
43
44
    def fill_store(self, store):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
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')
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
49
50
    def test_get(self):
51
        store = self.get_store()
52
        self.fill_store(store)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
53
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
54
        self.check_content(store, 'a', 'hello')
55
        self.check_content(store, 'b', 'other')
56
        self.check_content(store, 'c', 'something')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
57
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
58
        # Make sure that requesting a non-existing file fails
59
        self.assertRaises(KeyError, self.check_content, store, 'd', None)
60
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
61
    def test_multiple_add(self):
62
        """Multiple add with same ID should raise a BzrError"""
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
63
        store = self.get_store()
64
        self.fill_store(store)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
65
        self.assertRaises(BzrError, store.add, BytesIO(b'goodbye'), '123123')
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
66
67
68
class TestCompressedTextStore(TestCaseInTempDir, TestStores):
69
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
70
    def get_store(self, path=u'.'):
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
71
        t = transport.get_transport_from_path(path)
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
72
        return TextStore(t, compressed=True)
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
73
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
74
    def test_total_size(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
75
        store = self.get_store(u'.')
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
76
        store.register_suffix('dsc')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
77
        store.add(BytesIO(b'goodbye'), '123123')
78
        store.add(BytesIO(b'goodbye2'), '123123', 'dsc')
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
79
        # these get gzipped - content should be stable
80
        self.assertEqual(store.total_size(), (2, 55))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
81
1442.1.32 by Robert Collins
Teach CompressedTextStore._relpath to support file suffixes too.
82
    def test__relpath_suffixed(self):
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
83
        my_store = TextStore(MockTransport(),
84
                             prefixed=True, compressed=True)
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
85
        my_store.register_suffix('dsc')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
86
        self.assertEqual('45/foo.dsc', my_store._relpath('foo', ['dsc']))
1442.1.32 by Robert Collins
Teach CompressedTextStore._relpath to support file suffixes too.
87
1404 by Robert Collins
only pull remote text weaves once per fetch operation
88
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
89
class TestMemoryStore(TestCase):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
90
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
91
    def get_store(self):
1651.1.7 by Martin Pool
Move small ImmutableMemoryStore class into test module,
92
        return TextStore(MemoryTransport())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
93
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
94
    def test_add_and_retrieve(self):
95
        store = self.get_store()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
96
        store.add(BytesIO(b'hello'), 'aa')
1442.1.35 by Robert Collins
convert all users of __getitem__ into TransportStores to use .get instead
97
        self.assertNotEqual(store.get('aa'), None)
98
        self.assertEqual(store.get('aa').read(), 'hello')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
99
        store.add(BytesIO(b'hello world'), 'bb')
1442.1.35 by Robert Collins
convert all users of __getitem__ into TransportStores to use .get instead
100
        self.assertNotEqual(store.get('bb'), None)
101
        self.assertEqual(store.get('bb').read(), 'hello world')
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
102
103
    def test_missing_is_absent(self):
104
        store = self.get_store()
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
105
        self.assertFalse('aa' in store)
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
106
107
    def test_adding_fails_when_present(self):
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
108
        my_store = self.get_store()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
109
        my_store.add(BytesIO(b'hello'), 'aa')
1442.1.44 by Robert Collins
Many transport related tweaks:
110
        self.assertRaises(BzrError,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
111
                          my_store.add, BytesIO(b'hello'), 'aa')
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
112
113
    def test_total_size(self):
114
        store = self.get_store()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
115
        store.add(BytesIO(b'goodbye'), '123123')
116
        store.add(BytesIO(b'goodbye2'), '123123.dsc')
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
117
        self.assertEqual(store.total_size(), (2, 15))
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
118
        # TODO: Switch the exception form UnlistableStore to
119
        #       or make Stores throw UnlistableStore if their
120
        #       Transport doesn't support listing
121
        # store_c = RemoteStore('http://example.com/')
122
        # self.assertRaises(UnlistableStore, copy_all, store_c, store_b)
123
1404 by Robert Collins
only pull remote text weaves once per fetch operation
124
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
125
class TestTextStore(TestCaseInTempDir, TestStores):
126
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
127
    def get_store(self, path=u'.'):
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
128
        t = transport.get_transport_from_path(path)
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
129
        return TextStore(t, compressed=False)
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
130
131
    def test_total_size(self):
132
        store = self.get_store()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
133
        store.add(BytesIO(b'goodbye'), '123123')
134
        store.add(BytesIO(b'goodbye2'), '123123.dsc')
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
135
        self.assertEqual(store.total_size(), (2, 15))
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
136
        # TODO: Switch the exception form UnlistableStore to
137
        #       or make Stores throw UnlistableStore if their
138
        #       Transport doesn't support listing
139
        # store_c = RemoteStore('http://example.com/')
140
        # self.assertRaises(UnlistableStore, copy_all, store_c, store_b)
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
141
142
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
143
class TestMixedTextStore(TestCaseInTempDir, TestStores):
144
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
145
    def get_store(self, path=u'.', compressed=True):
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
146
        t = transport.get_transport_from_path(path)
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
147
        return TextStore(t, compressed=compressed)
148
149
    def test_get_mixed(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
150
        cs = self.get_store(u'.', compressed=True)
151
        s = self.get_store(u'.', compressed=False)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
152
        cs.add(BytesIO(b'hello there'), 'a')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
153
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
154
        self.assertPathExists('a.gz')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
155
        self.assertFalse(os.path.lexists('a'))
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
156
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
157
        self.assertEqual(gzip.GzipFile('a.gz').read(), 'hello there')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
158
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
159
        self.assertEqual(cs.has_id('a'), True)
160
        self.assertEqual(s.has_id('a'), True)
161
        self.assertEqual(cs.get('a').read(), 'hello there')
162
        self.assertEqual(s.get('a').read(), 'hello there')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
163
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
164
        self.assertRaises(BzrError, s.add, BytesIO(b'goodbye'), 'a')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
165
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
166
        s.add(BytesIO(b'goodbye'), 'b')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
167
        self.assertPathExists('b')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
168
        self.assertFalse(os.path.lexists('b.gz'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
169
        self.assertEqual(open('b').read(), 'goodbye')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
170
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
171
        self.assertEqual(cs.has_id('b'), True)
172
        self.assertEqual(s.has_id('b'), True)
173
        self.assertEqual(cs.get('b').read(), 'goodbye')
174
        self.assertEqual(s.get('b').read(), 'goodbye')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
175
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
176
        self.assertRaises(BzrError, cs.add, BytesIO(b'again'), 'b')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
177
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
178
class MockTransport(transport.Transport):
179
    """A fake transport for testing with."""
180
1442.1.30 by Robert Collins
Add stuf has and mkdir to MockTransport to enable testing store adds
181
    def has(self, filename):
182
        return False
183
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
184
    def __init__(self, url=None):
185
        if url is None:
186
            url = "http://example.com"
187
        super(MockTransport, self).__init__(url)
188
1442.1.30 by Robert Collins
Add stuf has and mkdir to MockTransport to enable testing store adds
189
    def mkdir(self, filename):
190
        return
191
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
192
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
193
class InstrumentedTransportStore(TransportStore):
1442.1.29 by Robert Collins
create an instrumented transport store for testing common logic.
194
    """An instrumented TransportStore.
195
196
    Here we replace template method worker methods with calls that record the
197
    expected results.
198
    """
199
200
    def _add(self, filename, file):
201
        self._calls.append(("_add", filename, file))
202
203
    def __init__(self, transport, prefixed=False):
204
        super(InstrumentedTransportStore, self).__init__(transport, prefixed)
205
        self._calls = []
206
207
208
class TestInstrumentedTransportStore(TestCase):
209
210
    def test__add_records(self):
211
        my_store = InstrumentedTransportStore(MockTransport())
1442.1.30 by Robert Collins
Add stuf has and mkdir to MockTransport to enable testing store adds
212
        my_store._add("filename", "file")
1442.1.29 by Robert Collins
create an instrumented transport store for testing common logic.
213
        self.assertEqual([("_add", "filename", "file")], my_store._calls)
214
215
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
216
class TestMockTransport(TestCase):
217
218
    def test_isinstance(self):
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
219
        self.assertIsInstance(MockTransport(), transport.Transport)
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
220
1442.1.30 by Robert Collins
Add stuf has and mkdir to MockTransport to enable testing store adds
221
    def test_has(self):
222
        self.assertEqual(False, MockTransport().has('foo'))
223
224
    def test_mkdir(self):
225
        MockTransport().mkdir('45')
226
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
227
228
class TestTransportStore(TestCase):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
229
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
230
    def test__relpath_invalid(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
231
        my_store = TransportStore(MockTransport())
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
232
        self.assertRaises(ValueError, my_store._relpath, '/foo')
233
        self.assertRaises(ValueError, my_store._relpath, 'foo/')
234
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
235
    def test_register_invalid_suffixes(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
236
        my_store = TransportStore(MockTransport())
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
237
        self.assertRaises(ValueError, my_store.register_suffix, '/')
238
        self.assertRaises(ValueError, my_store.register_suffix, '.gz/bar')
239
240
    def test__relpath_unregister_suffixes(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
241
        my_store = TransportStore(MockTransport())
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
242
        self.assertRaises(ValueError, my_store._relpath, 'foo', ['gz'])
243
        self.assertRaises(ValueError, my_store._relpath, 'foo', ['dsc', 'gz'])
1442.1.27 by Robert Collins
Check that file suffixes in TransportStore are also valid
244
1442.1.25 by Robert Collins
Test TransportStore._relpath for simple cases: pull up _prefixed attribute as a result.
245
    def test__relpath_simple(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
246
        my_store = TransportStore(MockTransport())
1442.1.25 by Robert Collins
Test TransportStore._relpath for simple cases: pull up _prefixed attribute as a result.
247
        self.assertEqual("foo", my_store._relpath('foo'))
1442.1.26 by Robert Collins
Pull up _relpath with gz suffix for CompressedTextStore into TransportStore
248
249
    def test__relpath_prefixed(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
250
        my_store = TransportStore(MockTransport(), True)
1442.1.26 by Robert Collins
Pull up _relpath with gz suffix for CompressedTextStore into TransportStore
251
        self.assertEqual('45/foo', my_store._relpath('foo'))
252
253
    def test__relpath_simple_suffixed(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
254
        my_store = TransportStore(MockTransport())
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
255
        my_store.register_suffix('bar')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
256
        my_store.register_suffix('baz')
257
        self.assertEqual('foo.baz', my_store._relpath('foo', ['baz']))
258
        self.assertEqual('foo.bar.baz', my_store._relpath('foo', ['bar', 'baz']))
1442.1.27 by Robert Collins
Check that file suffixes in TransportStore are also valid
259
260
    def test__relpath_prefixed_suffixed(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
261
        my_store = TransportStore(MockTransport(), True)
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
262
        my_store.register_suffix('bar')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
263
        my_store.register_suffix('baz')
264
        self.assertEqual('45/foo.baz', my_store._relpath('foo', ['baz']))
265
        self.assertEqual('45/foo.bar.baz',
266
                         my_store._relpath('foo', ['bar', 'baz']))
1442.1.27 by Robert Collins
Check that file suffixes in TransportStore are also valid
267
1442.1.31 by Robert Collins
test that TransportStore.add calls _add appropriately.
268
    def test_add_simple(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
269
        stream = BytesIO(b"content")
1442.1.31 by Robert Collins
test that TransportStore.add calls _add appropriately.
270
        my_store = InstrumentedTransportStore(MockTransport())
271
        my_store.add(stream, "foo")
272
        self.assertEqual([("_add", "foo", stream)], my_store._calls)
273
274
    def test_add_prefixed(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
275
        stream = BytesIO(b"content")
1442.1.31 by Robert Collins
test that TransportStore.add calls _add appropriately.
276
        my_store = InstrumentedTransportStore(MockTransport(), True)
277
        my_store.add(stream, "foo")
278
        self.assertEqual([("_add", "45/foo", stream)], my_store._calls)
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
279
280
    def test_add_simple_suffixed(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
281
        stream = BytesIO(b"content")
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
282
        my_store = InstrumentedTransportStore(MockTransport())
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
283
        my_store.register_suffix('dsc')
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
284
        my_store.add(stream, "foo", 'dsc')
285
        self.assertEqual([("_add", "foo.dsc", stream)], my_store._calls)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
286
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
287
    def test_add_simple_suffixed(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
288
        stream = BytesIO(b"content")
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
289
        my_store = InstrumentedTransportStore(MockTransport(), True)
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
290
        my_store.register_suffix('dsc')
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
291
        my_store.add(stream, "foo", 'dsc')
292
        self.assertEqual([("_add", "45/foo.dsc", stream)], my_store._calls)
1442.1.46 by Robert Collins
test simple has_id
293
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
294
    def get_populated_store(self, prefixed=False,
1185.16.159 by John Arbash Meinel
Updated the stores, all tests pass, and a store doesn't have to be 100% compressed
295
            store_class=TextStore, compressed=False):
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
296
        my_store = store_class(MemoryTransport(), prefixed,
297
                               compressed=compressed)
1442.1.46 by Robert Collins
test simple has_id
298
        my_store.register_suffix('sig')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
299
        stream = BytesIO(b"signature")
1442.1.46 by Robert Collins
test simple has_id
300
        my_store.add(stream, "foo", 'sig')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
301
        stream = BytesIO(b"content")
1442.1.46 by Robert Collins
test simple has_id
302
        my_store.add(stream, "foo")
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
303
        stream = BytesIO(b"signature for missing base")
1442.1.48 by Robert Collins
test that the presence of a signature does not make a missing base file magically appear present
304
        my_store.add(stream, "missing", 'sig')
1442.1.46 by Robert Collins
test simple has_id
305
        return my_store
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
306
1442.1.46 by Robert Collins
test simple has_id
307
    def test_has_simple(self):
308
        my_store = self.get_populated_store()
309
        self.assertEqual(True, my_store.has_id('foo'))
1442.1.47 by Robert Collins
test for has with suffixed files
310
        my_store = self.get_populated_store(True)
311
        self.assertEqual(True, my_store.has_id('foo'))
312
313
    def test_has_suffixed(self):
314
        my_store = self.get_populated_store()
315
        self.assertEqual(True, my_store.has_id('foo', 'sig'))
316
        my_store = self.get_populated_store(True)
317
        self.assertEqual(True, my_store.has_id('foo', 'sig'))
1442.1.48 by Robert Collins
test that the presence of a signature does not make a missing base file magically appear present
318
319
    def test_has_suffixed_no_base(self):
320
        my_store = self.get_populated_store()
321
        self.assertEqual(False, my_store.has_id('missing'))
322
        my_store = self.get_populated_store(True)
323
        self.assertEqual(False, my_store.has_id('missing'))
1442.1.50 by Robert Collins
test get with suffixes
324
325
    def test_get_simple(self):
326
        my_store = self.get_populated_store()
327
        self.assertEqual('content', my_store.get('foo').read())
328
        my_store = self.get_populated_store(True)
329
        self.assertEqual('content', my_store.get('foo').read())
330
331
    def test_get_suffixed(self):
332
        my_store = self.get_populated_store()
333
        self.assertEqual('signature', my_store.get('foo', 'sig').read())
334
        my_store = self.get_populated_store(True)
335
        self.assertEqual('signature', my_store.get('foo', 'sig').read())
336
337
    def test_get_suffixed_no_base(self):
338
        my_store = self.get_populated_store()
339
        self.assertEqual('signature for missing base',
340
                         my_store.get('missing', 'sig').read())
341
        my_store = self.get_populated_store(True)
342
        self.assertEqual('signature for missing base',
343
                         my_store.get('missing', 'sig').read())
1442.1.51 by Robert Collins
teach iter about suffixes
344
345
    def test___iter__no_suffix(self):
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
346
        my_store = TextStore(MemoryTransport(),
347
                             prefixed=False, compressed=False)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
348
        stream = BytesIO(b"content")
1442.1.51 by Robert Collins
teach iter about suffixes
349
        my_store.add(stream, "foo")
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
350
        self.assertEqual({'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
351
                         set(my_store.__iter__()))
352
353
    def test___iter__(self):
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
354
        self.assertEqual({'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
355
                         set(self.get_populated_store().__iter__()))
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
356
        self.assertEqual({'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
357
                         set(self.get_populated_store(True).__iter__()))
358
359
    def test___iter__compressed(self):
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
360
        self.assertEqual({'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
361
                         set(self.get_populated_store(
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
362
                             compressed=True).__iter__()))
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
363
        self.assertEqual({'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
364
                         set(self.get_populated_store(
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
365
                             True, compressed=True).__iter__()))
1442.1.51 by Robert Collins
teach iter about suffixes
366
367
    def test___len__(self):
368
        self.assertEqual(1, len(self.get_populated_store()))
1442.1.54 by Robert Collins
Teach store.copy_all about fileid suffixes
369
1469 by Robert Collins
Change Transport.* to work with URL's.
370
    def test_relpath_escaped(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
371
        my_store = TransportStore(MemoryTransport())
1469 by Robert Collins
Change Transport.* to work with URL's.
372
        self.assertEqual('%25', my_store._relpath('%'))
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
373
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
374
    def test_escaped_uppercase(self):
375
        """Uppercase letters are escaped for safety on Windows"""
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
376
        my_store = TransportStore(MemoryTransport(), prefixed=True,
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
377
            escaped=True)
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
378
        # a particularly perverse file-id! :-)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
379
        self.assertEqual(my_store._relpath('C:<>'), 'be/%2543%253a%253c%253e')
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
380
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
381
382
class TestVersionFileStore(TestCaseWithTransport):
383
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
384
    def get_scope(self):
385
        return self._transaction
386
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
387
    def setUp(self):
388
        super(TestVersionFileStore, self).setUp()
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
389
        self.vfstore = VersionedFileStore(MemoryTransport(),
390
            versionedfile_class=WeaveFile)
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
391
        self.vfstore.get_scope = self.get_scope
392
        self._transaction = None
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
393
394
    def test_get_weave_registers_dirty_in_write(self):
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
395
        self._transaction = transactions.WriteTransaction()
396
        vf = self.vfstore.get_weave_or_empty('id', self._transaction)
397
        self._transaction.finish()
398
        self._transaction = None
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
399
        self.assertRaises(errors.OutSideTransaction, vf.add_lines, 'b', [], [])
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
400
        self._transaction = transactions.WriteTransaction()
401
        vf = self.vfstore.get_weave('id', self._transaction)
402
        self._transaction.finish()
403
        self._transaction = None
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
404
        self.assertRaises(errors.OutSideTransaction, vf.add_lines, 'b', [], [])
405
406
    def test_get_weave_readonly_cant_write(self):
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
407
        self._transaction = transactions.WriteTransaction()
408
        vf = self.vfstore.get_weave_or_empty('id', self._transaction)
409
        self._transaction.finish()
410
        self._transaction = transactions.ReadOnlyTransaction()
411
        vf = self.vfstore.get_weave_or_empty('id', self._transaction)
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
412
        self.assertRaises(errors.ReadOnlyError, vf.add_lines, 'b', [], [])
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
413
414
    def test___iter__escaped(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
415
        self.vfstore = VersionedFileStore(MemoryTransport(),
416
            prefixed=True, escaped=True, versionedfile_class=WeaveFile)
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
417
        self.vfstore.get_scope = self.get_scope
418
        self._transaction = transactions.WriteTransaction()
419
        vf = self.vfstore.get_weave_or_empty(' ', self._transaction)
420
        vf.add_lines('a', [], [])
421
        del vf
422
        self._transaction.finish()
423
        self.assertEqual([' '], list(self.vfstore))