function test_multiple_file_upload
A test function that validates multiple file upload functionality to a Flask application endpoint by sending a transcript file and multiple previous report files.
/tf/active/vicechatdev/leexi/test_flask_upload.py
10 - 75
moderate
Purpose
This function serves as an integration test for a Flask application's file upload endpoint. It creates test files in a 'test_files' directory, prepares multipart form data with a transcript and multiple previous report files, sends a POST request to the Flask app's /generate endpoint, and validates the response. The test is designed to verify that the Flask application can correctly handle multiple file uploads simultaneously.
Source Code
def test_multiple_file_upload():
"""Test multiple file upload to the Flask app"""
# Prepare test files
test_files_dir = Path("test_files")
test_files_dir.mkdir(exist_ok=True)
# Create test files if they don't exist
test_file_1 = test_files_dir / "test1.txt"
test_file_2 = test_files_dir / "test2.md"
if not test_file_1.exists():
with open(test_file_1, 'w') as f:
f.write("Test file 1 content for multiple upload test.")
if not test_file_2.exists():
with open(test_file_2, 'w') as f:
f.write("# Test File 2\n\nThis is a markdown test file for multiple upload testing.")
print("Testing multiple file upload to Flask app...")
print("=" * 50)
# Test URL
url = "http://localhost:5000/generate"
# Prepare form data
data = {
'meeting_title': 'Test Meeting for Multiple File Upload',
'model': 'gpt-4o',
'user_instructions': 'Test multiple file upload functionality'
}
# Prepare files
files = {
'transcript': ('test_transcript.txt', 'This is a test transcript for multiple file upload testing.'),
'previous_reports': [
('test1.txt', open(test_file_1, 'rb')),
('test2.md', open(test_file_2, 'rb'))
]
}
try:
print(f"Sending request with:")
print(f" - 1 transcript file")
print(f" - 2 previous report files: {test_file_1.name}, {test_file_2.name}")
# Send request
response = requests.post(url, data=data, files=files)
print(f"\nResponse status: {response.status_code}")
print(f"Response content preview: {response.text[:200]}...")
if response.status_code == 200:
print("✅ Multiple file upload test successful!")
else:
print("❌ Multiple file upload test failed!")
print(f"Error: {response.text}")
except Exception as e:
print(f"❌ Error during test: {str(e)}")
finally:
# Close file handles
for file_tuple in files['previous_reports']:
if hasattr(file_tuple[1], 'close'):
file_tuple[1].close()
Return Value
This function does not return any value (implicitly returns None). It prints test results and status messages to stdout, indicating success or failure of the multiple file upload test.
Dependencies
requestspathlib
Required Imports
import requests
from pathlib import Path
Usage Example
# Ensure Flask app is running on localhost:5000 with /generate endpoint
# Then simply call the test function:
import requests
from pathlib import Path
test_multiple_file_upload()
# Expected output:
# Testing multiple file upload to Flask app...
# ==================================================
# Sending request with:
# - 1 transcript file
# - 2 previous report files: test1.txt, test2.md
#
# Response status: 200
# Response content preview: ...
# ✅ Multiple file upload test successful!
Best Practices
- Ensure the Flask application is running on localhost:5000 before executing this test
- The function creates test files in a 'test_files' directory which persists after execution
- File handles are properly closed in the finally block to prevent resource leaks
- The test is idempotent - it checks if test files exist before creating them
- Consider cleaning up test files after test execution in production test suites
- The function assumes the Flask endpoint returns a 200 status code on success
- Error handling is implemented to catch and display exceptions during the test
- This is a standalone test function and should be integrated into a proper test framework (pytest, unittest) for production use
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_upload 74.5% similar
-
function test_web_ui 67.2% similar
-
function test_upload_modalities 67.0% similar
-
function api_upload_document_v1 65.5% similar
-
function upload_data 64.9% similar