本文整理汇总了Python中unittest.mock.MagicMock.hash方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.hash方法的具体用法?Python MagicMock.hash怎么用?Python MagicMock.hash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.hash方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_file_content_headers
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_file_content_headers(self):
# === Initialization ===
display_name = 'test123'
extension = 'png'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
full_display_name = display_name + "." + extension
expected_content_type = "image/png"
expected_content_disposition = 'inline; filename="{}"'.format(
full_display_name)
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
_file.full_display_name = full_display_name
# === Service function call ===
with patch.object(file_service, 'get_file_mimetype',
return_value=expected_content_type):
headers = file_service.get_file_content_headers(_file)
# === Assertions ===
assert "Content-Type" in headers
assert "Content-Disposition" in headers
self.assertEqual(headers["Content-Type"], expected_content_type)
self.assertEqual(headers["Content-Disposition"],
expected_content_disposition)
示例2: test_get_file_content_headers_no_display_name
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_file_content_headers_no_display_name(self):
# === Initialization ===
display_name = None
extension = 'png'
category = FileCategory.ACTIVITY_PICTURE
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
full_display_name = None
expected_content_type = "image/png"
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
_file.full_display_name = full_display_name
# === Service function call ===
with patch.object(file_service, 'get_file_mimetype',
return_value=expected_content_type):
headers = file_service.get_file_content_headers(_file)
# === Assertions ===
assert "Content-Type" in headers
assert "Content-Disposition" not in headers
self.assertEqual(headers["Content-Type"], expected_content_type)
示例3: test_get_file_mimetype_txt
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_file_mimetype_txt(self):
# === Initialization ===
display_name = 'test123'
extension = 'txt'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
expected_mimetype = 'text/plain'
expected_mimetype_charset = 'text/plain; charset=utf-8'
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
# === Service function call ===
mimetype = file_service.get_file_mimetype(
_file, add_http_text_charset=None)
mimetype_charset = file_service.get_file_mimetype(_file)
# === Assertions ===
self.assertEqual(mimetype, expected_mimetype)
self.assertEqual(mimetype_charset, expected_mimetype_charset)
示例4: test_get_file_content_invalid_hash
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_file_content_invalid_hash(self):
# === Initialization ===
display_name = 'test123'
extension = 'png'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
# === Set return values ===
def mock_open(_hash):
raise IOError()
hashfs_mock.open.side_effect = mock_open
# === Service function call ===
with self.assertRaises(ResourceNotFoundException):
file_service.get_file_content(_file)
hashfs_mock.open.reset_mock(side_effect=True)
示例5: test_get_file_content
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_file_content(self):
# === Initialization ===
display_name = 'test123'
extension = 'png'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
data = '1eb55d09d99d4d0686581a7fdb8a3346'
data_reader = StringIO(data)
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
# === Set return values ===
hashfs_mock.open.return_value = data_reader
# === Service function call ===
content = file_service.get_file_content(_file)
# === Assertions ===
self.assertEqual(data, content)
示例6: test_get_file_by_id
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_file_by_id(self):
# === Initialization ===
display_name = 'test123'
extension = 'png'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
expected_file = MagicMock(spec=dir(File))
expected_file.id = 1
expected_file.hash = _hash
expected_file.category = category
expected_file.display_name = display_name
expected_file.extension = extension
# === Set return values ===
file_repository_mock.find_file_by_id.return_value = expected_file
# === Service function call ===
_file = file_service.get_file_by_id(expected_file.id)
# === Assertions ===
self.assertEqual(expected_file, _file)
示例7: test_delete_file_no_duplicates
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_delete_file_no_duplicates(self):
# === Initialization ===
display_name = 'test123'
extension = 'png'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
# === Set return values ===
file_repository_mock.find_all_files_by_hash.return_value = []
# === Service function call ===
file_service.delete_file(_file)
# === Assertions ===
file_repository_mock.delete.assert_called_once_with(_file)
file_repository_mock.find_all_files_by_hash \
.assert_called_once_with(_hash)
hashfs_mock.delete.assert_called_once_with(_hash)
示例8: test_get_all_files_in_category_with_pages
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_all_files_in_category_with_pages(self):
# === Initialization ===
files_uploads = []
files_activity_pictures = []
find_category = FileCategory.ACTIVITY_PICTURE
page_nr = 2
per_page = 3
for i in range(10):
if i < 5:
category = FileCategory.UPLOADS
display_name = 'test{}'.format(i)
else:
category = FileCategory.ACTIVITY_PICTURE
display_name = None
extension = 'png'
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
_file = MagicMock(spec=dir(File))
_file.id = i + 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
if category == FileCategory.UPLOADS:
files_uploads.append(_file)
else:
files_activity_pictures.append(_file)
expected_result = files_activity_pictures[per_page * (page_nr - 1):
per_page * page_nr]
# === Set return values ===
file_repository_mock.find_all_files_by_category.return_value = \
expected_result
# === Service function call ===
result = file_service.get_all_files_in_category(
find_category, page_nr=page_nr, per_page=per_page)
# === Assertions ===
self.assertEqual(set(result), set(expected_result))
file_repository_mock.find_all_files_by_category \
.assert_called_with(find_category, page_nr, per_page)
示例9: test_get_all_files_in_category
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_all_files_in_category(self):
# === Initialization ===
files_uploads = []
files_activity_pictures = []
find_category = FileCategory.ACTIVITY_PICTURE
find_category_empty = FileCategory.ALV_DOCUMENT
for i in range(10):
if i < 5:
category = FileCategory.UPLOADS
display_name = 'test{}'.format(i)
else:
category = FileCategory.ACTIVITY_PICTURE
display_name = None
extension = 'png'
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
_file = MagicMock(spec=dir(File))
_file.id = i + 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
if category == FileCategory.UPLOADS:
files_uploads.append(_file)
else:
files_activity_pictures.append(_file)
# === Test with category with files ===
file_repository_mock.find_all_files_by_category.return_value = \
files_activity_pictures
result = file_service.get_all_files_in_category(find_category)
self.assertEqual(set(result), set(files_activity_pictures))
file_repository_mock.find_all_files_by_category \
.assert_called_once_with(find_category, None, None)
# === Test with empty category ===
file_repository_mock.find_all_files_by_category.return_value = []
result = file_service.get_all_files_in_category(find_category_empty)
self.assertEqual(len(result), 0)
file_repository_mock.find_all_files_by_category \
.assert_called_with(find_category_empty, None, None)
示例10: search_test_case
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def search_test_case(filenames, search,
expect_in_result,
expect_ordering=None):
files = []
for i, fn in enumerate(filenames):
[display_name, extension] = fn.split('.')
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
_file = MagicMock(spec=dir(File))
_file.id = i + 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
_file.full_display_name = fn
files.append(_file)
file_repository_mock.find_all_files_by_category.return_value = files
search_results = file_service.search_files_in_uploads(search)
search_results_filenames = [r.full_display_name
for r in search_results]
expect_in_result = set(expect_in_result)
for fn in filenames:
if fn in expect_in_result:
assert fn in search_results_filenames, fn
else:
assert fn not in search_results_filenames, fn
if expect_ordering:
# Test if we have the same ordering in the result
prev_index = -1
for fn in expect_ordering:
assert fn in expect_in_result, fn
index = search_results_filenames.index(fn)
assert index > prev_index, fn
prev_index = index
示例11: test_file_full_display_name_no_extension
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_file_full_display_name_no_extension(self):
# === Initialization ===
display_name = 'test123'
extension = ''
expected_full_display_name = display_name
# == Property __get__ call ==
f = MagicMock(spec=dir(File))
f.display_name = display_name
f.extension = extension
f.hash = '123456789abcdefghiklmnopqrstuvwxyz'
f.category = FileCategory.UPLOADS
full_display_name = File.full_display_name.__get__(f)
# == Assertions
self.assertEqual(expected_full_display_name, full_display_name)
示例12: test_get_image_with_headers_thumbnail
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_image_with_headers_thumbnail(self):
# === Initialization ===
display_name = 'test123'
extension = 'png'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
# === Service function call ===
with patch.object(file_service, 'get_thumbnail_of_file'):
file_service.get_image_with_headers(_file, display_name,
'thumbnail')
file_service.get_thumbnail_of_file.assert_called_once()
示例13: test_get_file_mimetype_unknown
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_file_mimetype_unknown(self):
# === Initialization ===
display_name = 'test123'
extension = 'noidea'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
expected_mimetype = 'application/octet-stream'
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
# === Service function call ===
mimetype = file_service.get_file_mimetype(_file)
# === Assertions ===
self.assertEqual(mimetype, expected_mimetype)
示例14: test_get_thumbnail_of_file
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import hash [as 别名]
def test_get_thumbnail_of_file(self):
# === Initialization ===
display_name = 'test123'
extension = 'png'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
data = '1eb55d09d99d4d0686581a7fdb8a3346'
data_reader = StringIO(data)
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
# === Set return values ===
hashfs_mock.open.return_value = data_reader
pil_img = Mock()
pil_img.convert.return_value = pil_img
pil_image_mock.open.return_value = pil_img
# === Service function call ===
with_size = (567, 765)
file_service.get_thumbnail_of_file(
_file, thumbnail_size=with_size)
# === Assertions ===
pil_img.thumbnail.assert_called_once_with(with_size)
pil_img.save.assert_called_once()