|
| 1 | +import pytest |
| 2 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 3 | +from uuid import uuid4 |
| 4 | +from datetime import datetime |
| 5 | +from fastapi import UploadFile, HTTPException |
| 6 | + |
| 7 | +from common.services.batch_service import BatchService |
| 8 | +from common.models.api import ( |
| 9 | + FileRecord, |
| 10 | + BatchRecord, |
| 11 | + FileResult, |
| 12 | + LogType, |
| 13 | + AgentType, |
| 14 | + ProcessStatus, |
| 15 | +) |
| 16 | + |
| 17 | +# ---------- Helpers ---------- |
| 18 | +def make_file_record(**overrides): |
| 19 | + return FileRecord( |
| 20 | + file_id=overrides.get("file_id", "file1"), |
| 21 | + batch_id=overrides.get("batch_id", "batch123"), |
| 22 | + original_name=overrides.get("original_name", "file.txt"), |
| 23 | + blob_path=overrides.get("blob_path", "blob/path/file.txt"), |
| 24 | + translated_path=overrides.get("translated_path", "translated/file.txt"), |
| 25 | + status=overrides.get("status", ProcessStatus.READY_TO_PROCESS), |
| 26 | + error_count=overrides.get("error_count", 0), |
| 27 | + syntax_count=overrides.get("syntax_count", 0), |
| 28 | + created_at=overrides.get("created_at", datetime.utcnow()), |
| 29 | + updated_at=overrides.get("updated_at", datetime.utcnow()) |
| 30 | + ) |
| 31 | + |
| 32 | +def make_batch_record(**overrides): |
| 33 | + return BatchRecord( |
| 34 | + batch_id=overrides.get("batch_id", "batch123"), |
| 35 | + user_id=overrides.get("user_id", "user1"), |
| 36 | + file_count=overrides.get("file_count", 1), |
| 37 | + created_at=overrides.get("created_at", datetime.utcnow().isoformat()), |
| 38 | + updated_at=overrides.get("updated_at", datetime.utcnow().isoformat()), |
| 39 | + status=overrides.get("status", ProcessStatus.READY_TO_PROCESS), |
| 40 | + ) |
| 41 | + |
| 42 | +# ---------- Fixtures ---------- |
| 43 | +@pytest.fixture |
| 44 | +def batch_service(): |
| 45 | + service = BatchService() |
| 46 | + service.logger = MagicMock() |
| 47 | + service.database = AsyncMock() |
| 48 | + return service |
| 49 | + |
| 50 | +# ---------- Tests ---------- |
| 51 | +@pytest.mark.asyncio |
| 52 | +async def test_get_batch_success(batch_service): |
| 53 | + batch_id = uuid4() |
| 54 | + user_id = "test_user" |
| 55 | + batch_service.database.get_batch.return_value = {"batch_id": str(batch_id)} |
| 56 | + batch_service.database.get_batch_files.return_value = [{"file_id": "f1"}] |
| 57 | + |
| 58 | + result = await batch_service.get_batch(batch_id, user_id) |
| 59 | + assert result["batch"]["batch_id"] == str(batch_id) |
| 60 | + assert result["files"] == [{"file_id": "f1"}] |
| 61 | + |
| 62 | +@pytest.mark.asyncio |
| 63 | +async def test_get_file_not_found(batch_service): |
| 64 | + batch_service.database.get_file.return_value = None |
| 65 | + result = await batch_service.get_file("missing_file_id") |
| 66 | + assert result is None |
| 67 | + |
| 68 | +def test_is_valid_uuid_valid(batch_service): |
| 69 | + assert batch_service.is_valid_uuid(str(uuid4())) is True |
| 70 | + |
| 71 | +def test_generate_file_path(batch_service): |
| 72 | + path = batch_service.generate_file_path("batch1", "user1", "file1", "file@.txt") |
| 73 | + assert path == "user1/batch1/file1/file_.txt" |
| 74 | + |
| 75 | +@pytest.mark.asyncio |
| 76 | +@patch("common.storage.blob_factory.BlobStorageFactory.get_storage", new_callable=AsyncMock) |
| 77 | +async def test_get_file_report_success(mock_storage, batch_service): |
| 78 | + file_id = "file1" |
| 79 | + file_record = make_file_record(file_id=file_id) |
| 80 | + batch_record = make_batch_record(batch_id=file_record.batch_id) |
| 81 | + |
| 82 | + batch_service.database.get_file.return_value = file_record.dict() |
| 83 | + batch_service.database.get_batch_from_id.return_value = batch_record.dict() |
| 84 | + batch_service.database.get_file_logs.return_value = [{"log_type": "INFO"}] |
| 85 | + |
| 86 | + with patch("common.models.api.FileRecord.fromdb", return_value=file_record), \ |
| 87 | + patch("common.models.api.BatchRecord.fromdb", return_value=batch_record), \ |
| 88 | + patch.object(mock_storage, "get_file", new=AsyncMock(return_value="translated content")): |
| 89 | + |
| 90 | + result = await batch_service.get_file_report(file_id) |
| 91 | + assert result["translated_content"] == "translated content" |
| 92 | + |
| 93 | +@pytest.mark.asyncio |
| 94 | +@patch("common.storage.blob_factory.BlobStorageFactory.get_storage", new_callable=AsyncMock) |
| 95 | +async def test_upload_file_to_batch_creates_batch(mock_storage, batch_service): |
| 96 | + batch_id = str(uuid4()) |
| 97 | + user_id = "test_user" |
| 98 | + filename = "doc.txt" |
| 99 | + file_mock = MagicMock(spec=UploadFile) |
| 100 | + file_mock.filename = filename |
| 101 | + file_mock.content_type = "text/plain" |
| 102 | + file_mock.read = AsyncMock(return_value=b"content") |
| 103 | + |
| 104 | + # Simulate batch creation |
| 105 | + batch_service.database.get_batch.return_value = None |
| 106 | + batch_service.database.create_batch.return_value = {"batch_id": batch_id} |
| 107 | + batch_service.database.get_batch_files.return_value = [{"file_id": "f1"}] |
| 108 | + batch_service.database.get_file.return_value = {"file_id": "new_id"} |
| 109 | + |
| 110 | + mock_storage.upload_file.return_value = None |
| 111 | + |
| 112 | + file_record = make_file_record(file_id="new_id", batch_id=batch_id) |
| 113 | + |
| 114 | + with patch("common.models.api.FileRecord.fromdb", return_value=file_record), \ |
| 115 | + patch("uuid.uuid4", return_value=uuid4()): |
| 116 | + |
| 117 | + result = await batch_service.upload_file_to_batch(batch_id, user_id, file_mock) |
| 118 | + assert "file" in result |
| 119 | + assert "batch" in result |
| 120 | + |
| 121 | +@pytest.mark.asyncio |
| 122 | +@patch("common.storage.blob_factory.BlobStorageFactory.get_storage", new_callable=AsyncMock) |
| 123 | +async def test_delete_batch_and_files_batch_not_found(mock_storage, batch_service): |
| 124 | + batch_service.database.get_batch.return_value = None |
| 125 | + result = await batch_service.delete_batch_and_files("batch123", "user1") |
| 126 | + assert result["message"] == "Batch not found" |
| 127 | + |
| 128 | +@pytest.mark.asyncio |
| 129 | +async def test_update_file_not_found(batch_service): |
| 130 | + batch_service.database.get_file.return_value = None |
| 131 | + with pytest.raises(HTTPException) as exc_info: |
| 132 | + await batch_service.update_file("file123", ProcessStatus.COMPLETED, FileResult.SUCCESS, 1, 2) |
| 133 | + assert exc_info.value.status_code == 404 |
| 134 | + |
| 135 | +@pytest.mark.asyncio |
| 136 | +async def test_batch_files_final_update_with_error_log(batch_service): |
| 137 | + file_id = str(uuid4()) |
| 138 | + file_record = make_file_record(file_id=file_id, translated_path=None, status=ProcessStatus.IN_PROGRESS) |
| 139 | + |
| 140 | + batch_service.database.get_batch_files.return_value = [file_record.dict()] |
| 141 | + batch_service.get_file_counts = AsyncMock(return_value=(1, 0)) |
| 142 | + batch_service.update_file_record = AsyncMock() |
| 143 | + batch_service.create_file_log = AsyncMock() |
| 144 | + |
| 145 | + with patch("common.models.api.FileRecord.fromdb", return_value=file_record): |
| 146 | + await batch_service.batch_files_final_update("batch1") |
| 147 | + batch_service.update_file_record.assert_awaited() |
0 commit comments