Add more tests.
[jelmer/dulwich.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 from unittest import TestCase
24
25 from dulwich.objects import (
26     Blob,
27     )
28 from dulwich.object_store import (
29     DiskObjectStore,
30     MemoryObjectStore,
31     )
32 import os
33 import shutil
34
35
36 testobject = Blob()
37 testobject.data = "yummy data"
38
39
40 class SpecificDiskObjectStoreTests(TestCase):
41
42     def test_pack_dir(self):
43         o = DiskObjectStore("foo")
44         self.assertEquals(os.path.join("foo", "pack"), o.pack_dir)
45
46     def test_empty_packs(self):
47         o = DiskObjectStore("foo")
48         self.assertEquals([], o.packs)
49
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, self.store.__getitem__, "a" * 40)
59
60     def test_contains_nonexistant(self):
61         self.assertFalse(self.store.__contains__("a" * 40))
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(self.store.__contains__(testobject.id))
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(self.store.__contains__(testobject.id))
83         r = self.store[testobject.id]
84         self.assertEquals(r, testobject)
85
86
87 class MemoryObjectStoreTests(ObjectStoreTests,TestCase):
88
89     def setUp(self):
90         TestCase.setUp(self)
91         self.store = MemoryObjectStore()
92
93
94 class DiskObjectStoreTests(ObjectStoreTests,TestCase):
95
96     def setUp(self):
97         TestCase.setUp(self)
98         if os.path.exists("foo"):
99             shutil.rmtree("foo")
100         os.makedirs(os.path.join("foo", "pack"))
101         self.store = DiskObjectStore("foo")