~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__chk_map.py

  • Committer: Danny van Heumen
  • Date: 2010-03-09 16:38:10 UTC
  • mto: (4634.139.5 2.0)
  • mto: This revision was merged to the branch mainline in revision 5160.
  • Revision ID: danny@dannyvanheumen.nl-20100309163810-ujn8hcx08f75nlf1
Refined test to make use of locking hooks and also validate if lock is truly a checkout-lock.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
18
18
 
19
19
from bzrlib import (
20
20
    chk_map,
21
 
    inventory,
22
21
    tests,
23
22
    )
24
 
from bzrlib.static_tuple import StaticTuple
25
 
stuple = StaticTuple
26
23
 
27
24
 
28
25
def load_tests(standard_tests, module, loader):
29
 
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
30
 
        'bzrlib._chk_map_py', 'bzrlib._chk_map_pyx')
 
26
    # parameterize all tests in this module
 
27
    suite = loader.suiteClass()
 
28
    import bzrlib._chk_map_py as py_module
 
29
    scenarios = [('python', {'module': py_module})]
 
30
    if CompiledChkMapFeature.available():
 
31
        import bzrlib._chk_map_pyx as c_module
 
32
        scenarios.append(('C', {'module': c_module}))
 
33
    else:
 
34
        # the compiled module isn't available, so we add a failing test
 
35
        class FailWithoutFeature(tests.TestCase):
 
36
            def test_fail(self):
 
37
                self.requireFeature(CompiledChkMapFeature)
 
38
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
 
39
    tests.multiply_tests(standard_tests, scenarios, suite)
31
40
    return suite
32
41
 
33
42
 
 
43
class _CompiledChkMapFeature(tests.Feature):
 
44
 
 
45
    def _probe(self):
 
46
        try:
 
47
            import bzrlib._chk_map_pyx
 
48
        except ImportError:
 
49
            return False
 
50
        return True
 
51
 
 
52
    def feature_name(self):
 
53
        return 'bzrlib._chk_map_pyx'
 
54
 
 
55
CompiledChkMapFeature = _CompiledChkMapFeature()
 
56
 
 
57
 
34
58
class TestSearchKeys(tests.TestCase):
35
59
 
36
60
    module = None # Filled in by test parameterization
43
67
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
44
68
 
45
69
    def test_simple_16(self):
46
 
        self.assertSearchKey16('8C736521', stuple('foo',))
47
 
        self.assertSearchKey16('8C736521\x008C736521', stuple('foo', 'foo'))
48
 
        self.assertSearchKey16('8C736521\x0076FF8CAA', stuple('foo', 'bar'))
49
 
        self.assertSearchKey16('ED82CD11', stuple('abcd',))
 
70
        self.assertSearchKey16('8C736521', ('foo',))
 
71
        self.assertSearchKey16('8C736521\x008C736521', ('foo', 'foo'))
 
72
        self.assertSearchKey16('8C736521\x0076FF8CAA', ('foo', 'bar'))
 
73
        self.assertSearchKey16('ED82CD11', ('abcd',))
50
74
 
51
75
    def test_simple_255(self):
52
 
        self.assertSearchKey255('\x8cse!', stuple('foo',))
53
 
        self.assertSearchKey255('\x8cse!\x00\x8cse!', stuple('foo', 'foo'))
54
 
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', stuple('foo', 'bar'))
 
76
        self.assertSearchKey255('\x8cse!', ('foo',))
 
77
        self.assertSearchKey255('\x8cse!\x00\x8cse!', ('foo', 'foo'))
 
78
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', ('foo', 'bar'))
55
79
        # The standard mapping for these would include '\n', so it should be
56
80
        # mapped to '_'
57
 
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', stuple('<', 'V'))
 
81
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', ('<', 'V'))
58
82
 
59
83
    def test_255_does_not_include_newline(self):
60
84
        # When mapping via _search_key_255, we should never have the '\n'
61
85
        # character, but all other 255 values should be present
62
86
        chars_used = set()
63
87
        for char_in in range(256):
64
 
            search_key = self.module._search_key_255(stuple(chr(char_in),))
 
88
            search_key = self.module._search_key_255((chr(char_in),))
65
89
            chars_used.update(search_key)
66
90
        all_chars = set([chr(x) for x in range(256)])
67
91
        unused_chars = all_chars.symmetric_difference(chars_used)
89
113
 
90
114
    def test_deserialise_empty(self):
91
115
        node = self.module._deserialise_leaf_node(
92
 
            "chkleaf:\n10\n1\n0\n\n", stuple("sha1:1234",))
 
116
            "chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
93
117
        self.assertEqual(0, len(node))
94
118
        self.assertEqual(10, node.maximum_size)
95
119
        self.assertEqual(("sha1:1234",), node.key())
96
 
        self.assertIsInstance(node.key(), StaticTuple)
97
120
        self.assertIs(None, node._search_prefix)
98
121
        self.assertIs(None, node._common_serialised_prefix)
99
122
 
171
194
 
172
195
    def assertDeserialiseErrors(self, text):
173
196
        self.assertRaises((ValueError, IndexError),
174
 
            self.module._deserialise_internal_node, text,
175
 
                stuple('not-a-real-sha',))
 
197
            self.module._deserialise_internal_node, text, 'not-a-real-sha')
176
198
 
177
199
    def test_raises_on_non_internal(self):
178
200
        self.assertDeserialiseErrors('')
189
211
 
190
212
    def test_deserialise_one(self):
191
213
        node = self.module._deserialise_internal_node(
192
 
            "chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple('sha1:1234',))
 
214
            "chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", ('sha1:1234',))
193
215
        self.assertIsInstance(node, chk_map.InternalNode)
194
216
        self.assertEqual(1, len(node))
195
217
        self.assertEqual(10, node.maximum_size)
199
221
 
200
222
    def test_deserialise_with_prefix(self):
201
223
        node = self.module._deserialise_internal_node(
202
 
            "chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", stuple('sha1:1234',))
 
224
            "chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", ('sha1:1234',))
203
225
        self.assertIsInstance(node, chk_map.InternalNode)
204
226
        self.assertEqual(1, len(node))
205
227
        self.assertEqual(10, node.maximum_size)
208
230
        self.assertEqual({'prefa': ('sha1:abcd',)}, node._items)
209
231
 
210
232
        node = self.module._deserialise_internal_node(
211
 
            "chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", stuple('sha1:1234',))
 
233
            "chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", ('sha1:1234',))
212
234
        self.assertIsInstance(node, chk_map.InternalNode)
213
235
        self.assertEqual(1, len(node))
214
236
        self.assertEqual(10, node.maximum_size)
218
240
 
219
241
    def test_deserialise_pref_with_null(self):
220
242
        node = self.module._deserialise_internal_node(
221
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
222
 
            stuple('sha1:1234',))
 
243
            "chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n", ('sha1:1234',))
223
244
        self.assertIsInstance(node, chk_map.InternalNode)
224
245
        self.assertEqual(1, len(node))
225
246
        self.assertEqual(10, node.maximum_size)
229
250
 
230
251
    def test_deserialise_with_null_pref(self):
231
252
        node = self.module._deserialise_internal_node(
232
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
233
 
            stuple('sha1:1234',))
 
253
            "chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n", ('sha1:1234',))
234
254
        self.assertIsInstance(node, chk_map.InternalNode)
235
255
        self.assertEqual(1, len(node))
236
256
        self.assertEqual(10, node.maximum_size)
237
257
        self.assertEqual(("sha1:1234",), node.key())
238
258
        self.assertEqual('pref\x00fo', node._search_prefix)
239
259
        self.assertEqual({'pref\x00fo\x00': ('sha1:abcd',)}, node._items)
240
 
 
241
 
 
242
 
class Test_BytesToTextKey(tests.TestCase):
243
 
 
244
 
    def assertBytesToTextKey(self, key, bytes):
245
 
        self.assertEqual(key,
246
 
                         self.module._bytes_to_text_key(bytes))
247
 
 
248
 
    def assertBytesToTextKeyRaises(self, bytes):
249
 
        # These are invalid bytes, and we want to make sure the code under test
250
 
        # raises an exception rather than segfaults, etc. We don't particularly
251
 
        # care what exception.
252
 
        self.assertRaises(Exception, self.module._bytes_to_text_key, bytes)
253
 
 
254
 
    def test_file(self):
255
 
        self.assertBytesToTextKey(('file-id', 'revision-id'),
256
 
                 'file: file-id\nparent-id\nname\nrevision-id\n'
257
 
                 'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
258
 
 
259
 
    def test_invalid_no_kind(self):
260
 
        self.assertBytesToTextKeyRaises(
261
 
                 'file  file-id\nparent-id\nname\nrevision-id\n'
262
 
                 'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
263
 
 
264
 
    def test_invalid_no_space(self):
265
 
        self.assertBytesToTextKeyRaises(
266
 
                 'file:file-id\nparent-id\nname\nrevision-id\n'
267
 
                 'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
268
 
 
269
 
    def test_invalid_too_short_file_id(self):
270
 
        self.assertBytesToTextKeyRaises('file:file-id')
271
 
 
272
 
    def test_invalid_too_short_parent_id(self):
273
 
        self.assertBytesToTextKeyRaises('file:file-id\nparent-id')
274
 
 
275
 
    def test_invalid_too_short_name(self):
276
 
        self.assertBytesToTextKeyRaises('file:file-id\nparent-id\nname')
277
 
 
278
 
    def test_dir(self):
279
 
        self.assertBytesToTextKey(('dir-id', 'revision-id'),
280
 
                 'dir: dir-id\nparent-id\nname\nrevision-id')