/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
1
# Copyright (C) 2008 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for maps built on a CHK versionedfiles facility."""
18
19
from bzrlib.chk_map import CHKMap, RootNode, ValueNode
20
from bzrlib.tests import TestCaseWithTransport
21
22
23
class TestDumbMap(TestCaseWithTransport):
24
25
    def get_chk_bytes(self):
26
        # The eassiest way to get a CHK store is a development3 repository and
27
        # then work with the chk_bytes attribute directly.
28
        repo = self.make_repository(".", format="development3")
29
        repo.lock_write()
30
        self.addCleanup(repo.unlock)
31
        repo.start_write_group()
32
        self.addCleanup(repo.abort_write_group)
33
        return repo.chk_bytes
34
35
    def read_bytes(self, chk_bytes, key):
36
        stream = chk_bytes.get_record_stream([key], 'unordered', True)
37
        return stream.next().get_bytes_as("fulltext")
38
39
    def assertHasABMap(self, chk_bytes):
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
40
        root_key = ('sha1:674c986f86eacc7d9e4fbee45eda625e5689f9a4',)
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
41
        self.assertEqual(
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
42
            "chkroot:\n1\na\x00sha1:cb29f32e561a1b7f862c38ccfd6bc7c7d892f04b\n",
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
43
            self.read_bytes(chk_bytes, root_key))
44
        self.assertEqual(
45
            "chkvalue:\nb",
46
            self.read_bytes(chk_bytes,
47
                ("sha1:cb29f32e561a1b7f862c38ccfd6bc7c7d892f04b",)))
48
49
    def assertHasEmptyMap(self, chk_bytes):
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
50
        root_key = ('sha1:8e13ef930ff710425830ffd5077146b259b49534',)
51
        self.assertEqual("chkroot:\n0\n", self.read_bytes(chk_bytes, root_key))
52
53
    def _get_map(self, a_dict):
54
        chk_bytes = self.get_chk_bytes()
55
        root_key = CHKMap.from_dict(chk_bytes, a_dict)
56
        chkmap = CHKMap(chk_bytes, root_key)
57
        return chkmap
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
58
59
    def test_from_dict_empty(self):
60
        chk_bytes = self.get_chk_bytes()
61
        root_key = CHKMap.from_dict(chk_bytes, {})
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
62
        self.assertEqual(('sha1:8e13ef930ff710425830ffd5077146b259b49534',),
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
63
            root_key)
64
        self.assertHasEmptyMap(chk_bytes)
65
66
    def test_from_dict_ab(self):
67
        chk_bytes = self.get_chk_bytes()
68
        root_key = CHKMap.from_dict(chk_bytes, {"a":"b"})
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
69
        self.assertEqual(('sha1:674c986f86eacc7d9e4fbee45eda625e5689f9a4',),
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
70
            root_key)
71
        self.assertHasABMap(chk_bytes)
72
73
    def test_apply_empty_ab(self):
74
        # applying a delta (None, "a", "b") to an empty chkmap generates the
75
        # same map as from_dict_ab.
76
        chk_bytes = self.get_chk_bytes()
77
        root_key = CHKMap.from_dict(chk_bytes, {})
78
        chkmap = CHKMap(chk_bytes, root_key)
79
        new_root = chkmap.apply_delta([(None, "a", "b")])
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
80
        self.assertEqual(('sha1:674c986f86eacc7d9e4fbee45eda625e5689f9a4',),
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
81
            new_root)
82
        self.assertHasABMap(chk_bytes)
83
        # The update should have left us with an in memory root node, with an
84
        # updated key.
85
        self.assertEqual(new_root, chkmap._root_node._key)
86
87
    def test_apply_ab_empty(self):
88
        # applying a delta ("a", None, None) to an empty chkmap generates the
89
        # same map as from_dict_ab.
90
        chk_bytes = self.get_chk_bytes()
91
        root_key = CHKMap.from_dict(chk_bytes, {"a":"b"})
92
        chkmap = CHKMap(chk_bytes, root_key)
93
        new_root = chkmap.apply_delta([("a", None, None)])
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
94
        self.assertEqual(('sha1:8e13ef930ff710425830ffd5077146b259b49534',),
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
95
            new_root)
96
        self.assertHasEmptyMap(chk_bytes)
97
        # The update should have left us with an in memory root node, with an
98
        # updated key.
99
        self.assertEqual(new_root, chkmap._root_node._key)
100
101
    def test_iteritems_empty(self):
102
        chk_bytes = self.get_chk_bytes()
103
        root_key = CHKMap.from_dict(chk_bytes, {})
104
        chkmap = CHKMap(chk_bytes, root_key)
105
        self.assertEqual([], list(chkmap.iteritems()))
106
107
    def test_iteritems_two_items(self):
108
        chk_bytes = self.get_chk_bytes()
109
        root_key = CHKMap.from_dict(chk_bytes,
110
            {"a":"content here", "b":"more content"})
111
        chkmap = CHKMap(chk_bytes, root_key)
112
        self.assertEqual([("a", "content here"), ("b", "more content")],
113
            sorted(list(chkmap.iteritems())))
114
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
115
    def test_iteritems_selected_one_of_two_items(self):
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
116
        chkmap = self._get_map( {"a":"content here", "b":"more content"})
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
117
        self.assertEqual([("a", "content here")],
118
            sorted(list(chkmap.iteritems(["a"]))))
119
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
120
    def test___len__empty(self):
121
        chkmap = self._get_map({})
122
        self.assertEqual(0, len(chkmap))
123
124
    def test___len__2(self):
125
        chkmap = self._get_map( {"foo":"bar", "gam":"quux"})
126
        self.assertEqual(2, len(chkmap))
127
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
128
129
class TestRootNode(TestCaseWithTransport):
130
131
    def test_serialise_empty(self):
132
        node = RootNode()
133
        bytes = node.serialise()
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
134
        self.assertEqual("chkroot:\n0\n", bytes)
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
135
136
    def test_add_child_resets_key(self):
137
        node = RootNode()
138
        node._key = ("something",)
139
        node.add_child("c", ("sha1:1234",))
140
        self.assertEqual(None, node._key)
141
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
142
    def test_add_child_increases_len(self):
143
        node = RootNode()
144
        node._key = ("something",)
145
        node.add_child("c", ("sha1:1234",))
146
        self.assertEqual(1, len(node))
147
148
    def test_remove_child_decreases_len(self):
149
        node = RootNode()
150
        node.add_child("c", ("sha1:1234",))
151
        node._key = ("something",)
152
        node.remove_child("c")
153
        self.assertEqual(0, len(node))
154
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
155
    def test_remove_child_removes_child(self):
156
        node = RootNode()
157
        node.add_child("a", ("sha1:4321",))
158
        node.add_child("c", ("sha1:1234",))
159
        node._key = ("something",)
160
        node.remove_child("a")
161
        self.assertEqual({"c":("sha1:1234",)}, node._nodes)
162
163
    def test_remove_child_resets_key(self):
164
        node = RootNode()
165
        node.add_child("c", ("sha1:1234",))
166
        node._key = ("something",)
167
        node.remove_child("c")
168
        self.assertEqual(None, node._key)
169
170
    def test_deserialise(self):
171
        # deserialising from a bytestring & key sets the nodes and the known
172
        # key.
173
        node = RootNode()
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
174
        node.deserialise("chkroot:\n1\nc\x00sha1:1234\n", ("foo",))
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
175
        self.assertEqual({"c": ("sha1:1234",)}, node._nodes)
176
        self.assertEqual(("foo",), node._key)
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
177
        self.assertEqual(1, len(node))
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
178
179
    def test_serialise_with_child(self):
180
        node = RootNode()
181
        node.add_child("c", ("sha1:1234",))
182
        bytes = node.serialise()
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
183
        self.assertEqual("chkroot:\n1\nc\x00sha1:1234\n", bytes)
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
184
185
186
class TestValueNode(TestCaseWithTransport):
187
188
    def test_deserialise(self):
189
        node = ValueNode.deserialise("chkvalue:\nfoo bar baz\n")
190
        self.assertEqual("foo bar baz\n", node.value)
191
192
    def test_serialise(self):
193
        node = ValueNode("b")
194
        bytes = node.serialise()
195
        self.assertEqual("chkvalue:\nb", bytes)