1 # test_object_store.py -- tests for object_store.py
2 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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,
19 """Tests for the object store interface."""
26 from dulwich.index import (
29 from dulwich.objects import (
32 from dulwich.object_store import (
36 from dulwich.pack import (
39 from dulwich.tests import (
47 testobject = make_object(Blob, data="yummy data")
50 class ObjectStoreTests(object):
53 self.assertEquals([], list(self.store))
55 def test_get_nonexistant(self):
56 self.assertRaises(KeyError, lambda: self.store["a" * 40])
58 def test_contains_nonexistant(self):
59 self.assertFalse(("a" * 40) in self.store)
61 def test_add_objects_empty(self):
62 self.store.add_objects([])
64 def test_add_commit(self):
65 # TODO: Argh, no way to construct Git commit objects without
66 # access to a serialized form.
67 self.store.add_objects([])
69 def test_add_object(self):
70 self.store.add_object(testobject)
71 self.assertEquals(set([testobject.id]), set(self.store))
72 self.assertTrue(testobject.id in self.store)
73 r = self.store[testobject.id]
74 self.assertEquals(r, testobject)
76 def test_add_objects(self):
77 data = [(testobject, "mypath")]
78 self.store.add_objects(data)
79 self.assertEquals(set([testobject.id]), set(self.store))
80 self.assertTrue(testobject.id in self.store)
81 r = self.store[testobject.id]
82 self.assertEquals(r, testobject)
84 def test_iter_tree_contents(self):
85 blob_a = make_object(Blob, data='a')
86 blob_b = make_object(Blob, data='b')
87 blob_c = make_object(Blob, data='c')
88 for blob in [blob_a, blob_b, blob_c]:
89 self.store.add_object(blob)
92 ('a', blob_a.id, 0100644),
93 ('ad/b', blob_b.id, 0100644),
94 ('ad/bd/c', blob_c.id, 0100755),
95 ('ad/c', blob_c.id, 0100644),
96 ('c', blob_c.id, 0100644),
98 tree_id = commit_tree(self.store, blobs)
99 self.assertEquals([(p, m, h) for (p, h, m) in blobs],
100 list(self.store.iter_tree_contents(tree_id)))
102 def test_iter_tree_contents_include_trees(self):
103 blob_a = make_object(Blob, data='a')
104 blob_b = make_object(Blob, data='b')
105 blob_c = make_object(Blob, data='c')
106 for blob in [blob_a, blob_b, blob_c]:
107 self.store.add_object(blob)
110 ('a', blob_a.id, 0100644),
111 ('ad/b', blob_b.id, 0100644),
112 ('ad/bd/c', blob_c.id, 0100755),
114 tree_id = commit_tree(self.store, blobs)
115 tree = self.store[tree_id]
116 tree_ad = self.store[tree['ad'][1]]
117 tree_bd = self.store[tree_ad['bd'][1]]
120 ('', 0040000, tree_id),
121 ('a', 0100644, blob_a.id),
122 ('ad', 0040000, tree_ad.id),
123 ('ad/b', 0100644, blob_b.id),
124 ('ad/bd', 0040000, tree_bd.id),
125 ('ad/bd/c', 0100755, blob_c.id),
127 actual = self.store.iter_tree_contents(tree_id, include_trees=True)
128 self.assertEquals(expected, list(actual))
131 class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
135 self.store = MemoryObjectStore()
138 class PackBasedObjectStoreTests(ObjectStoreTests):
140 def test_empty_packs(self):
141 self.assertEquals([], self.store.packs)
143 def test_pack_loose_objects(self):
144 b1 = make_object(Blob, data="yummy data")
145 self.store.add_object(b1)
146 b2 = make_object(Blob, data="more yummy data")
147 self.store.add_object(b2)
148 self.assertEquals([], self.store.packs)
149 self.assertEquals(2, self.store.pack_loose_objects())
150 self.assertNotEquals([], self.store.packs)
151 self.assertEquals(0, self.store.pack_loose_objects())
154 class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
158 self.store_dir = tempfile.mkdtemp()
159 self.store = DiskObjectStore.init(self.store_dir)
162 TestCase.tearDown(self)
163 shutil.rmtree(self.store_dir)
165 def test_pack_dir(self):
166 o = DiskObjectStore(self.store_dir)
167 self.assertEquals(os.path.join(self.store_dir, "pack"), o.pack_dir)
169 def test_add_pack(self):
170 o = DiskObjectStore(self.store_dir)
171 f, commit = o.add_pack()
172 b = make_object(Blob, data="more yummy data")
173 write_pack_data(f, [(b, None)], 1)
176 def test_add_thin_pack(self):
177 o = DiskObjectStore(self.store_dir)
178 f, commit = o.add_thin_pack()
179 b = make_object(Blob, data="more yummy data")
180 write_pack_data(f, [(b, None)], 1)
184 # TODO: MissingObjectFinderTests