Delegate SHA peeling to the object store.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_object_store.py
1 # test_object_store.py -- tests for object_store.py
2 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
7 # or (at your option) any later version of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 # MA  02110-1301, USA.
18
19 """Tests for the object store interface."""
20
21
22 import os
23 import shutil
24 import tempfile
25
26 from dulwich.index import (
27     commit_tree,
28     )
29 from dulwich.objects import (
30     object_class,
31     Blob,
32     Tag,
33     )
34 from dulwich.object_store import (
35     DiskObjectStore,
36     MemoryObjectStore,
37     )
38 from dulwich.pack import (
39     write_pack_data,
40     )
41 from dulwich.tests import (
42     TestCase,
43     )
44 from utils import (
45     make_object,
46     )
47
48
49 testobject = make_object(Blob, data="yummy data")
50
51
52 class ObjectStoreTests(object):
53
54     def test_iter(self):
55         self.assertEquals([], list(self.store))
56
57     def test_get_nonexistant(self):
58         self.assertRaises(KeyError, lambda: self.store["a" * 40])
59
60     def test_contains_nonexistant(self):
61         self.assertFalse(("a" * 40) in self.store)
62
63     def test_add_objects_empty(self):
64         self.store.add_objects([])
65
66     def test_add_commit(self):
67         # TODO: Argh, no way to construct Git commit objects without 
68         # access to a serialized form.
69         self.store.add_objects([])
70
71     def test_add_object(self):
72         self.store.add_object(testobject)
73         self.assertEquals(set([testobject.id]), set(self.store))
74         self.assertTrue(testobject.id in self.store)
75         r = self.store[testobject.id]
76         self.assertEquals(r, testobject)
77
78     def test_add_objects(self):
79         data = [(testobject, "mypath")]
80         self.store.add_objects(data)
81         self.assertEquals(set([testobject.id]), set(self.store))
82         self.assertTrue(testobject.id in self.store)
83         r = self.store[testobject.id]
84         self.assertEquals(r, testobject)
85
86     def test_iter_tree_contents(self):
87         blob_a = make_object(Blob, data='a')
88         blob_b = make_object(Blob, data='b')
89         blob_c = make_object(Blob, data='c')
90         for blob in [blob_a, blob_b, blob_c]:
91             self.store.add_object(blob)
92
93         blobs = [
94           ('a', blob_a.id, 0100644),
95           ('ad/b', blob_b.id, 0100644),
96           ('ad/bd/c', blob_c.id, 0100755),
97           ('ad/c', blob_c.id, 0100644),
98           ('c', blob_c.id, 0100644),
99           ]
100         tree_id = commit_tree(self.store, blobs)
101         self.assertEquals([(p, m, h) for (p, h, m) in blobs],
102                           list(self.store.iter_tree_contents(tree_id)))
103
104     def test_iter_tree_contents_include_trees(self):
105         blob_a = make_object(Blob, data='a')
106         blob_b = make_object(Blob, data='b')
107         blob_c = make_object(Blob, data='c')
108         for blob in [blob_a, blob_b, blob_c]:
109             self.store.add_object(blob)
110
111         blobs = [
112           ('a', blob_a.id, 0100644),
113           ('ad/b', blob_b.id, 0100644),
114           ('ad/bd/c', blob_c.id, 0100755),
115           ]
116         tree_id = commit_tree(self.store, blobs)
117         tree = self.store[tree_id]
118         tree_ad = self.store[tree['ad'][1]]
119         tree_bd = self.store[tree_ad['bd'][1]]
120
121         expected = [
122           ('', 0040000, tree_id),
123           ('a', 0100644, blob_a.id),
124           ('ad', 0040000, tree_ad.id),
125           ('ad/b', 0100644, blob_b.id),
126           ('ad/bd', 0040000, tree_bd.id),
127           ('ad/bd/c', 0100755, blob_c.id),
128           ]
129         actual = self.store.iter_tree_contents(tree_id, include_trees=True)
130         self.assertEquals(expected, list(actual))
131
132     def make_tag(self, name, obj):
133         tag = make_object(Tag, name=name, message='',
134                           tag_time=12345, tag_timezone=0,
135                           tagger='Test Tagger <test@example.com>',
136                           object=(object_class(obj.type_name), obj.id))
137         self.store.add_object(tag)
138         return tag
139
140     def test_peel_sha(self):
141         self.store.add_object(testobject)
142         tag1 = self.make_tag('1', testobject)
143         tag2 = self.make_tag('2', testobject)
144         tag3 = self.make_tag('3', testobject)
145         for obj in [testobject, tag1, tag2, tag3]:
146             self.assertEqual(testobject, self.store.peel_sha(obj.id))
147
148
149 class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
150
151     def setUp(self):
152         TestCase.setUp(self)
153         self.store = MemoryObjectStore()
154
155
156 class PackBasedObjectStoreTests(ObjectStoreTests):
157
158     def test_empty_packs(self):
159         self.assertEquals([], self.store.packs)
160
161     def test_pack_loose_objects(self):
162         b1 = make_object(Blob, data="yummy data")
163         self.store.add_object(b1)
164         b2 = make_object(Blob, data="more yummy data")
165         self.store.add_object(b2)
166         self.assertEquals([], self.store.packs)
167         self.assertEquals(2, self.store.pack_loose_objects())
168         self.assertNotEquals([], self.store.packs)
169         self.assertEquals(0, self.store.pack_loose_objects())
170
171
172 class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
173
174     def setUp(self):
175         TestCase.setUp(self)
176         self.store_dir = tempfile.mkdtemp()
177         self.store = DiskObjectStore.init(self.store_dir)
178
179     def tearDown(self):
180         TestCase.tearDown(self)
181         shutil.rmtree(self.store_dir)
182
183     def test_pack_dir(self):
184         o = DiskObjectStore(self.store_dir)
185         self.assertEquals(os.path.join(self.store_dir, "pack"), o.pack_dir)
186
187     def test_add_pack(self):
188         o = DiskObjectStore(self.store_dir)
189         f, commit = o.add_pack()
190         b = make_object(Blob, data="more yummy data")
191         write_pack_data(f, [(b, None)], 1)
192         commit()
193
194     def test_add_thin_pack(self):
195         o = DiskObjectStore(self.store_dir)
196         f, commit = o.add_thin_pack()
197         b = make_object(Blob, data="more yummy data")
198         write_pack_data(f, [(b, None)], 1)
199         commit()
200
201
202 # TODO: MissingObjectFinderTests