/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test__chk_map.py

  • Committer: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Tests for _chk_map_*."""
18
 
 
19
 
from .. import (
20
 
    tests,
21
 
    )
22
 
from ..bzr import (
23
 
    chk_map,
24
 
    )
25
 
from ..sixish import int2byte
26
 
from ..static_tuple import StaticTuple
27
 
stuple = StaticTuple
28
 
 
29
 
 
30
 
def load_tests(loader, standard_tests, pattern):
31
 
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
32
 
                                                 'breezy.bzr._chk_map_py', 'breezy.bzr._chk_map_pyx')
33
 
    return suite
34
 
 
35
 
 
36
 
class TestSearchKeys(tests.TestCase):
37
 
 
38
 
    module = None  # Filled in by test parameterization
39
 
 
40
 
    def assertSearchKey16(self, expected, key):
41
 
        self.assertEqual(expected, self.module._search_key_16(key))
42
 
 
43
 
    def assertSearchKey255(self, expected, key):
44
 
        actual = self.module._search_key_255(key)
45
 
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
46
 
 
47
 
    def test_simple_16(self):
48
 
        self.assertSearchKey16(b'8C736521', stuple(b'foo',))
49
 
        self.assertSearchKey16(b'8C736521\x008C736521', stuple(b'foo', b'foo'))
50
 
        self.assertSearchKey16(b'8C736521\x0076FF8CAA', stuple(b'foo', b'bar'))
51
 
        self.assertSearchKey16(b'ED82CD11', stuple(b'abcd',))
52
 
 
53
 
    def test_simple_255(self):
54
 
        self.assertSearchKey255(b'\x8cse!', stuple(b'foo',))
55
 
        self.assertSearchKey255(b'\x8cse!\x00\x8cse!', stuple(b'foo', b'foo'))
56
 
        self.assertSearchKey255(
57
 
            b'\x8cse!\x00v\xff\x8c\xaa', stuple(b'foo', b'bar'))
58
 
        # The standard mapping for these would include '\n', so it should be
59
 
        # mapped to '_'
60
 
        self.assertSearchKey255(b'\xfdm\x93_\x00P_\x1bL', stuple(b'<', b'V'))
61
 
 
62
 
    def test_255_does_not_include_newline(self):
63
 
        # When mapping via _search_key_255, we should never have the '\n'
64
 
        # character, but all other 255 values should be present
65
 
        chars_used = set()
66
 
        for char_in in range(256):
67
 
            search_key = self.module._search_key_255(
68
 
                stuple(int2byte(char_in),))
69
 
            chars_used.update(map(int2byte, bytearray(search_key)))
70
 
        all_chars = {int2byte(x) for x in range(256)}
71
 
        unused_chars = all_chars.symmetric_difference(chars_used)
72
 
        self.assertEqual({b'\n'}, unused_chars)
73
 
 
74
 
 
75
 
class TestDeserialiseLeafNode(tests.TestCase):
76
 
 
77
 
    module = None
78
 
 
79
 
    def assertDeserialiseErrors(self, text):
80
 
        self.assertRaises((ValueError, IndexError),
81
 
                          self.module._deserialise_leaf_node, text, b'not-a-real-sha')
82
 
 
83
 
    def test_raises_on_non_leaf(self):
84
 
        self.assertDeserialiseErrors(b'')
85
 
        self.assertDeserialiseErrors(b'short\n')
86
 
        self.assertDeserialiseErrors(b'chknotleaf:\n')
87
 
        self.assertDeserialiseErrors(b'chkleaf:x\n')
88
 
        self.assertDeserialiseErrors(b'chkleaf:\n')
89
 
        self.assertDeserialiseErrors(b'chkleaf:\nnotint\n')
90
 
        self.assertDeserialiseErrors(b'chkleaf:\n10\n')
91
 
        self.assertDeserialiseErrors(b'chkleaf:\n10\n256\n')
92
 
        self.assertDeserialiseErrors(b'chkleaf:\n10\n256\n10\n')
93
 
 
94
 
    def test_deserialise_empty(self):
95
 
        node = self.module._deserialise_leaf_node(
96
 
            b"chkleaf:\n10\n1\n0\n\n", stuple(b"sha1:1234",))
97
 
        self.assertEqual(0, len(node))
98
 
        self.assertEqual(10, node.maximum_size)
99
 
        self.assertEqual((b"sha1:1234",), node.key())
100
 
        self.assertIsInstance(node.key(), StaticTuple)
101
 
        self.assertIs(None, node._search_prefix)
102
 
        self.assertIs(None, node._common_serialised_prefix)
103
 
 
104
 
    def test_deserialise_items(self):
105
 
        node = self.module._deserialise_leaf_node(
106
 
            b"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
107
 
            (b"sha1:1234",))
108
 
        self.assertEqual(2, len(node))
109
 
        self.assertEqual([((b"foo bar",), b"baz"), ((b"quux",), b"blarh")],
110
 
                         sorted(node.iteritems(None)))
111
 
 
112
 
    def test_deserialise_item_with_null_width_1(self):
113
 
        node = self.module._deserialise_leaf_node(
114
 
            b"chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
115
 
            (b"sha1:1234",))
116
 
        self.assertEqual(2, len(node))
117
 
        self.assertEqual([((b"foo",), b"bar\x00baz"), ((b"quux",), b"blarh")],
118
 
                         sorted(node.iteritems(None)))
119
 
 
120
 
    def test_deserialise_item_with_null_width_2(self):
121
 
        node = self.module._deserialise_leaf_node(
122
 
            b"chkleaf:\n0\n2\n2\n\nfoo\x001\x001\nbar\x00baz\n"
123
 
            b"quux\x00\x001\nblarh\n",
124
 
            (b"sha1:1234",))
125
 
        self.assertEqual(2, len(node))
126
 
        self.assertEqual([((b"foo", b"1"), b"bar\x00baz"), ((b"quux", b""), b"blarh")],
127
 
                         sorted(node.iteritems(None)))
128
 
 
129
 
    def test_iteritems_selected_one_of_two_items(self):
130
 
        node = self.module._deserialise_leaf_node(
131
 
            b"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
132
 
            (b"sha1:1234",))
133
 
        self.assertEqual(2, len(node))
134
 
        self.assertEqual([((b"quux",), b"blarh")],
135
 
                         sorted(node.iteritems(None, [(b"quux",), (b"qaz",)])))
136
 
 
137
 
    def test_deserialise_item_with_common_prefix(self):
138
 
        node = self.module._deserialise_leaf_node(
139
 
            b"chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
140
 
            (b"sha1:1234",))
141
 
        self.assertEqual(2, len(node))
142
 
        self.assertEqual([((b"foo", b"1"), b"bar\x00baz"), ((b"foo", b"2"), b"blarh")],
143
 
                         sorted(node.iteritems(None)))
144
 
        self.assertIs(chk_map._unknown, node._search_prefix)
145
 
        self.assertEqual(b'foo\x00', node._common_serialised_prefix)
146
 
 
147
 
    def test_deserialise_multi_line(self):
148
 
        node = self.module._deserialise_leaf_node(
149
 
            b"chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
150
 
            (b"sha1:1234",))
151
 
        self.assertEqual(2, len(node))
152
 
        self.assertEqual([((b"foo", b"1"), b"bar\nbaz"),
153
 
                          ((b"foo", b"2"), b"blarh\n"),
154
 
                          ], sorted(node.iteritems(None)))
155
 
        self.assertIs(chk_map._unknown, node._search_prefix)
156
 
        self.assertEqual(b'foo\x00', node._common_serialised_prefix)
157
 
 
158
 
    def test_key_after_map(self):
159
 
        node = self.module._deserialise_leaf_node(
160
 
            b"chkleaf:\n10\n1\n0\n\n", (b"sha1:1234",))
161
 
        node.map(None, (b"foo bar",), b"baz quux")
162
 
        self.assertEqual(None, node.key())
163
 
 
164
 
    def test_key_after_unmap(self):
165
 
        node = self.module._deserialise_leaf_node(
166
 
            b"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
167
 
            (b"sha1:1234",))
168
 
        node.unmap(None, (b"foo bar",))
169
 
        self.assertEqual(None, node.key())
170
 
 
171
 
 
172
 
class TestDeserialiseInternalNode(tests.TestCase):
173
 
 
174
 
    module = None
175
 
 
176
 
    def assertDeserialiseErrors(self, text):
177
 
        self.assertRaises((ValueError, IndexError),
178
 
                          self.module._deserialise_internal_node, text,
179
 
                          stuple(b'not-a-real-sha',))
180
 
 
181
 
    def test_raises_on_non_internal(self):
182
 
        self.assertDeserialiseErrors(b'')
183
 
        self.assertDeserialiseErrors(b'short\n')
184
 
        self.assertDeserialiseErrors(b'chknotnode:\n')
185
 
        self.assertDeserialiseErrors(b'chknode:x\n')
186
 
        self.assertDeserialiseErrors(b'chknode:\n')
187
 
        self.assertDeserialiseErrors(b'chknode:\nnotint\n')
188
 
        self.assertDeserialiseErrors(b'chknode:\n10\n')
189
 
        self.assertDeserialiseErrors(b'chknode:\n10\n256\n')
190
 
        self.assertDeserialiseErrors(b'chknode:\n10\n256\n10\n')
191
 
        # no trailing newline
192
 
        self.assertDeserialiseErrors(b'chknode:\n10\n256\n0\n1\nfo')
193
 
 
194
 
    def test_deserialise_one(self):
195
 
        node = self.module._deserialise_internal_node(
196
 
            b"chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple(b'sha1:1234',))
197
 
        self.assertIsInstance(node, chk_map.InternalNode)
198
 
        self.assertEqual(1, len(node))
199
 
        self.assertEqual(10, node.maximum_size)
200
 
        self.assertEqual((b"sha1:1234",), node.key())
201
 
        self.assertEqual(b'', node._search_prefix)
202
 
        self.assertEqual({b'a': (b'sha1:abcd',)}, node._items)
203
 
 
204
 
    def test_deserialise_with_prefix(self):
205
 
        node = self.module._deserialise_internal_node(
206
 
            b"chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n",
207
 
            stuple(b'sha1:1234',))
208
 
        self.assertIsInstance(node, chk_map.InternalNode)
209
 
        self.assertEqual(1, len(node))
210
 
        self.assertEqual(10, node.maximum_size)
211
 
        self.assertEqual((b"sha1:1234",), node.key())
212
 
        self.assertEqual(b'pref', node._search_prefix)
213
 
        self.assertEqual({b'prefa': (b'sha1:abcd',)}, node._items)
214
 
 
215
 
        node = self.module._deserialise_internal_node(
216
 
            b"chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n",
217
 
            stuple(b'sha1:1234',))
218
 
        self.assertIsInstance(node, chk_map.InternalNode)
219
 
        self.assertEqual(1, len(node))
220
 
        self.assertEqual(10, node.maximum_size)
221
 
        self.assertEqual((b"sha1:1234",), node.key())
222
 
        self.assertEqual(b'pref', node._search_prefix)
223
 
        self.assertEqual({b'pref': (b'sha1:abcd',)}, node._items)
224
 
 
225
 
    def test_deserialise_pref_with_null(self):
226
 
        node = self.module._deserialise_internal_node(
227
 
            b"chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
228
 
            stuple(b'sha1:1234',))
229
 
        self.assertIsInstance(node, chk_map.InternalNode)
230
 
        self.assertEqual(1, len(node))
231
 
        self.assertEqual(10, node.maximum_size)
232
 
        self.assertEqual((b"sha1:1234",), node.key())
233
 
        self.assertEqual(b'pref\x00fo', node._search_prefix)
234
 
        self.assertEqual({b'pref\x00fo': (b'sha1:abcd',)}, node._items)
235
 
 
236
 
    def test_deserialise_with_null_pref(self):
237
 
        node = self.module._deserialise_internal_node(
238
 
            b"chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
239
 
            stuple(b'sha1:1234',))
240
 
        self.assertIsInstance(node, chk_map.InternalNode)
241
 
        self.assertEqual(1, len(node))
242
 
        self.assertEqual(10, node.maximum_size)
243
 
        self.assertEqual((b"sha1:1234",), node.key())
244
 
        self.assertEqual(b'pref\x00fo', node._search_prefix)
245
 
        self.assertEqual({b'pref\x00fo\x00': (b'sha1:abcd',)}, node._items)
246
 
 
247
 
 
248
 
class Test_BytesToTextKey(tests.TestCase):
249
 
 
250
 
    def assertBytesToTextKey(self, key, bytes):
251
 
        self.assertEqual(key,
252
 
                         self.module._bytes_to_text_key(bytes))
253
 
 
254
 
    def assertBytesToTextKeyRaises(self, bytes):
255
 
        # These are invalid bytes, and we want to make sure the code under test
256
 
        # raises an exception rather than segfaults, etc. We don't particularly
257
 
        # care what exception.
258
 
        self.assertRaises(Exception, self.module._bytes_to_text_key, bytes)
259
 
 
260
 
    def test_file(self):
261
 
        self.assertBytesToTextKey((b'file-id', b'revision-id'),
262
 
                                  b'file: file-id\nparent-id\nname\nrevision-id\n'
263
 
                                  b'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
264
 
 
265
 
    def test_invalid_no_kind(self):
266
 
        self.assertBytesToTextKeyRaises(
267
 
            b'file  file-id\nparent-id\nname\nrevision-id\n'
268
 
            b'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
269
 
 
270
 
    def test_invalid_no_space(self):
271
 
        self.assertBytesToTextKeyRaises(
272
 
            b'file:file-id\nparent-id\nname\nrevision-id\n'
273
 
            b'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
274
 
 
275
 
    def test_invalid_too_short_file_id(self):
276
 
        self.assertBytesToTextKeyRaises(b'file:file-id')
277
 
 
278
 
    def test_invalid_too_short_parent_id(self):
279
 
        self.assertBytesToTextKeyRaises(b'file:file-id\nparent-id')
280
 
 
281
 
    def test_invalid_too_short_name(self):
282
 
        self.assertBytesToTextKeyRaises(b'file:file-id\nparent-id\nname')
283
 
 
284
 
    def test_dir(self):
285
 
        self.assertBytesToTextKey((b'dir-id', b'revision-id'),
286
 
                                  b'dir: dir-id\nparent-id\nname\nrevision-id')