Added Tag object testing to BlobReadTests
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_objects.py
1 # test_objects.py -- tests for objects.py
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
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 # 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 import unittest
21
22 from dulwich.objects import (Blob,
23                          Tree,
24                          Commit,
25                          Tag
26                          )
27
28 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
29 b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
30 c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
31 tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
32 tag_sha = '71033db03a03c6a36721efcf1968dd8f8e0cf023'
33
34 class BlobReadTests(unittest.TestCase):
35   """Test decompression of blobs"""
36
37   def get_sha_file(self, obj, base, sha):
38     return obj.from_file(os.path.join(os.path.dirname(__file__),
39                                       'data', base, sha))
40
41   def get_blob(self, sha):
42     """Return the blob named sha from the test data dir"""
43     return self.get_sha_file(Blob, 'blobs', sha)
44
45   def get_tree(self, sha):
46     return self.get_sha_file(Tree, 'trees', sha)
47
48   def get_tag(self, sha):
49     return self.get_sha_file(Tag, 'tags', sha)
50
51   def commit(self, sha):
52     return self.get_sha_file(Commit, 'commits', sha)
53
54   def test_decompress_simple_blob(self):
55     b = self.get_blob(a_sha)
56     self.assertEqual(b.data, 'test 1\n')
57     self.assertEqual(b.sha().hexdigest(), a_sha)
58
59   def test_parse_empty_blob_object(self):
60     sha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
61     b = self.get_blob(sha)
62     self.assertEqual(b.data, '')
63     self.assertEqual(b.sha().hexdigest(), sha)
64
65   def test_create_blob_from_string(self):
66     string = 'test 2\n'
67     b = Blob.from_string(string)
68     self.assertEqual(b.data, string)
69     self.assertEqual(b.sha().hexdigest(), b_sha)
70
71   def test_parse_legacy_blob(self):
72     string = 'test 3\n'
73     b = self.get_blob(c_sha)
74     self.assertEqual(b.data, string)
75     self.assertEqual(b.sha().hexdigest(), c_sha)
76
77   def test_eq(self):
78     blob1 = self.get_blob(a_sha)
79     blob2 = self.get_blob(a_sha)
80     self.assertEqual(blob1, blob2)
81
82   def test_read_tree_from_file(self):
83     t = self.get_tree(tree_sha)
84     self.assertEqual(t.entries()[0], (33188, 'a', a_sha))
85     self.assertEqual(t.entries()[1], (33188, 'b', b_sha))
86
87   def test_read_tag_from_file(self):
88     t = self.get_tag(tag_sha)
89     self.assertEqual(t.object, ('commit', '51b668fd5bf7061b7d6fa525f88803e6cfadaa51'))
90     self.assertEqual(t.name,'signed')
91     self.assertEqual(t.tagger,'Ali Sabil <ali.sabil@gmail.com>')
92     self.assertEqual(t.tag_time, 1231203091)
93     self.assertEqual(t.message, 'This is a signed tag\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n\niEYEABECAAYFAkliqx8ACgkQqSMmLy9u/kcx5ACfakZ9NnPl02tOyYP6pkBoEkU1\n5EcAn0UFgokaSvS371Ym/4W9iJj6vh3h\n=ql7y\n-----END PGP SIGNATURE-----\n')
94
95
96   def test_read_commit_from_file(self):
97     sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
98     c = self.commit(sha)
99     self.assertEqual(c.tree, tree_sha)
100     self.assertEqual(c.parents, ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
101     self.assertEqual(c.author,
102         'James Westby <jw+debian@jameswestby.net>')
103     self.assertEqual(c.committer,
104         'James Westby <jw+debian@jameswestby.net>')
105     self.assertEqual(c.commit_time, 1174759230)
106     self.assertEqual(c.message, 'Test commit\n')
107
108   def test_read_commit_no_parents(self):
109     sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
110     c = self.commit(sha)
111     self.assertEqual(c.tree, '90182552c4a85a45ec2a835cadc3451bebdfe870')
112     self.assertEqual(c.parents, [])
113     self.assertEqual(c.author,
114         'James Westby <jw+debian@jameswestby.net>')
115     self.assertEqual(c.committer,
116         'James Westby <jw+debian@jameswestby.net>')
117     self.assertEqual(c.commit_time, 1174758034)
118     self.assertEqual(c.message, 'Test commit\n')
119
120   def test_read_commit_two_parents(self):
121     sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
122     c = self.commit(sha)
123     self.assertEqual(c.tree, 'd80c186a03f423a81b39df39dc87fd269736ca86')
124     self.assertEqual(c.parents, ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
125                                    '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
126     self.assertEqual(c.author,
127         'James Westby <jw+debian@jameswestby.net>')
128     self.assertEqual(c.committer,
129         'James Westby <jw+debian@jameswestby.net>')
130     self.assertEqual(c.commit_time, 1174773719)
131     self.assertEqual(c.message, 'Merge ../b\n')
132