/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tests/test_object_store.py

Various minor fixes.

Merged from https://code.launchpad.net/~jelmer/brz-git/misc-2/+merge/342482

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for bzr-git's object store."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from dulwich.objects import (
 
22
    Blob,
 
23
    )
 
24
 
 
25
from ....branchbuilder import (
 
26
    BranchBuilder,
 
27
    )
 
28
from ....errors import (
 
29
    NoSuchRevision,
 
30
    )
 
31
from ....graph import (
 
32
    DictParentsProvider,
 
33
    Graph,
 
34
    )
 
35
from ....tests import (
 
36
    TestCase,
 
37
    TestCaseWithTransport,
 
38
    )
 
39
 
 
40
from ..cache import (
 
41
    DictGitShaMap,
 
42
    )
 
43
from ..object_store import (
 
44
    BazaarObjectStore,
 
45
    LRUTreeCache,
 
46
    _check_expected_sha,
 
47
    _find_missing_bzr_revids,
 
48
    _tree_to_objects,
 
49
    )
 
50
 
 
51
 
 
52
class ExpectedShaTests(TestCase):
 
53
 
 
54
    def setUp(self):
 
55
        super(ExpectedShaTests, self).setUp()
 
56
        self.obj = Blob()
 
57
        self.obj.data = "foo"
 
58
 
 
59
    def test_none(self):
 
60
        _check_expected_sha(None, self.obj)
 
61
 
 
62
    def test_hex(self):
 
63
        _check_expected_sha(self.obj.sha().hexdigest(), self.obj)
 
64
        self.assertRaises(AssertionError, _check_expected_sha, 
 
65
            "0" * 40, self.obj)
 
66
 
 
67
    def test_binary(self):
 
68
        _check_expected_sha(self.obj.sha().digest(), self.obj)
 
69
        self.assertRaises(AssertionError, _check_expected_sha, 
 
70
            "x" * 20, self.obj)
 
71
 
 
72
 
 
73
class FindMissingBzrRevidsTests(TestCase):
 
74
 
 
75
    def _find_missing(self, ancestry, want, have):
 
76
        return _find_missing_bzr_revids(
 
77
            Graph(DictParentsProvider(ancestry)),
 
78
            set(want), set(have))
 
79
 
 
80
    def test_simple(self):
 
81
        self.assertEquals(set(), self._find_missing({}, [], []))
 
82
 
 
83
    def test_up_to_date(self):
 
84
        self.assertEquals(set(),
 
85
                self._find_missing({"a": ["b"]}, ["a"], ["a"]))
 
86
 
 
87
    def test_one_missing(self):
 
88
        self.assertEquals(set(["a"]),
 
89
                self._find_missing({"a": ["b"]}, ["a"], ["b"]))
 
90
 
 
91
    def test_two_missing(self):
 
92
        self.assertEquals(set(["a", "b"]),
 
93
                self._find_missing({"a": ["b"], "b": ["c"]}, ["a"], ["c"]))
 
94
 
 
95
    def test_two_missing_history(self):
 
96
        self.assertEquals(set(["a", "b"]),
 
97
                self._find_missing({"a": ["b"], "b": ["c"], "c": ["d"]},
 
98
                    ["a"], ["c"]))
 
99
 
 
100
 
 
101
class LRUTreeCacheTests(TestCaseWithTransport):
 
102
 
 
103
    def setUp(self):
 
104
        super(LRUTreeCacheTests, self).setUp()
 
105
        self.branch = self.make_branch(".")
 
106
        self.branch.lock_write()
 
107
        self.addCleanup(self.branch.unlock)
 
108
        self.cache = LRUTreeCache(self.branch.repository)
 
109
 
 
110
    def test_get_not_present(self):
 
111
        self.assertRaises(NoSuchRevision, self.cache.revision_tree,
 
112
                "unknown")
 
113
 
 
114
    def test_revision_trees(self):
 
115
        self.assertRaises(NoSuchRevision, self.cache.revision_trees,
 
116
                ["unknown", "la"])
 
117
 
 
118
    def test_iter_revision_trees(self):
 
119
        self.assertRaises(NoSuchRevision, self.cache.iter_revision_trees,
 
120
                ["unknown", "la"])
 
121
 
 
122
    def test_get(self):
 
123
        bb = BranchBuilder(branch=self.branch)
 
124
        bb.start_series()
 
125
        revid = bb.build_snapshot(None,
 
126
            [('add', ('', None, 'directory', None)),
 
127
             ('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
 
128
             ])
 
129
        bb.finish_series()
 
130
        tree = self.cache.revision_tree(revid)
 
131
        self.assertEquals(revid, tree.get_revision_id())
 
132
 
 
133
 
 
134
class BazaarObjectStoreTests(TestCaseWithTransport):
 
135
 
 
136
    def setUp(self):
 
137
        super(BazaarObjectStoreTests, self).setUp()
 
138
        self.branch = self.make_branch(".")
 
139
        self.branch.lock_write()
 
140
        self.addCleanup(self.branch.unlock)
 
141
        self.store = BazaarObjectStore(self.branch.repository)
 
142
 
 
143
    def test_get_blob(self):
 
144
        b = Blob()
 
145
        b.data = 'a\nb\nc\nd\ne\n'
 
146
        self.store.lock_read()
 
147
        self.addCleanup(self.store.unlock)
 
148
        self.assertRaises(KeyError, self.store.__getitem__, b.id)
 
149
        bb = BranchBuilder(branch=self.branch)
 
150
        bb.start_series()
 
151
        bb.build_snapshot(None,
 
152
            [('add', ('', None, 'directory', None)),
 
153
             ('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
 
154
             ])
 
155
        bb.finish_series()
 
156
        # read locks cache
 
157
        self.assertRaises(KeyError, self.store.__getitem__, b.id)
 
158
        self.store.unlock()
 
159
        self.store.lock_read()
 
160
        self.assertEquals(b, self.store[b.id])
 
161
 
 
162
    def test_get_raw(self):
 
163
        b = Blob()
 
164
        b.data = 'a\nb\nc\nd\ne\n'
 
165
        self.store.lock_read()
 
166
        self.addCleanup(self.store.unlock)
 
167
        self.assertRaises(KeyError, self.store.get_raw, b.id)
 
168
        bb = BranchBuilder(branch=self.branch)
 
169
        bb.start_series()
 
170
        bb.build_snapshot(None,
 
171
            [('add', ('', None, 'directory', None)),
 
172
             ('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
 
173
             ])
 
174
        bb.finish_series()
 
175
        # read locks cache
 
176
        self.assertRaises(KeyError, self.store.get_raw, b.id)
 
177
        self.store.unlock()
 
178
        self.store.lock_read()
 
179
        self.assertEquals(b.as_raw_string(), self.store.get_raw(b.id)[1])
 
180
 
 
181
    def test_contains(self):
 
182
        b = Blob()
 
183
        b.data = 'a\nb\nc\nd\ne\n'
 
184
        self.store.lock_read()
 
185
        self.addCleanup(self.store.unlock)
 
186
        self.assertFalse(b.id in self.store)
 
187
        bb = BranchBuilder(branch=self.branch)
 
188
        bb.start_series()
 
189
        bb.build_snapshot(None,
 
190
            [('add', ('', None, 'directory', None)),
 
191
             ('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
 
192
             ])
 
193
        bb.finish_series()
 
194
        # read locks cache
 
195
        self.assertFalse(b.id in self.store)
 
196
        self.store.unlock()
 
197
        self.store.lock_read()
 
198
        self.assertTrue(b.id in self.store)
 
199
 
 
200
 
 
201
class TreeToObjectsTests(TestCaseWithTransport):
 
202
 
 
203
    def setUp(self):
 
204
        super(TreeToObjectsTests, self).setUp()
 
205
        self.idmap = DictGitShaMap()
 
206
 
 
207
    def test_no_changes(self):
 
208
        tree = self.make_branch_and_tree('.')
 
209
        self.addCleanup(tree.lock_read().unlock)
 
210
        entries = list(_tree_to_objects(tree, [tree], self.idmap, {}))
 
211
        self.assertEquals([], entries)