Fix index tests.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_index.py
1 # test_index.py -- Tests for the git index cache
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 import os
20 from unittest import TestCase
21
22 from dulwich.index import (
23     Index,
24     read_index,
25     write_index,
26     )
27
28 class IndexTestCase(TestCase):
29
30     datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
31
32     def get_simple_index(self, name):
33         return Index(os.path.join(self.datadir, name))
34
35
36 class SimpleIndexTestcase(IndexTestCase):
37
38     def test_len(self):
39         self.assertEquals(1, len(self.get_simple_index("index")))
40
41     def test_iter(self):
42         self.assertEquals(['bla'], list(self.get_simple_index("index")))
43
44     def test_getitem(self):
45         self.assertEquals( ((1230680220, 0), (1230680220, 0), 2050, 3761020, 33188, 1000, 1000, 0, '\xe6\x9d\xe2\x9b\xb2\xd1\xd6CK\x8b)\xaewZ\xd8\xc2\xe4\x8cS\x91', 3)
46             , 
47                 self.get_simple_index("index")["bla"])
48
49
50 class SimpleIndexWriterTestCase(IndexTestCase):
51
52     def test_simple_write(self):
53         entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020, 33188, 1000, 1000, 0, '\xe6\x9d\xe2\x9b\xb2\xd1\xd6CK\x8b)\xaewZ\xd8\xc2\xe4\x8cS\x91', 3)]
54         x = open('test-simple-write-index', 'w+')
55         try:
56             write_index(x, entries)
57         finally:
58             x.close()
59         x = open('test-simple-write-index', 'r')
60         try:
61             self.assertEquals(entries, list(read_index(x)))
62         finally:
63             x.close()
64