summaryrefslogtreecommitdiff
path: root/test/git/test_odb.py
blob: b284071902fc0cb89530a3dbb09f5b8f236fa58c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Test for object db"""

from test.testlib import *
from git.odb.db import *
from git import Blob
from git.errors import BadObject

from cStringIO import StringIO
import os


class TestDB(TestBase):
	"""Test the different db class implementations"""
	
	# data
	two_lines = "1234\nhello world"
	
	all_data = (two_lines, )
	
	def _assert_object_writing(self, db):
		"""General tests to verify object writing, compatible to iObjectDBW
		:note: requires write access to the database"""
		# start in dry-run mode
		for dry_run in range(1, -1, -1):
			for data in self.all_data:
				for hex_sha in range(2):
					sha = db.to_object(Blob.type, len(data), StringIO(data), dry_run, hex_sha)
					assert db.has_object(sha) != dry_run
					assert len(sha) == 20 + hex_sha * 20
					
					# verify data - the slow way, we want to run code
					if not dry_run:
						type, size = db.object_info(sha)
						assert Blob.type == type
						assert size == len(data)
						
						type, size, stream = db.object(sha)
						assert stream.read() == data
					else:
						self.failUnlessRaises(BadObject, db.object_info, sha)
						self.failUnlessRaises(BadObject, db.object, sha)
				# END for each sha type
			# END for each data set
		# END for each dry_run mode
				
	@with_bare_rw_repo
	def test_writing(self, rwrepo):
		ldb = LooseObjectDB(os.path.join(rwrepo.git_dir, 'objects'))
		
		# write data
		self._assert_object_writing(ldb)