~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_btree_index.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 16:40:10 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216164010-z3hy00xrnclnkf7a
Update tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008-2011 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
31
31
    )
32
32
from bzrlib.tests import (
33
33
    TestCaseWithTransport,
34
 
    condition_isinstance,
35
 
    multiply_tests,
36
 
    split_suite_by_condition,
37
 
    )
38
 
 
39
 
 
40
 
def load_tests(standard_tests, module, loader):
41
 
    # parameterise the TestBTreeNodes tests
42
 
    node_tests, others = split_suite_by_condition(standard_tests,
43
 
        condition_isinstance(TestBTreeNodes))
 
34
    scenarios,
 
35
    )
 
36
from bzrlib.tests import (
 
37
    features,
 
38
    )
 
39
 
 
40
 
 
41
load_tests = scenarios.load_tests_apply_scenarios
 
42
 
 
43
 
 
44
def btreeparser_scenarios():
44
45
    import bzrlib._btree_serializer_py as py_module
45
46
    scenarios = [('python', {'parse_btree': py_module})]
46
47
    if compiled_btreeparser_feature.available():
47
 
        scenarios.append(('C', {'parse_btree':
48
 
                                compiled_btreeparser_feature.module}))
49
 
    return multiply_tests(node_tests, scenarios, others)
50
 
 
51
 
 
52
 
compiled_btreeparser_feature = tests.ModuleAvailableFeature(
53
 
                                'bzrlib._btree_serializer_pyx')
 
48
        scenarios.append(('C', 
 
49
            {'parse_btree': compiled_btreeparser_feature.module}))
 
50
    return scenarios
 
51
 
 
52
 
 
53
compiled_btreeparser_feature = features.ModuleAvailableFeature(
 
54
    'bzrlib._btree_serializer_pyx')
54
55
 
55
56
 
56
57
class BTreeTestCase(TestCaseWithTransport):
280
281
 
281
282
        for node in nodes:
282
283
            builder.add_node(*node)
283
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
284
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
284
285
        size = t.put_file('index', self.time(builder.finish))
285
286
        del builder
286
287
        index = btree_index.BTreeGraphIndex(t, 'index', size)
607
608
        for key, value, references in nodes:
608
609
            builder.add_node(key, value, references)
609
610
        stream = builder.finish()
610
 
        trans = transport.get_transport('trace+' + self.get_url())
 
611
        trans = transport.get_transport_from_url('trace+' + self.get_url())
611
612
        size = trans.put_file('index', stream)
612
613
        return btree_index.BTreeGraphIndex(trans, 'index', size)
613
614
 
648
649
        self.assertEqual(0, len(index._leaf_node_cache))
649
650
 
650
651
    def test_trivial_constructor(self):
651
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
652
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
652
653
        index = btree_index.BTreeGraphIndex(t, 'index', None)
653
654
        # Checks the page size at load, but that isn't logged yet.
654
655
        self.assertEqual([], t._activity)
655
656
 
656
657
    def test_with_size_constructor(self):
657
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
658
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
658
659
        index = btree_index.BTreeGraphIndex(t, 'index', 1)
659
660
        # Checks the page size at load, but that isn't logged yet.
660
661
        self.assertEqual([], t._activity)
661
662
 
662
663
    def test_empty_key_count_no_size(self):
663
664
        builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
664
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
665
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
665
666
        t.put_file('index', builder.finish())
666
667
        index = btree_index.BTreeGraphIndex(t, 'index', None)
667
668
        del t._activity[:]
674
675
 
675
676
    def test_empty_key_count(self):
676
677
        builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
677
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
678
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
678
679
        size = t.put_file('index', builder.finish())
679
680
        self.assertEqual(72, size)
680
681
        index = btree_index.BTreeGraphIndex(t, 'index', size)
690
691
        nodes = self.make_nodes(35, 2, 2)
691
692
        for node in nodes:
692
693
            builder.add_node(*node)
693
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
694
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
694
695
        size = t.put_file('index', builder.finish())
695
696
        index = btree_index.BTreeGraphIndex(t, 'index', size)
696
697
        del t._activity[:]
722
723
 
723
724
    def test__read_nodes_no_size_one_page_reads_once(self):
724
725
        self.make_index(nodes=[(('key',), 'value', ())])
725
 
        trans = transport.get_transport('trace+' + self.get_url())
 
726
        trans = transport.get_transport_from_url('trace+' + self.get_url())
726
727
        index = btree_index.BTreeGraphIndex(trans, 'index', None)
727
728
        del trans._activity[:]
728
729
        nodes = dict(index._read_nodes([0]))
736
737
        index.key_count()
737
738
        num_pages = index._row_offsets[-1]
738
739
        # Reopen with a traced transport and no size
739
 
        trans = transport.get_transport('trace+' + self.get_url())
 
740
        trans = transport.get_transport_from_url('trace+' + self.get_url())
740
741
        index = btree_index.BTreeGraphIndex(trans, 'index', None)
741
742
        del trans._activity[:]
742
743
        nodes = dict(index._read_nodes([0]))
747
748
        nodes = self.make_nodes(160, 2, 2)
748
749
        for node in nodes:
749
750
            builder.add_node(*node)
750
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
751
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
751
752
        size = t.put_file('index', builder.finish())
752
753
        self.assertEqual(17692, size)
753
754
        index = btree_index.BTreeGraphIndex(t, 'index', size)
763
764
        nodes = self.make_nodes(45, 2, 2)
764
765
        for node in nodes:
765
766
            builder.add_node(*node)
766
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
767
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
767
768
        size = t.put_file('index', builder.finish())
768
769
        index = btree_index.BTreeGraphIndex(t, 'index', size)
769
770
        del t._activity[:]
779
780
        nodes = self.make_nodes(80, 2, 2)
780
781
        for node in nodes:
781
782
            builder.add_node(*node)
782
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
783
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
783
784
        size = t.put_file('index', builder.finish())
784
785
        # Root page, 2 leaf pages
785
786
        self.assertEqual(9339, size)
797
798
 
798
799
    def test_eq_ne(self):
799
800
        # two indices are equal when constructed with the same parameters:
800
 
        t1 = transport.get_transport('trace+' + self.get_url(''))
801
 
        t2 = transport.get_transport(self.get_url(''))
 
801
        t1 = transport.get_transport_from_url('trace+' + self.get_url(''))
 
802
        t2 = self.get_transport()
802
803
        self.assertTrue(
803
804
            btree_index.BTreeGraphIndex(t1, 'index', None) ==
804
805
            btree_index.BTreeGraphIndex(t1, 'index', None))
830
831
            btree_index.BTreeGraphIndex(t1, 'index', 10) !=
831
832
            btree_index.BTreeGraphIndex(t1, 'index', 20))
832
833
 
 
834
    def test_key_too_big(self):
 
835
        # the size that matters here is the _compressed_ size of the key, so we can't
 
836
        # do a simple character repeat.
 
837
        bigKey = ''.join(map(repr, xrange(btree_index._PAGE_SIZE)))
 
838
        self.assertRaises(errors.BadIndexKey,
 
839
                          self.make_index,
 
840
                          nodes=[((bigKey,), 'value', ())])
 
841
        
833
842
    def test_iter_all_only_root_no_size(self):
834
843
        self.make_index(nodes=[(('key',), 'value', ())])
835
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
844
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
836
845
        index = btree_index.BTreeGraphIndex(t, 'index', None)
837
846
        del t._activity[:]
838
847
        self.assertEqual([(('key',), 'value')],
849
858
        nodes = self.make_nodes(10000, 2, 2)
850
859
        for node in nodes:
851
860
            builder.add_node(*node)
852
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
861
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
853
862
        size = t.put_file('index', builder.finish())
854
863
        self.assertEqual(1303220, size, 'number of expected bytes in the'
855
864
                                        ' output changed')
904
913
        nodes = self.make_nodes(160, 2, 2)
905
914
        for node in nodes:
906
915
            builder.add_node(*node)
907
 
        t = transport.get_transport('trace+' + self.get_url(''))
 
916
        t = transport.get_transport_from_url('trace+' + self.get_url(''))
908
917
        size = t.put_file('index', builder.finish())
909
918
        del builder
910
919
        index = btree_index.BTreeGraphIndex(t, 'index', size)
1153
1162
        for node in nodes:
1154
1163
            builder.add_node(*node)
1155
1164
        stream = builder.finish()
1156
 
        trans = transport.get_transport(self.get_url())
 
1165
        trans = self.get_transport()
1157
1166
        size = trans.put_file('index', stream)
1158
1167
        index = btree_index.BTreeGraphIndex(trans, 'index', size)
1159
1168
        self.assertEqual(500, index.key_count())
1185
1194
 
1186
1195
class TestBTreeNodes(BTreeTestCase):
1187
1196
 
 
1197
    scenarios = btreeparser_scenarios()
 
1198
 
1188
1199
    def setUp(self):
1189
1200
        BTreeTestCase.setUp(self)
1190
1201
        self.overrideAttr(btree_index, '_btree_serializer', self.parse_btree)
1351
1362
        BTreeGraphIndex with the recommended information.
1352
1363
        """
1353
1364
        index = btree_index.BTreeGraphIndex(
1354
 
            transport.get_transport('memory:///'), 'test-index', size=size)
 
1365
            transport.get_transport_from_url('memory:///'),
 
1366
            'test-index', size=size)
1355
1367
        if recommended_pages is not None:
1356
1368
            index._recommended_pages = recommended_pages
1357
1369
        return index