Skip to content

Commit 7e0937c

Browse files
author
Harmanpreet Kaur
committed
solved pylint issue of api_test.py
1 parent 4ffa2af commit 7e0937c

3 files changed

Lines changed: 160 additions & 7 deletions

File tree

src/tests/backend/common/models/api_test.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import pytest
2-
from uuid import uuid4
31
from datetime import datetime
4-
from backend.common.models.api import (
5-
FileLog, FileRecord, FileProcessUpdate, FileProcessUpdateJSONEncoder,
6-
QueueBatch, BatchRecord,
7-
LogType, AgentType, AuthorRole, ProcessStatus, FileResult, TranslateType
8-
)
2+
from uuid import uuid4
3+
4+
from backend.common.models.api import AgentType, BatchRecord, FileLog, FileProcessUpdate, FileProcessUpdateJSONEncoder, FileRecord, FileResult, ProcessStatus, QueueBatch, TranslateType
5+
6+
import pytest
7+
98

109
@pytest.fixture
1110
def common_datetime():
1211
return datetime.now()
1312

13+
1414
@pytest.fixture
1515
def uuid_pair():
1616
return str(uuid4()), str(uuid4())
1717

18+
1819
def test_filelog_fromdb_and_dict(uuid_pair, common_datetime):
1920
log_id, file_id = uuid_pair
2021
data = {
@@ -33,6 +34,7 @@ def test_filelog_fromdb_and_dict(uuid_pair, common_datetime):
3334

3435
assert log.dict()["author_role"] == "user"
3536

37+
3638
def test_filerecord_fromdb_and_dict(uuid_pair, common_datetime):
3739
file_id, batch_id = uuid_pair
3840
data = {
@@ -53,6 +55,7 @@ def test_filerecord_fromdb_and_dict(uuid_pair, common_datetime):
5355
assert record.dict()["status"] == "ready_to_process"
5456
assert record.dict()["file_result"] == "warning"
5557

58+
5659
def test_fileprocessupdate_dict(uuid_pair):
5760
file_id, batch_id = uuid_pair
5861
update = FileProcessUpdate(
@@ -69,6 +72,7 @@ def test_fileprocessupdate_dict(uuid_pair):
6972
assert result["agent_type"] == "fixer"
7073
assert result["agent_message"] == "Translation done"
7174

75+
7276
def test_fileprocessupdate_json_encoder(uuid_pair):
7377
file_id, batch_id = uuid_pair
7478
update = FileProcessUpdate(
@@ -83,6 +87,7 @@ def test_fileprocessupdate_json_encoder(uuid_pair):
8387
assert "failed" in json_string
8488
assert "human" in json_string
8589

90+
8691
def test_queuebatch_dict(uuid_pair, common_datetime):
8792
batch_id, _ = uuid_pair
8893
batch = QueueBatch(
@@ -98,6 +103,7 @@ def test_queuebatch_dict(uuid_pair, common_datetime):
98103
assert result["status"] == "in_process"
99104
assert result["user_id"] == "user123"
100105

106+
101107
def test_batchrecord_fromdb_and_dict(uuid_pair, common_datetime):
102108
batch_id, _ = uuid_pair
103109
data = {

src/tests/backend/common/services/__init__.py

Whitespace-only changes.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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

Comments
 (0)