reformatting
formatting_check / formatting_check (push) Failing after 3s
Python Test / test (.ps1, windows-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, macos-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, ubuntu-latest, 3.9) (push) Has been cancelled
formatting_check / formatting_check (push) Failing after 3s
Python Test / test (.ps1, windows-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, macos-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, ubuntu-latest, 3.9) (push) Has been cancelled
This commit is contained in:
@@ -3,14 +3,15 @@
|
||||
Test script to verify hash persistence functionality.
|
||||
"""
|
||||
import json
|
||||
import tempfile
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock
|
||||
|
||||
# Import the necessary modules
|
||||
import sys
|
||||
sys.path.insert(0, '/Users/Daniel/Documents/GitHub/bulk-downloader-for-reddit')
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock
|
||||
|
||||
sys.path.insert(0, "/Users/Daniel/Documents/GitHub/bulk-downloader-for-reddit")
|
||||
|
||||
from bdfr.configuration import Configuration
|
||||
from bdfr.downloader import RedditDownloader
|
||||
@@ -59,7 +60,7 @@ def test_hash_persistence():
|
||||
# Test 2: Save empty hash list
|
||||
print("Test 2: Saving empty hash list")
|
||||
downloader._save_hash_list()
|
||||
hash_file = temp_path / '.bdfr_hashes.json'
|
||||
hash_file = temp_path / ".bdfr_hashes.json"
|
||||
assert hash_file.exists(), "Hash file should be created even when empty"
|
||||
print("PASS Passed")
|
||||
|
||||
@@ -71,18 +72,18 @@ def test_hash_persistence():
|
||||
|
||||
# Test 4: Add some test data and save
|
||||
print("Test 4: Adding test data and saving")
|
||||
test_file = temp_path / 'test.txt'
|
||||
test_file = temp_path / "test.txt"
|
||||
test_file.write_text("test content")
|
||||
downloader.master_hash_list['test_hash_123'] = test_file
|
||||
downloader.master_hash_list["test_hash_123"] = test_file
|
||||
|
||||
downloader._save_hash_list()
|
||||
|
||||
# Verify the saved JSON structure
|
||||
with open(hash_file, 'r') as f:
|
||||
with open(hash_file, "r") as f:
|
||||
saved_data = json.load(f)
|
||||
|
||||
assert 'test_hash_123' in saved_data, "Test hash should be in saved data"
|
||||
assert saved_data['test_hash_123'] == 'test.txt', f"Expected 'test.txt', got {saved_data['test_hash_123']}"
|
||||
assert "test_hash_123" in saved_data, "Test hash should be in saved data"
|
||||
assert saved_data["test_hash_123"] == "test.txt", f"Expected 'test.txt', got {saved_data['test_hash_123']}"
|
||||
print("PASS Passed")
|
||||
|
||||
# Test 5: Load hash list and verify data is restored
|
||||
@@ -100,13 +101,15 @@ def test_hash_persistence():
|
||||
|
||||
loaded_hash_list = new_downloader._load_hash_list()
|
||||
assert len(loaded_hash_list) == 1, f"Expected 1 hash, got {len(loaded_hash_list)}"
|
||||
assert 'test_hash_123' in loaded_hash_list, "Test hash should be loaded"
|
||||
assert loaded_hash_list['test_hash_123'] == test_file, f"File path should match: {loaded_hash_list['test_hash_123']} != {test_file}"
|
||||
assert "test_hash_123" in loaded_hash_list, "Test hash should be loaded"
|
||||
assert (
|
||||
loaded_hash_list["test_hash_123"] == test_file
|
||||
), f"File path should match: {loaded_hash_list['test_hash_123']} != {test_file}"
|
||||
print("PASS Passed")
|
||||
|
||||
# Test 6: Test corrupted hash file handling
|
||||
print("Test 6: Testing corrupted hash file handling")
|
||||
with open(hash_file, 'w') as f:
|
||||
with open(hash_file, "w") as f:
|
||||
f.write("invalid json content")
|
||||
|
||||
corrupted_downloader = RedditDownloader.__new__(RedditDownloader)
|
||||
@@ -122,7 +125,9 @@ def test_hash_persistence():
|
||||
|
||||
# Should handle corrupted file gracefully and return empty dict
|
||||
corrupted_hash_list = corrupted_downloader._load_hash_list()
|
||||
assert len(corrupted_hash_list) == 0, f"Expected empty hash list for corrupted file, got {len(corrupted_hash_list)}"
|
||||
assert (
|
||||
len(corrupted_hash_list) == 0
|
||||
), f"Expected empty hash list for corrupted file, got {len(corrupted_hash_list)}"
|
||||
print("PASS Passed")
|
||||
|
||||
print("\nAll tests passed! Hash persistence functionality is working correctly.")
|
||||
@@ -174,7 +179,7 @@ def test_simple_check_functionality():
|
||||
|
||||
# Test 2: Add test data and save with simple_check format
|
||||
print("Test 2: Adding test data and saving with simple_check format")
|
||||
test_file = temp_path / 'test.txt'
|
||||
test_file = temp_path / "test.txt"
|
||||
test_file.write_text("test content")
|
||||
test_url = "https://example.com/test.txt"
|
||||
test_hash = "test_hash_123"
|
||||
@@ -185,16 +190,16 @@ def test_simple_check_functionality():
|
||||
downloader._save_hash_list()
|
||||
|
||||
# Verify the saved JSON structure has enhanced format
|
||||
with open(temp_path / '.bdfr_hashes.json', 'r') as f:
|
||||
with open(temp_path / ".bdfr_hashes.json", "r") as f:
|
||||
saved_data = json.load(f)
|
||||
|
||||
assert 'files' in saved_data, "Enhanced format should have 'files' section"
|
||||
assert 'urls' in saved_data, "Enhanced format should have 'urls' section"
|
||||
assert 'metadata' in saved_data, "Enhanced format should have 'metadata' section"
|
||||
assert test_hash in saved_data['files'], "Test hash should be in files section"
|
||||
assert test_url in saved_data['urls'], "Test URL should be in urls section"
|
||||
assert saved_data['metadata']['version'] == '2.0', "Version should be 2.0"
|
||||
assert saved_data['metadata']['created_with'] == 'simple_check', "Should be created with simple_check"
|
||||
assert "files" in saved_data, "Enhanced format should have 'files' section"
|
||||
assert "urls" in saved_data, "Enhanced format should have 'urls' section"
|
||||
assert "metadata" in saved_data, "Enhanced format should have 'metadata' section"
|
||||
assert test_hash in saved_data["files"], "Test hash should be in files section"
|
||||
assert test_url in saved_data["urls"], "Test URL should be in urls section"
|
||||
assert saved_data["metadata"]["version"] == "2.0", "Version should be 2.0"
|
||||
assert saved_data["metadata"]["created_with"] == "simple_check", "Should be created with simple_check"
|
||||
print("PASS")
|
||||
|
||||
# Test 3: Load hash list and verify URL mapping is restored
|
||||
@@ -228,7 +233,7 @@ def test_simple_check_functionality():
|
||||
mock_resource.hash.hexdigest.return_value = test_hash
|
||||
|
||||
# Create a mock destination that exists
|
||||
mock_destination = temp_path / 'existing_file.txt'
|
||||
mock_destination = temp_path / "existing_file.txt"
|
||||
mock_destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
mock_destination.write_text("existing content")
|
||||
|
||||
@@ -260,17 +265,14 @@ def test_backward_compatibility():
|
||||
temp_path = Path(temp_dir)
|
||||
|
||||
# Create old-format hash file manually
|
||||
(temp_path / 'relative' / 'path').mkdir(parents=True, exist_ok=True)
|
||||
(temp_path / 'relative' / 'path' / 'file1.txt').write_text("content1")
|
||||
(temp_path / 'relative' / 'path' / 'file2.txt').write_text("content2")
|
||||
(temp_path / "relative" / "path").mkdir(parents=True, exist_ok=True)
|
||||
(temp_path / "relative" / "path" / "file1.txt").write_text("content1")
|
||||
(temp_path / "relative" / "path" / "file2.txt").write_text("content2")
|
||||
|
||||
old_hash_data = {
|
||||
"hash1": "relative/path/file1.txt",
|
||||
"hash2": "relative/path/file2.txt"
|
||||
}
|
||||
old_hash_data = {"hash1": "relative/path/file1.txt", "hash2": "relative/path/file2.txt"}
|
||||
|
||||
hash_file = temp_path / '.bdfr_hashes.json'
|
||||
with open(hash_file, 'w') as f:
|
||||
hash_file = temp_path / ".bdfr_hashes.json"
|
||||
with open(hash_file, "w") as f:
|
||||
json.dump(old_hash_data, f)
|
||||
|
||||
# Create downloader and load old format
|
||||
@@ -294,21 +296,21 @@ def test_backward_compatibility():
|
||||
print("PASS - Old format loaded correctly")
|
||||
|
||||
# Test saving in new format
|
||||
(temp_path / 'another').mkdir(parents=True, exist_ok=True)
|
||||
test_file = temp_path / 'another' / 'new_file.txt'
|
||||
(temp_path / "another").mkdir(parents=True, exist_ok=True)
|
||||
test_file = temp_path / "another" / "new_file.txt"
|
||||
test_file.write_text("new content")
|
||||
downloader.master_hash_list["new_hash"] = test_file
|
||||
|
||||
downloader._save_hash_list()
|
||||
|
||||
# Verify new format was created
|
||||
with open(hash_file, 'r') as f:
|
||||
with open(hash_file, "r") as f:
|
||||
new_data = json.load(f)
|
||||
|
||||
assert 'files' in new_data, "New format should have 'files' section"
|
||||
assert 'urls' in new_data, "New format should have 'urls' section"
|
||||
assert 'metadata' in new_data, "New format should have 'metadata' section"
|
||||
assert new_data['metadata']['version'] == '2.0', "Should be version 2.0"
|
||||
assert "files" in new_data, "New format should have 'files' section"
|
||||
assert "urls" in new_data, "New format should have 'urls' section"
|
||||
assert "metadata" in new_data, "New format should have 'metadata' section"
|
||||
assert new_data["metadata"]["version"] == "2.0", "Should be version 2.0"
|
||||
|
||||
print("PASS - Old format upgraded to new format correctly")
|
||||
|
||||
@@ -318,4 +320,4 @@ def test_backward_compatibility():
|
||||
if __name__ == "__main__":
|
||||
test_hash_persistence()
|
||||
test_simple_check_functionality()
|
||||
test_backward_compatibility()
|
||||
test_backward_compatibility()
|
||||
|
||||
Reference in New Issue
Block a user