style: Apply Black formatting to modified files
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Tests for file detail view improvements including reprocessing and preview endpoints.
|
||||
"""
|
||||
|
||||
import os
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
@@ -11,7 +12,7 @@ from app.models import FileRecord, ProcessingLog
|
||||
@pytest.mark.integration
|
||||
class TestFileReprocessing:
|
||||
"""Tests for single file reprocessing endpoint."""
|
||||
|
||||
|
||||
@patch("app.api.files.process_document")
|
||||
def test_reprocess_existing_file(self, mock_process_document, client: TestClient, db_session, sample_pdf_path):
|
||||
"""Test reprocessing an existing file."""
|
||||
@@ -19,30 +20,30 @@ class TestFileReprocessing:
|
||||
mock_task = MagicMock()
|
||||
mock_task.id = "test-task-123"
|
||||
mock_process_document.delay.return_value = mock_task
|
||||
|
||||
|
||||
# Create a file record
|
||||
file_record = FileRecord(
|
||||
filehash="abc123",
|
||||
original_filename="test.pdf",
|
||||
local_filename=sample_pdf_path,
|
||||
file_size=1024,
|
||||
mime_type="application/pdf"
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
db_session.add(file_record)
|
||||
db_session.commit()
|
||||
db_session.refresh(file_record)
|
||||
|
||||
|
||||
# Add a failed processing log
|
||||
log = ProcessingLog(
|
||||
file_id=file_record.id,
|
||||
task_id="test-task-123",
|
||||
step_name="extract_metadata_with_gpt",
|
||||
status="failure",
|
||||
message="API error"
|
||||
message="API error",
|
||||
)
|
||||
db_session.add(log)
|
||||
db_session.commit()
|
||||
|
||||
|
||||
# Test reprocessing
|
||||
response = client.post(f"/api/files/{file_record.id}/reprocess")
|
||||
assert response.status_code == 200
|
||||
@@ -51,13 +52,13 @@ class TestFileReprocessing:
|
||||
assert "task_id" in data
|
||||
assert data["file_id"] == file_record.id
|
||||
assert data["filename"] == "test.pdf"
|
||||
|
||||
|
||||
def test_reprocess_nonexistent_file(self, client: TestClient):
|
||||
"""Test reprocessing a file that doesn't exist."""
|
||||
response = client.post("/api/files/99999/reprocess")
|
||||
assert response.status_code == 404
|
||||
assert "not found" in response.json()["detail"].lower()
|
||||
|
||||
|
||||
def test_reprocess_file_missing_on_disk(self, client: TestClient, db_session):
|
||||
"""Test reprocessing when local file is missing."""
|
||||
# Create a file record with non-existent local path
|
||||
@@ -66,12 +67,12 @@ class TestFileReprocessing:
|
||||
original_filename="missing.pdf",
|
||||
local_filename="/nonexistent/path/missing.pdf",
|
||||
file_size=1024,
|
||||
mime_type="application/pdf"
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
db_session.add(file_record)
|
||||
db_session.commit()
|
||||
db_session.refresh(file_record)
|
||||
|
||||
|
||||
# Test reprocessing
|
||||
response = client.post(f"/api/files/{file_record.id}/reprocess")
|
||||
assert response.status_code == 400
|
||||
@@ -81,13 +82,13 @@ class TestFileReprocessing:
|
||||
@pytest.mark.integration
|
||||
class TestSubtaskRetry:
|
||||
"""Tests for per-subtask retry endpoint."""
|
||||
|
||||
|
||||
def test_retry_subtask_invalid_file(self, client: TestClient):
|
||||
"""Test retrying a subtask for nonexistent file."""
|
||||
response = client.post("/api/files/99999/retry-subtask?subtask_name=upload_to_dropbox")
|
||||
assert response.status_code == 404
|
||||
assert "not found" in response.json()["detail"].lower()
|
||||
|
||||
|
||||
def test_retry_subtask_invalid_task_name(self, client: TestClient, db_session, sample_pdf_path):
|
||||
"""Test retrying with invalid subtask name."""
|
||||
# Create a file record
|
||||
@@ -96,17 +97,17 @@ class TestSubtaskRetry:
|
||||
original_filename="retry.pdf",
|
||||
local_filename=sample_pdf_path,
|
||||
file_size=1024,
|
||||
mime_type="application/pdf"
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
db_session.add(file_record)
|
||||
db_session.commit()
|
||||
db_session.refresh(file_record)
|
||||
|
||||
|
||||
# Test with invalid subtask name
|
||||
response = client.post(f"/api/files/{file_record.id}/retry-subtask?subtask_name=invalid_task")
|
||||
assert response.status_code == 400
|
||||
assert "invalid subtask name" in response.json()["detail"].lower()
|
||||
|
||||
|
||||
def test_retry_subtask_missing_processed_file(self, client: TestClient, db_session, sample_pdf_path):
|
||||
"""Test retrying when processed file is missing."""
|
||||
# Create a file record
|
||||
@@ -115,12 +116,12 @@ class TestSubtaskRetry:
|
||||
original_filename="retry2.pdf",
|
||||
local_filename=sample_pdf_path,
|
||||
file_size=1024,
|
||||
mime_type="application/pdf"
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
db_session.add(file_record)
|
||||
db_session.commit()
|
||||
db_session.refresh(file_record)
|
||||
|
||||
|
||||
# Test retry (processed file won't exist)
|
||||
response = client.post(f"/api/files/{file_record.id}/retry-subtask?subtask_name=upload_to_dropbox")
|
||||
assert response.status_code == 400
|
||||
@@ -130,7 +131,7 @@ class TestSubtaskRetry:
|
||||
@pytest.mark.integration
|
||||
class TestFilePreview:
|
||||
"""Tests for file preview endpoint."""
|
||||
|
||||
|
||||
def test_preview_original_file(self, client: TestClient, db_session, sample_pdf_path):
|
||||
"""Test getting original file preview."""
|
||||
# Create a file record
|
||||
@@ -139,17 +140,17 @@ class TestFilePreview:
|
||||
original_filename="preview.pdf",
|
||||
local_filename=sample_pdf_path,
|
||||
file_size=1024,
|
||||
mime_type="application/pdf"
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
db_session.add(file_record)
|
||||
db_session.commit()
|
||||
db_session.refresh(file_record)
|
||||
|
||||
|
||||
# Test preview
|
||||
response = client.get(f"/api/files/{file_record.id}/preview?version=original")
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"].startswith("application/pdf")
|
||||
|
||||
|
||||
def test_preview_processed_file_not_found(self, client: TestClient, db_session, sample_pdf_path):
|
||||
"""Test getting processed file preview when it doesn't exist."""
|
||||
# Create a file record
|
||||
@@ -158,23 +159,23 @@ class TestFilePreview:
|
||||
original_filename="processed.pdf",
|
||||
local_filename=sample_pdf_path,
|
||||
file_size=1024,
|
||||
mime_type="application/pdf"
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
db_session.add(file_record)
|
||||
db_session.commit()
|
||||
db_session.refresh(file_record)
|
||||
|
||||
|
||||
# Test preview (processed version should not exist)
|
||||
response = client.get(f"/api/files/{file_record.id}/preview?version=processed")
|
||||
assert response.status_code == 404
|
||||
assert "not found" in response.json()["detail"].lower()
|
||||
|
||||
|
||||
def test_preview_nonexistent_file(self, client: TestClient):
|
||||
"""Test preview for a file that doesn't exist."""
|
||||
response = client.get("/api/files/99999/preview?version=original")
|
||||
assert response.status_code == 404
|
||||
assert "not found" in response.json()["detail"].lower()
|
||||
|
||||
|
||||
def test_preview_invalid_version(self, client: TestClient, db_session, sample_pdf_path):
|
||||
"""Test preview with invalid version parameter."""
|
||||
# Create a file record
|
||||
@@ -183,12 +184,12 @@ class TestFilePreview:
|
||||
original_filename="test.pdf",
|
||||
local_filename=sample_pdf_path,
|
||||
file_size=1024,
|
||||
mime_type="application/pdf"
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
db_session.add(file_record)
|
||||
db_session.commit()
|
||||
db_session.refresh(file_record)
|
||||
|
||||
|
||||
# Test with invalid version
|
||||
response = client.get(f"/api/files/{file_record.id}/preview?version=invalid")
|
||||
assert response.status_code == 400
|
||||
@@ -198,7 +199,7 @@ class TestFilePreview:
|
||||
@pytest.mark.integration
|
||||
class TestFileDetailView:
|
||||
"""Tests for enhanced file detail view."""
|
||||
|
||||
|
||||
def test_file_detail_view_with_logs(self, client: TestClient, db_session, sample_pdf_path):
|
||||
"""Test file detail view returns enhanced data."""
|
||||
# Create a file record
|
||||
@@ -207,12 +208,12 @@ class TestFileDetailView:
|
||||
original_filename="detail.pdf",
|
||||
local_filename=sample_pdf_path,
|
||||
file_size=1024,
|
||||
mime_type="application/pdf"
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
db_session.add(file_record)
|
||||
db_session.commit()
|
||||
db_session.refresh(file_record)
|
||||
|
||||
|
||||
# Add processing logs
|
||||
logs = [
|
||||
ProcessingLog(
|
||||
@@ -220,27 +221,27 @@ class TestFileDetailView:
|
||||
task_id="task-1",
|
||||
step_name="hash_file",
|
||||
status="success",
|
||||
message="File hashed successfully"
|
||||
message="File hashed successfully",
|
||||
),
|
||||
ProcessingLog(
|
||||
file_id=file_record.id,
|
||||
task_id="task-1",
|
||||
step_name="create_file_record",
|
||||
status="success",
|
||||
message="File record created"
|
||||
message="File record created",
|
||||
),
|
||||
ProcessingLog(
|
||||
file_id=file_record.id,
|
||||
task_id="task-1",
|
||||
step_name="extract_metadata_with_gpt",
|
||||
status="failure",
|
||||
message="API rate limit exceeded"
|
||||
)
|
||||
message="API rate limit exceeded",
|
||||
),
|
||||
]
|
||||
for log in logs:
|
||||
db_session.add(log)
|
||||
db_session.commit()
|
||||
|
||||
|
||||
# Test detail view
|
||||
response = client.get(f"/files/{file_record.id}/detail")
|
||||
assert response.status_code == 200
|
||||
@@ -248,7 +249,7 @@ class TestFileDetailView:
|
||||
assert b"File Information" in response.content
|
||||
assert b"detail.pdf" in response.content
|
||||
assert b"Processing History" in response.content
|
||||
|
||||
|
||||
def test_file_detail_view_with_upload_branches(self, client: TestClient, db_session, sample_pdf_path):
|
||||
"""Test file detail view with upload subtask branches."""
|
||||
# Create a file record
|
||||
@@ -257,12 +258,12 @@ class TestFileDetailView:
|
||||
original_filename="branches.pdf",
|
||||
local_filename=sample_pdf_path,
|
||||
file_size=1024,
|
||||
mime_type="application/pdf"
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
db_session.add(file_record)
|
||||
db_session.commit()
|
||||
db_session.refresh(file_record)
|
||||
|
||||
|
||||
# Add processing logs including upload branches
|
||||
logs = [
|
||||
ProcessingLog(
|
||||
@@ -270,34 +271,34 @@ class TestFileDetailView:
|
||||
task_id="task-1",
|
||||
step_name="send_to_all_destinations",
|
||||
status="success",
|
||||
message="Queued uploads"
|
||||
message="Queued uploads",
|
||||
),
|
||||
ProcessingLog(
|
||||
file_id=file_record.id,
|
||||
task_id="task-2",
|
||||
step_name="upload_to_dropbox",
|
||||
status="success",
|
||||
message="Uploaded to Dropbox"
|
||||
message="Uploaded to Dropbox",
|
||||
),
|
||||
ProcessingLog(
|
||||
file_id=file_record.id,
|
||||
task_id="task-3",
|
||||
step_name="upload_to_s3",
|
||||
status="failure",
|
||||
message="S3 connection error"
|
||||
message="S3 connection error",
|
||||
),
|
||||
ProcessingLog(
|
||||
file_id=file_record.id,
|
||||
task_id="task-4",
|
||||
step_name="upload_to_nextcloud",
|
||||
status="success",
|
||||
message="Uploaded to Nextcloud"
|
||||
)
|
||||
message="Uploaded to Nextcloud",
|
||||
),
|
||||
]
|
||||
for log in logs:
|
||||
db_session.add(log)
|
||||
db_session.commit()
|
||||
|
||||
|
||||
# Test detail view
|
||||
response = client.get(f"/files/{file_record.id}/detail")
|
||||
assert response.status_code == 200
|
||||
@@ -306,7 +307,7 @@ class TestFileDetailView:
|
||||
assert b"Processing Status Summary" in response.content
|
||||
# Should have upload branches
|
||||
assert b"Dropbox" in response.content or b"dropbox" in response.content
|
||||
|
||||
|
||||
def test_file_detail_view_nonexistent(self, client: TestClient):
|
||||
"""Test file detail view for nonexistent file."""
|
||||
response = client.get("/files/99999/detail")
|
||||
@@ -317,11 +318,11 @@ class TestFileDetailView:
|
||||
@pytest.mark.unit
|
||||
class TestProcessingFlowComputation:
|
||||
"""Tests for the _compute_processing_flow function."""
|
||||
|
||||
|
||||
def test_flow_with_upload_branches(self, db_session):
|
||||
"""Test that upload tasks are properly grouped as branches."""
|
||||
from app.views.files import _compute_processing_flow
|
||||
|
||||
|
||||
# Create mock logs
|
||||
class MockLog:
|
||||
def __init__(self, step_name, status, message, timestamp, task_id):
|
||||
@@ -330,27 +331,27 @@ class TestProcessingFlowComputation:
|
||||
self.message = message
|
||||
self.timestamp = timestamp
|
||||
self.task_id = task_id
|
||||
|
||||
|
||||
logs = [
|
||||
MockLog("hash_file", "success", "Hashed", None, "task-1"),
|
||||
MockLog("send_to_all_destinations", "success", "Queued", None, "task-2"),
|
||||
MockLog("upload_to_dropbox", "success", "Uploaded", None, "task-3"),
|
||||
MockLog("upload_to_s3", "failure", "Failed", None, "task-4"),
|
||||
]
|
||||
|
||||
|
||||
flow = _compute_processing_flow(logs)
|
||||
|
||||
|
||||
# Find the upload stage
|
||||
upload_stage = None
|
||||
for stage in flow:
|
||||
if stage.get("is_branch_parent"):
|
||||
upload_stage = stage
|
||||
break
|
||||
|
||||
|
||||
assert upload_stage is not None
|
||||
assert "branches" in upload_stage
|
||||
assert len(upload_stage["branches"]) == 2
|
||||
|
||||
|
||||
# Check branch details
|
||||
branches = {b["key"]: b for b in upload_stage["branches"]}
|
||||
assert "upload_to_dropbox" in branches
|
||||
@@ -363,17 +364,17 @@ class TestProcessingFlowComputation:
|
||||
@pytest.mark.unit
|
||||
class TestStepSummary:
|
||||
"""Tests for the _compute_step_summary function."""
|
||||
|
||||
|
||||
def test_summary_with_mixed_statuses(self):
|
||||
"""Test step summary with various statuses."""
|
||||
from app.views.files import _compute_step_summary
|
||||
|
||||
|
||||
# Create mock logs
|
||||
class MockLog:
|
||||
def __init__(self, step_name, status):
|
||||
self.step_name = step_name
|
||||
self.status = status
|
||||
|
||||
|
||||
logs = [
|
||||
MockLog("hash_file", "success"),
|
||||
MockLog("create_file_record", "success"),
|
||||
@@ -382,9 +383,9 @@ class TestStepSummary:
|
||||
MockLog("upload_to_s3", "failure"),
|
||||
MockLog("upload_to_nextcloud", "in_progress"),
|
||||
]
|
||||
|
||||
|
||||
summary = _compute_step_summary(logs)
|
||||
|
||||
|
||||
assert "main" in summary
|
||||
assert "uploads" in summary
|
||||
assert summary["total_main_steps"] == 3
|
||||
|
||||
Reference in New Issue
Block a user