~bzr-pqm/bzr/bzr.dev

4597.9.2 by Vincent Ladeuil
Merge bzr.dev into cleanup
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
3770.1.1 by John Arbash Meinel
First draft of a basic dump-btree command.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3770.1.1 by John Arbash Meinel
First draft of a basic dump-btree command.
16
#
17
18
"""Tests of the 'bzr dump-btree' command."""
19
20
from bzrlib import (
21
    btree_index,
22
    tests,
23
    )
24
from bzrlib.tests import (
25
    http_server,
26
    )
27
28
29
class TestDumpBtree(tests.TestCaseWithTransport):
30
31
    def create_sample_btree_index(self):
32
        builder = btree_index.BTreeBuilder(
33
            reference_lists=1, key_elements=2)
34
        builder.add_node(('test', 'key1'), 'value', ((('ref', 'entry'),),))
35
        builder.add_node(('test', 'key2'), 'value2', ((('ref', 'entry2'),),))
36
        builder.add_node(('test2', 'key3'), 'value3', ((('ref', 'entry3'),),))
37
        out_f = builder.finish()
38
        try:
39
            self.build_tree_contents([('test.btree', out_f.read())])
40
        finally:
41
            out_f.close()
42
43
    def test_dump_btree_smoke(self):
44
        self.create_sample_btree_index()
45
        out, err = self.run_bzr('dump-btree test.btree')
46
        self.assertEqualDiff(
47
            "(('test', 'key1'), 'value', ((('ref', 'entry'),),))\n"
48
            "(('test', 'key2'), 'value2', ((('ref', 'entry2'),),))\n"
49
            "(('test2', 'key3'), 'value3', ((('ref', 'entry3'),),))\n",
50
            out)
51
52
    def test_dump_btree_http_smoke(self):
53
        self.transport_readonly_server = http_server.HttpServer
54
        self.create_sample_btree_index()
55
        url = self.get_readonly_url('test.btree')
56
        out, err = self.run_bzr(['dump-btree', url])
57
        self.assertEqualDiff(
58
            "(('test', 'key1'), 'value', ((('ref', 'entry'),),))\n"
59
            "(('test', 'key2'), 'value2', ((('ref', 'entry2'),),))\n"
60
            "(('test2', 'key3'), 'value3', ((('ref', 'entry3'),),))\n",
61
            out)
3770.1.2 by John Arbash Meinel
Add a --raw output for dump-btree.
62
63
    def test_dump_btree_raw_smoke(self):
64
        self.create_sample_btree_index()
65
        out, err = self.run_bzr('dump-btree test.btree --raw')
66
        self.assertEqualDiff(
67
            'Root node:\n'
68
            'B+Tree Graph Index 2\n'
69
            'node_ref_lists=1\n'
70
            'key_elements=2\n'
71
            'len=3\n'
72
            'row_lengths=1\n'
73
            '\n'
3770.1.3 by John Arbash Meinel
Simplify the --raw mode.
74
            'Page 0\n'
3770.1.2 by John Arbash Meinel
Add a --raw output for dump-btree.
75
            'type=leaf\n'
76
            'test\0key1\0ref\0entry\0value\n'
77
            'test\0key2\0ref\0entry2\0value2\n'
78
            'test2\0key3\0ref\0entry3\0value3\n'
79
            '\n',
80
            out)
5091.1.2 by Andrew Bennetts
Add basic test.
81
82
    def test_dump_btree_no_refs_smoke(self):
83
        # A BTree index with no ref lists (such as *.cix) can be dumped without
84
        # errors.
85
        builder = btree_index.BTreeBuilder(
86
            reference_lists=0, key_elements=2)
87
        builder.add_node(('test', 'key1'), 'value')
88
        out_f = builder.finish()
89
        try:
90
            self.build_tree_contents([('test.btree', out_f.read())])
91
        finally:
92
            out_f.close()
93
        out, err = self.run_bzr('dump-btree test.btree')
5651.2.1 by Eric Siegerman
Fix traceback attempting to "bzr dump-btree --raw btree-with-0-rows".
94
95
    def create_sample_empty_btree_index(self):
96
        builder = btree_index.BTreeBuilder(
97
            reference_lists=1, key_elements=2)
98
        out_f = builder.finish()
99
        try:
100
            self.build_tree_contents([('test.btree', out_f.read())])
101
        finally:
102
            out_f.close()
103
104
    def test_dump_empty_btree_smoke(self):
105
        self.create_sample_empty_btree_index()
106
        out, err = self.run_bzr('dump-btree test.btree')
107
        self.assertEqualDiff("", out)
108
109
    def test_dump_empty_btree_http_smoke(self):
110
        self.transport_readonly_server = http_server.HttpServer
111
        self.create_sample_empty_btree_index()
112
        url = self.get_readonly_url('test.btree')
113
        out, err = self.run_bzr(['dump-btree', url])
114
        self.assertEqualDiff("", out)
115
116
    def test_dump_empty_btree_raw_smoke(self):
117
        self.create_sample_empty_btree_index()
118
        out, err = self.run_bzr('dump-btree test.btree --raw')
119
        self.assertEqualDiff(
120
            'Root node:\n'
121
            'B+Tree Graph Index 2\n'
122
            'node_ref_lists=1\n'
123
            'key_elements=2\n'
124
            'len=0\n'
125
            'row_lengths=\n'
126
            '\n'
127
            'Page 0\n'
128
            '(empty)\n',
129
            out)
130