Clean up object store tests.
[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
20 """Tests for the object store interface."""
21
22
23 import shutil
24 import tempfile
25 from unittest import TestCase
26
27 from dulwich.objects import (
28     Blob,
29     )
30 from dulwich.object_store import (
31     DiskObjectStore,
32     MemoryObjectStore,
33     )
34 import os
35 import shutil
36 import tempfile
37
38
39 testobject = Blob()
40 testobject.data = "yummy data"
41
42
43 class ObjectStoreTests(object):
44
45     def test_iter(self):
46         self.assertEquals([], list(self.store))
47
48     def test_get_nonexistant(self):
49         self.assertRaises(KeyError, lambda: self.store["a" * 40])
50
51     def test_contains_nonexistant(self):
52         self.assertFalse(("a" * 40) in self.store)
53
54     def test_add_objects_empty(self):
55         self.store.add_objects([])
56
57     def test_add_commit(self):
58         # TODO: Argh, no way to construct Git commit objects without 
59         # access to a serialized form.
60         self.store.add_objects([])
61
62     def test_add_object(self):
63         self.store.add_object(testobject)
64         self.assertEquals(set([testobject.id]), set(self.store))
65         self.assertTrue(testobject.id in self.store)
66         r = self.store[testobject.id]
67         self.assertEquals(r, testobject)
68
69     def test_add_objects(self):
70         data = [(testobject, "mypath")]
71         self.store.add_objects(data)
72         self.assertEquals(set([testobject.id]), set(self.store))
73         self.assertTrue(testobject.id in self.store)
74         r = self.store[testobject.id]
75         self.assertEquals(r, testobject)
76
77
78 class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
79
80     def setUp(self):
81         TestCase.setUp(self)
82         self.store = MemoryObjectStore()
83
84
85 class DiskObjectStoreTests(ObjectStoreTests, TestCase):
86
87     def setUp(self):
88         TestCase.setUp(self)
89         self.store_dir = tempfile.mkdtemp()
90         self.store = DiskObjectStore.init(self.store_dir)
91
92     def tearDown(self):
93         TestCase.tearDown(self)
94         shutil.rmtree(self.store_dir)
95
96     def test_pack_dir(self):
97         o = DiskObjectStore(self.store_dir)
98         self.assertEquals(os.path.join(self.store_dir, "pack"), o.pack_dir)
99
100     def test_empty_packs(self):
101         o = DiskObjectStore(self.store_dir)
102         self.assertEquals([], o.packs)
103
104
105 # TODO: MissingObjectFinderTests