1 # test_pack.py -- Tests for the handling of git packs.
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
3 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; version 2
8 # of the License, or (at your option) any later version of the license.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 from dulwich.pack import (
30 pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
32 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
33 tree_sha = 'b2a2766a2879c209ab1176e7e778b81ae422eeaa'
34 commit_sha = 'f18faa16531ac570a3fdc8c7ca16682548dafd12'
36 class PackTests(unittest.TestCase):
37 """Base class for testing packs"""
39 datadir = os.path.join(os.path.dirname(__file__), 'data/packs')
41 def get_pack_index(self, sha):
42 """Returns a PackIndex from the datadir with the given sha"""
43 return PackIndex(os.path.join(self.datadir, 'pack-%s.idx' % sha))
45 def get_pack_data(self, sha):
46 """Returns a PackData object from the datadir with the given sha"""
47 return PackData(os.path.join(self.datadir, 'pack-%s.pack' % sha))
50 class PackIndexTests(PackTests):
51 """Class that tests the index of packfiles"""
53 def test_object_index(self):
54 """Tests that the correct object offset is returned from the index."""
55 p = self.get_pack_index(pack1_sha)
56 self.assertEqual(p.object_index(pack1_sha), None)
57 self.assertEqual(p.object_index(a_sha), 178)
58 self.assertEqual(p.object_index(tree_sha), 138)
59 self.assertEqual(p.object_index(commit_sha), 12)
62 class TestPackData(PackTests):
63 """Tests getting the data from the packfile."""
65 def test_create_pack(self):
66 p = self.get_pack_data(pack1_sha)
68 def test_get_object_at(self):
69 """Tests random access for non-delta objects"""
70 p = self.get_pack_data(pack1_sha)
71 idx = self.get_pack_index(pack1_sha)
72 obj = p.get_object_at(idx.object_index(a_sha))
73 self.assertEqual(obj._type, 'blob')
74 self.assertEqual(obj.sha().hexdigest(), a_sha)
75 obj = p.get_object_at(idx.object_index(tree_sha))
76 self.assertEqual(obj._type, 'tree')
77 self.assertEqual(obj.sha().hexdigest(), tree_sha)
78 obj = p.get_object_at(idx.object_index(commit_sha))
79 self.assertEqual(obj._type, 'commit')
80 self.assertEqual(obj.sha().hexdigest(), commit_sha)
83 p = self.get_pack_data(pack1_sha)
84 self.assertEquals(3, len(p))
87 class TestHexToSha(unittest.TestCase):
89 def test_simple(self):
90 self.assertEquals(703710, hex_to_sha("abcde"))
93 class TestMultiOrd(unittest.TestCase):
95 def test_simple(self):
96 self.assertEquals(418262508645L, multi_ord("abcde", 0, 5))