~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_index.py

  • Committer: Vincent Ladeuil
  • Date: 2010-01-25 15:55:48 UTC
  • mto: (4985.1.4 add-attr-cleanup)
  • mto: This revision was merged to the branch mainline in revision 4988.
  • Revision ID: v.ladeuil+lp@free.fr-20100125155548-0l352pujvt5bzl5e
Deploy addAttrCleanup on the whole test suite.

Several use case worth mentioning:

- setting a module or any other object attribute is the majority
by far. In some cases the setting itself is deferred but most of
the time we want to set at the same time we add the cleanup.

- there multiple occurrences of protecting hooks or ui factory
which are now useless (the test framework takes care of that now),

- there was some lambda uses that can now be avoided.

That first cleanup already simplifies things a lot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 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
173
173
            "key\x00\x00\t\x00data\n"
174
174
            "\n", contents)
175
175
 
 
176
    def test_clear_cache(self):
 
177
        builder = GraphIndexBuilder(reference_lists=2)
 
178
        # This is a no-op, but the api should exist
 
179
        builder.clear_cache()
 
180
 
176
181
    def test_node_references_are_byte_offsets(self):
177
182
        builder = GraphIndexBuilder(reference_lists=1)
178
183
        builder.add_node(('reference', ), 'data', ([], ))
230
235
        builder.add_node(('2-key', ), '', (references, ))
231
236
        stream = builder.finish()
232
237
        contents = stream.read()
233
 
        self.assertEqual(
 
238
        self.assertEqualDiff(
234
239
            "Bazaar Graph Index 1\nnode_ref_lists=1\nkey_elements=1\nlen=1\n"
235
240
            "0\x00a\x00\x00\n"
236
241
            "1\x00a\x00\x00\n"
383
388
        size = trans.put_file('index', stream)
384
389
        return GraphIndex(trans, 'index', size)
385
390
 
 
391
    def test_clear_cache(self):
 
392
        index = self.make_index()
 
393
        # For now, we just want to make sure the api is available. As this is
 
394
        # old code, we don't really worry if it *does* anything.
 
395
        index.clear_cache()
 
396
 
386
397
    def test_open_bad_index_no_error(self):
387
398
        trans = self.get_transport()
388
399
        trans.put_bytes('name', "not an index\n")
953
964
            ])
954
965
        self.assertEqual(set([]), index.external_references(0))
955
966
 
 
967
    def test__find_ancestors(self):
 
968
        key1 = ('key-1',)
 
969
        key2 = ('key-2',)
 
970
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
 
971
            (key1, 'value', ([key2],)),
 
972
            (key2, 'value', ([],)),
 
973
            ])
 
974
        parent_map = {}
 
975
        missing_keys = set()
 
976
        search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
 
977
        self.assertEqual({key1: (key2,)}, parent_map)
 
978
        self.assertEqual(set(), missing_keys)
 
979
        self.assertEqual(set([key2]), search_keys)
 
980
        search_keys = index._find_ancestors(search_keys, 0, parent_map,
 
981
                                            missing_keys)
 
982
        self.assertEqual({key1: (key2,), key2: ()}, parent_map)
 
983
        self.assertEqual(set(), missing_keys)
 
984
        self.assertEqual(set(), search_keys)
 
985
 
 
986
    def test__find_ancestors_w_missing(self):
 
987
        key1 = ('key-1',)
 
988
        key2 = ('key-2',)
 
989
        key3 = ('key-3',)
 
990
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
 
991
            (key1, 'value', ([key2],)),
 
992
            (key2, 'value', ([],)),
 
993
            ])
 
994
        parent_map = {}
 
995
        missing_keys = set()
 
996
        search_keys = index._find_ancestors([key2, key3], 0, parent_map,
 
997
                                            missing_keys)
 
998
        self.assertEqual({key2: ()}, parent_map)
 
999
        self.assertEqual(set([key3]), missing_keys)
 
1000
        self.assertEqual(set(), search_keys)
 
1001
 
 
1002
    def test__find_ancestors_dont_search_known(self):
 
1003
        key1 = ('key-1',)
 
1004
        key2 = ('key-2',)
 
1005
        key3 = ('key-3',)
 
1006
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
 
1007
            (key1, 'value', ([key2],)),
 
1008
            (key2, 'value', ([key3],)),
 
1009
            (key3, 'value', ([],)),
 
1010
            ])
 
1011
        # We already know about key2, so we won't try to search for key3
 
1012
        parent_map = {key2: (key3,)}
 
1013
        missing_keys = set()
 
1014
        search_keys = index._find_ancestors([key1], 0, parent_map,
 
1015
                                            missing_keys)
 
1016
        self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
 
1017
        self.assertEqual(set(), missing_keys)
 
1018
        self.assertEqual(set(), search_keys)
 
1019
 
 
1020
    def test_supports_unlimited_cache(self):
 
1021
        builder = GraphIndexBuilder(0, key_elements=1)
 
1022
        stream = builder.finish()
 
1023
        trans = get_transport(self.get_url())
 
1024
        size = trans.put_file('index', stream)
 
1025
        # It doesn't matter what unlimited_cache does here, just that it can be
 
1026
        # passed
 
1027
        index = GraphIndex(trans, 'index', size, unlimited_cache=True)
 
1028
 
956
1029
 
957
1030
class TestCombinedGraphIndex(TestCaseWithMemoryTransport):
958
1031
 
1009
1082
        index.insert_index(0, index1)
1010
1083
        self.assertEqual([(index1, ('key', ), '')], list(index.iter_all_entries()))
1011
1084
 
 
1085
    def test_clear_cache(self):
 
1086
        log = []
 
1087
 
 
1088
        class ClearCacheProxy(object):
 
1089
 
 
1090
            def __init__(self, index):
 
1091
                self._index = index
 
1092
 
 
1093
            def __getattr__(self, name):
 
1094
                return getattr(self._index)
 
1095
 
 
1096
            def clear_cache(self):
 
1097
                log.append(self._index)
 
1098
                return self._index.clear_cache()
 
1099
 
 
1100
        index = CombinedGraphIndex([])
 
1101
        index1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
 
1102
        index.insert_index(0, ClearCacheProxy(index1))
 
1103
        index2 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
 
1104
        index.insert_index(1, ClearCacheProxy(index2))
 
1105
        # CombinedGraphIndex should call 'clear_cache()' on all children
 
1106
        index.clear_cache()
 
1107
        self.assertEqual(sorted([index1, index2]), sorted(log))
 
1108
 
1012
1109
    def test_iter_all_entries_empty(self):
1013
1110
        index = CombinedGraphIndex([])
1014
1111
        self.assertEqual([], list(index.iter_all_entries()))
1271
1368
                                    ['1', '2', '3'])
1272
1369
        self.assertRaises(errors.NoSuchFile, index.validate)
1273
1370
 
 
1371
    def test_find_ancestors_across_indexes(self):
 
1372
        key1 = ('key-1',)
 
1373
        key2 = ('key-2',)
 
1374
        key3 = ('key-3',)
 
1375
        key4 = ('key-4',)
 
1376
        index1 = self.make_index('12', ref_lists=1, nodes=[
 
1377
            (key1, 'value', ([],)),
 
1378
            (key2, 'value', ([key1],)),
 
1379
            ])
 
1380
        index2 = self.make_index('34', ref_lists=1, nodes=[
 
1381
            (key3, 'value', ([key2],)),
 
1382
            (key4, 'value', ([key3],)),
 
1383
            ])
 
1384
        c_index = CombinedGraphIndex([index1, index2])
 
1385
        parent_map, missing_keys = c_index.find_ancestry([key1], 0)
 
1386
        self.assertEqual({key1: ()}, parent_map)
 
1387
        self.assertEqual(set(), missing_keys)
 
1388
        # Now look for a key from index2 which requires us to find the key in
 
1389
        # the second index, and then continue searching for parents in the
 
1390
        # first index
 
1391
        parent_map, missing_keys = c_index.find_ancestry([key3], 0)
 
1392
        self.assertEqual({key1: (), key2: (key1,), key3: (key2,)}, parent_map)
 
1393
        self.assertEqual(set(), missing_keys)
 
1394
 
 
1395
    def test_find_ancestors_missing_keys(self):
 
1396
        key1 = ('key-1',)
 
1397
        key2 = ('key-2',)
 
1398
        key3 = ('key-3',)
 
1399
        key4 = ('key-4',)
 
1400
        index1 = self.make_index('12', ref_lists=1, nodes=[
 
1401
            (key1, 'value', ([],)),
 
1402
            (key2, 'value', ([key1],)),
 
1403
            ])
 
1404
        index2 = self.make_index('34', ref_lists=1, nodes=[
 
1405
            (key3, 'value', ([key2],)),
 
1406
            ])
 
1407
        c_index = CombinedGraphIndex([index1, index2])
 
1408
        # Searching for a key which is actually not present at all should
 
1409
        # eventually converge
 
1410
        parent_map, missing_keys = c_index.find_ancestry([key4], 0)
 
1411
        self.assertEqual({}, parent_map)
 
1412
        self.assertEqual(set([key4]), missing_keys)
 
1413
 
 
1414
    def test_find_ancestors_no_indexes(self):
 
1415
        c_index = CombinedGraphIndex([])
 
1416
        key1 = ('key-1',)
 
1417
        parent_map, missing_keys = c_index.find_ancestry([key1], 0)
 
1418
        self.assertEqual({}, parent_map)
 
1419
        self.assertEqual(set([key1]), missing_keys)
 
1420
 
 
1421
    def test_find_ancestors_ghost_parent(self):
 
1422
        key1 = ('key-1',)
 
1423
        key2 = ('key-2',)
 
1424
        key3 = ('key-3',)
 
1425
        key4 = ('key-4',)
 
1426
        index1 = self.make_index('12', ref_lists=1, nodes=[
 
1427
            (key1, 'value', ([],)),
 
1428
            (key2, 'value', ([key1],)),
 
1429
            ])
 
1430
        index2 = self.make_index('34', ref_lists=1, nodes=[
 
1431
            (key4, 'value', ([key2, key3],)),
 
1432
            ])
 
1433
        c_index = CombinedGraphIndex([index1, index2])
 
1434
        # Searching for a key which is actually not present at all should
 
1435
        # eventually converge
 
1436
        parent_map, missing_keys = c_index.find_ancestry([key4], 0)
 
1437
        self.assertEqual({key4: (key2, key3), key2: (key1,), key1: ()},
 
1438
                         parent_map)
 
1439
        self.assertEqual(set([key3]), missing_keys)
 
1440
 
 
1441
    def test__find_ancestors_empty_index(self):
 
1442
        index = self.make_index('test', ref_lists=1, key_elements=1, nodes=[])
 
1443
        parent_map = {}
 
1444
        missing_keys = set()
 
1445
        search_keys = index._find_ancestors([('one',), ('two',)], 0, parent_map,
 
1446
                                            missing_keys)
 
1447
        self.assertEqual(set(), search_keys)
 
1448
        self.assertEqual({}, parent_map)
 
1449
        self.assertEqual(set([('one',), ('two',)]), missing_keys)
 
1450
 
1274
1451
 
1275
1452
class TestInMemoryGraphIndex(TestCaseWithMemoryTransport):
1276
1453