summaryrefslogtreecommitdiff
path: root/test/git/test_blob.py
blob: ebb53d0c0c4df81f8822ecdd3ba1e11ac81960c4 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# test_blob.py
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php

import time
from test.testlib import *
from git import *

class TestBlob(object):
	def setup(self):
		self.repo = Repo(GIT_REPO)
	
	@patch_object(Git, '_call_process')
	def test_should_return_blob_contents(self, git):
		git.return_value = fixture('cat_file_blob')
		blob = Blob(self.repo, **{'id': 'abc'})
		assert_equal("Hello world", blob.data)
		assert_true(git.called)
		assert_equal(git.call_args, (('cat_file', 'abc'), {'p': True, 'with_raw_output': True}))

	@patch_object(Git, '_call_process')
	def test_should_return_blob_contents_with_newline(self, git):
		git.return_value = fixture('cat_file_blob_nl')
		blob = Blob(self.repo, **{'id': 'abc'})
		assert_equal("Hello world\n", blob.data)
		assert_true(git.called)
		assert_equal(git.call_args, (('cat_file', 'abc'), {'p': True, 'with_raw_output': True}))
	
	@patch_object(Git, '_call_process')
	def test_should_cache_data(self, git):
		git.return_value = fixture('cat_file_blob')
		bid = '787b92b63f629398f3d2ceb20f7f0c2578259e84'
		blob = Blob(self.repo, bid)
		blob.data
		blob.data
		assert_true(git.called)
		assert_equal(git.call_count, 1)
		assert_equal(git.call_args, (('cat_file', bid), {'p': True, 'with_raw_output': True}))

	@patch_object(Git, '_call_process')
	def test_should_return_file_size(self, git):
		git.return_value = fixture('cat_file_blob_size')
		blob = Blob(self.repo, **{'id': 'abc'})
		assert_equal(11, blob.size)
		assert_true(git.called)
		assert_equal(git.call_args, (('cat_file', 'abc'), {'s': True}))

	@patch_object(Git, '_call_process')
	def test_should_cache_file_size(self, git):
		git.return_value = fixture('cat_file_blob_size')
		blob = Blob(self.repo, **{'id': 'abc'})
		assert_equal(11, blob.size)
		assert_equal(11, blob.size)
		assert_true(git.called)
		assert_equal(git.call_count, 1)
		assert_equal(git.call_args, (('cat_file', 'abc'), {'s': True}))
  
	def test_mime_type_should_return_mime_type_for_known_types(self):
		blob = Blob(self.repo, **{'id': 'abc', 'path': 'foo.png'})
		assert_equal("image/png", blob.mime_type)
  
	def test_mime_type_should_return_text_plain_for_unknown_types(self):
		blob = Blob(self.repo, **{'id': 'abc','path': 'something'})
		assert_equal("text/plain", blob.mime_type)
  
	def test_should_return_appropriate_representation(self):
		blob = Blob(self.repo, **{'id': 'abc'})
		assert_equal('<git.Blob "abc">', repr(blob))