Add unit tests for utils.hash_file function
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from app.utils import hash_file
|
||||
|
||||
class TestUtils(unittest.TestCase):
|
||||
def test_hash_file_empty(self):
|
||||
"""Test hashing an empty file"""
|
||||
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
||||
pass
|
||||
|
||||
try:
|
||||
# Known SHA-256 hash of an empty file
|
||||
expected_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
||||
self.assertEqual(hash_file(tmp_file.name), expected_hash)
|
||||
finally:
|
||||
os.unlink(tmp_file.name)
|
||||
|
||||
def test_hash_file_with_content(self):
|
||||
"""Test hashing a file with known content"""
|
||||
content = b"Hello, World!"
|
||||
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
||||
tmp_file.write(content)
|
||||
tmp_file.flush()
|
||||
|
||||
try:
|
||||
# Known SHA-256 hash of "Hello, World!"
|
||||
expected_hash = "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f"
|
||||
self.assertEqual(hash_file(tmp_file.name), expected_hash)
|
||||
finally:
|
||||
os.unlink(tmp_file.name)
|
||||
|
||||
def test_hash_file_large_content(self):
|
||||
"""Test hashing a file larger than the chunk size"""
|
||||
# Create content larger than the default chunk size (65536)
|
||||
content = b"x" * 100000
|
||||
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
||||
tmp_file.write(content)
|
||||
tmp_file.flush()
|
||||
|
||||
try:
|
||||
# Test that we can hash a large file
|
||||
hash_result = hash_file(tmp_file.name)
|
||||
self.assertEqual(len(hash_result), 64) # SHA-256 hashes are 64 characters long
|
||||
finally:
|
||||
os.unlink(tmp_file.name)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user