From b5c74dca4ee024aad1ea180bd5b593f1a61bb71d Mon Sep 17 00:00:00 2001 From: openhands Date: Wed, 26 Mar 2025 16:27:36 +0000 Subject: [PATCH] Add unit tests for utils.hash_file function --- tests/__init__.py | 0 tests/test_utils.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_utils.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..f6471e68 --- /dev/null +++ b/tests/test_utils.py @@ -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() \ No newline at end of file