Spaces:
Running
on
Zero
Running
on
Zero
AndyC
commited on
Commit
·
2bb36fc
1
Parent(s):
c8f1234
added tests with checking size
Browse files
tests/{test_media.py → test_main.py}
RENAMED
|
@@ -5,7 +5,7 @@ from PIL import Image
|
|
| 5 |
from pathlib import Path
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
-
from app import get_frames, process_video, process_user_input, process_history, extract_pdf_text, update_custom_prompt
|
| 9 |
|
| 10 |
# Get the project root directory
|
| 11 |
ROOT_DIR = Path(__file__).parent.parent
|
|
@@ -698,4 +698,218 @@ def test_system_prompt_content_quality():
|
|
| 698 |
|
| 699 |
# Creative Storyteller should mention creativity/stories
|
| 700 |
creative = update_custom_prompt("Creative Storyteller")
|
| 701 |
-
assert any(word in creative.lower() for word in ["creative", "story", "narrative", "storyteller"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
+
from app import get_frames, process_video, process_user_input, process_history, extract_pdf_text, update_custom_prompt, check_file_size, MAX_VIDEO_SIZE, MAX_IMAGE_SIZE
|
| 9 |
|
| 10 |
# Get the project root directory
|
| 11 |
ROOT_DIR = Path(__file__).parent.parent
|
|
|
|
| 698 |
|
| 699 |
# Creative Storyteller should mention creativity/stories
|
| 700 |
creative = update_custom_prompt("Creative Storyteller")
|
| 701 |
+
assert any(word in creative.lower() for word in ["creative", "story", "narrative", "storyteller"])
|
| 702 |
+
|
| 703 |
+
|
| 704 |
+
def test_check_file_size_nonexistent_file():
|
| 705 |
+
"""Test that check_file_size raises ValueError for non-existent files."""
|
| 706 |
+
with pytest.raises(ValueError, match="File not found"):
|
| 707 |
+
check_file_size("nonexistent_file.txt")
|
| 708 |
+
|
| 709 |
+
|
| 710 |
+
def test_check_file_size_valid_image():
|
| 711 |
+
"""Test that check_file_size returns True for valid image files."""
|
| 712 |
+
# Create a small temporary image file
|
| 713 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
| 714 |
+
# Write minimal JPEG header to make it a valid file
|
| 715 |
+
temp_file.write(b"small image content")
|
| 716 |
+
temp_path = temp_file.name
|
| 717 |
+
|
| 718 |
+
try:
|
| 719 |
+
# File should be well under the image size limit
|
| 720 |
+
result = check_file_size(temp_path)
|
| 721 |
+
assert result is True
|
| 722 |
+
|
| 723 |
+
finally:
|
| 724 |
+
if os.path.exists(temp_path):
|
| 725 |
+
os.unlink(temp_path)
|
| 726 |
+
|
| 727 |
+
|
| 728 |
+
def test_check_file_size_valid_video():
|
| 729 |
+
"""Test that check_file_size returns True for valid video files."""
|
| 730 |
+
# Create a small temporary video file
|
| 731 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
|
| 732 |
+
# Write some minimal content
|
| 733 |
+
temp_file.write(b"small video content")
|
| 734 |
+
temp_path = temp_file.name
|
| 735 |
+
|
| 736 |
+
try:
|
| 737 |
+
# File should be well under the video size limit
|
| 738 |
+
result = check_file_size(temp_path)
|
| 739 |
+
assert result is True
|
| 740 |
+
|
| 741 |
+
finally:
|
| 742 |
+
if os.path.exists(temp_path):
|
| 743 |
+
os.unlink(temp_path)
|
| 744 |
+
|
| 745 |
+
|
| 746 |
+
def test_check_file_size_large_image():
|
| 747 |
+
"""Test that check_file_size raises ValueError for oversized image files."""
|
| 748 |
+
# Create a temporary file that exceeds the image size limit
|
| 749 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
| 750 |
+
# Write content larger than MAX_IMAGE_SIZE (10MB)
|
| 751 |
+
large_content = b"x" * (MAX_IMAGE_SIZE + 1024) # 10MB + 1KB
|
| 752 |
+
temp_file.write(large_content)
|
| 753 |
+
temp_path = temp_file.name
|
| 754 |
+
|
| 755 |
+
try:
|
| 756 |
+
with pytest.raises(ValueError, match="Image file too large"):
|
| 757 |
+
check_file_size(temp_path)
|
| 758 |
+
|
| 759 |
+
finally:
|
| 760 |
+
if os.path.exists(temp_path):
|
| 761 |
+
os.unlink(temp_path)
|
| 762 |
+
|
| 763 |
+
|
| 764 |
+
def test_check_file_size_large_video():
|
| 765 |
+
"""Test that check_file_size raises ValueError for oversized video files."""
|
| 766 |
+
# Create a temporary file that exceeds the video size limit
|
| 767 |
+
with tempfile.NamedTemporaryFile(suffix=".mov", delete=False) as temp_file:
|
| 768 |
+
# Write content larger than MAX_VIDEO_SIZE (100MB)
|
| 769 |
+
# Write in chunks to avoid memory issues
|
| 770 |
+
chunk_size = 1024 * 1024 # 1MB chunks
|
| 771 |
+
chunks_needed = (MAX_VIDEO_SIZE // chunk_size) + 2 # Exceed limit by 2MB
|
| 772 |
+
|
| 773 |
+
for _ in range(chunks_needed):
|
| 774 |
+
temp_file.write(b"x" * chunk_size)
|
| 775 |
+
temp_path = temp_file.name
|
| 776 |
+
|
| 777 |
+
try:
|
| 778 |
+
with pytest.raises(ValueError, match="Video file too large"):
|
| 779 |
+
check_file_size(temp_path)
|
| 780 |
+
|
| 781 |
+
finally:
|
| 782 |
+
if os.path.exists(temp_path):
|
| 783 |
+
os.unlink(temp_path)
|
| 784 |
+
|
| 785 |
+
|
| 786 |
+
def test_check_file_size_edge_cases():
|
| 787 |
+
"""Test check_file_size with files at the exact size limits."""
|
| 788 |
+
# Test image file at exact limit
|
| 789 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
| 790 |
+
# Write exactly MAX_IMAGE_SIZE bytes
|
| 791 |
+
temp_file.write(b"x" * MAX_IMAGE_SIZE)
|
| 792 |
+
image_path = temp_file.name
|
| 793 |
+
|
| 794 |
+
try:
|
| 795 |
+
# Should pass at exact limit
|
| 796 |
+
result = check_file_size(image_path)
|
| 797 |
+
assert result is True
|
| 798 |
+
|
| 799 |
+
finally:
|
| 800 |
+
if os.path.exists(image_path):
|
| 801 |
+
os.unlink(image_path)
|
| 802 |
+
|
| 803 |
+
# Test video file at exact limit
|
| 804 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
|
| 805 |
+
# Write exactly MAX_VIDEO_SIZE bytes in chunks to avoid memory issues
|
| 806 |
+
chunk_size = 1024 * 1024 # 1MB chunks
|
| 807 |
+
chunks_needed = MAX_VIDEO_SIZE // chunk_size
|
| 808 |
+
|
| 809 |
+
for _ in range(chunks_needed):
|
| 810 |
+
temp_file.write(b"x" * chunk_size)
|
| 811 |
+
video_path = temp_file.name
|
| 812 |
+
|
| 813 |
+
try:
|
| 814 |
+
# Should pass at exact limit
|
| 815 |
+
result = check_file_size(video_path)
|
| 816 |
+
assert result is True
|
| 817 |
+
|
| 818 |
+
finally:
|
| 819 |
+
if os.path.exists(video_path):
|
| 820 |
+
os.unlink(video_path)
|
| 821 |
+
|
| 822 |
+
|
| 823 |
+
def test_check_file_size_different_extensions():
|
| 824 |
+
"""Test that check_file_size correctly categorizes different file extensions."""
|
| 825 |
+
# Test various video extensions
|
| 826 |
+
video_extensions = [".mp4", ".mov", ".MP4", ".MOV"] # Test case sensitivity
|
| 827 |
+
for ext in video_extensions:
|
| 828 |
+
with tempfile.NamedTemporaryFile(suffix=ext, delete=False) as temp_file:
|
| 829 |
+
temp_file.write(b"small content")
|
| 830 |
+
temp_path = temp_file.name
|
| 831 |
+
|
| 832 |
+
try:
|
| 833 |
+
# Should be treated as video file (checked against video limit)
|
| 834 |
+
result = check_file_size(temp_path)
|
| 835 |
+
assert result is True
|
| 836 |
+
|
| 837 |
+
finally:
|
| 838 |
+
if os.path.exists(temp_path):
|
| 839 |
+
os.unlink(temp_path)
|
| 840 |
+
|
| 841 |
+
# Test various image extensions
|
| 842 |
+
image_extensions = [".jpg", ".png", ".jpeg", ".gif", ".bmp", ".JPG", ".PNG"]
|
| 843 |
+
for ext in image_extensions:
|
| 844 |
+
with tempfile.NamedTemporaryFile(suffix=ext, delete=False) as temp_file:
|
| 845 |
+
temp_file.write(b"small content")
|
| 846 |
+
temp_path = temp_file.name
|
| 847 |
+
|
| 848 |
+
try:
|
| 849 |
+
# Should be treated as image file (checked against image limit)
|
| 850 |
+
result = check_file_size(temp_path)
|
| 851 |
+
assert result is True
|
| 852 |
+
|
| 853 |
+
finally:
|
| 854 |
+
if os.path.exists(temp_path):
|
| 855 |
+
os.unlink(temp_path)
|
| 856 |
+
|
| 857 |
+
|
| 858 |
+
def test_check_file_size_empty_file():
|
| 859 |
+
"""Test that check_file_size handles empty files correctly."""
|
| 860 |
+
# Create empty image file
|
| 861 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
| 862 |
+
temp_path = temp_file.name # File is created but empty
|
| 863 |
+
|
| 864 |
+
try:
|
| 865 |
+
# Empty file should pass size check
|
| 866 |
+
result = check_file_size(temp_path)
|
| 867 |
+
assert result is True
|
| 868 |
+
|
| 869 |
+
finally:
|
| 870 |
+
if os.path.exists(temp_path):
|
| 871 |
+
os.unlink(temp_path)
|
| 872 |
+
|
| 873 |
+
|
| 874 |
+
def test_check_file_size_error_messages():
|
| 875 |
+
"""Test that check_file_size provides informative error messages."""
|
| 876 |
+
# Test oversized image error message
|
| 877 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
| 878 |
+
oversized_content = b"x" * (MAX_IMAGE_SIZE + 1024)
|
| 879 |
+
temp_file.write(oversized_content)
|
| 880 |
+
temp_path = temp_file.name
|
| 881 |
+
|
| 882 |
+
try:
|
| 883 |
+
with pytest.raises(ValueError) as exc_info:
|
| 884 |
+
check_file_size(temp_path)
|
| 885 |
+
|
| 886 |
+
error_message = str(exc_info.value)
|
| 887 |
+
assert "Image file too large" in error_message
|
| 888 |
+
assert "Maximum allowed:" in error_message
|
| 889 |
+
assert "10MB" in error_message # Should show the limit
|
| 890 |
+
|
| 891 |
+
finally:
|
| 892 |
+
if os.path.exists(temp_path):
|
| 893 |
+
os.unlink(temp_path)
|
| 894 |
+
|
| 895 |
+
|
| 896 |
+
def test_check_file_size_with_actual_test_video():
|
| 897 |
+
"""Test check_file_size with the actual test video file."""
|
| 898 |
+
video_path = os.path.join(ROOT_DIR, "assets", "test_video.mp4")
|
| 899 |
+
|
| 900 |
+
if os.path.exists(video_path):
|
| 901 |
+
# Should pass since test video should be under the limit
|
| 902 |
+
result = check_file_size(video_path)
|
| 903 |
+
assert result is True
|
| 904 |
+
else:
|
| 905 |
+
pytest.skip("Test video file not found")
|
| 906 |
+
|
| 907 |
+
|
| 908 |
+
def test_check_file_size_constants():
|
| 909 |
+
"""Test that the size constants are set to expected values."""
|
| 910 |
+
# Verify the constants are set correctly
|
| 911 |
+
assert MAX_VIDEO_SIZE == 100 * 1024 * 1024 # 100 MB
|
| 912 |
+
assert MAX_IMAGE_SIZE == 10 * 1024 * 1024 # 10 MB
|
| 913 |
+
|
| 914 |
+
# Ensure video limit is larger than image limit
|
| 915 |
+
assert MAX_VIDEO_SIZE > MAX_IMAGE_SIZE
|