Skip to content

Commit b9b579c

Browse files
pylint issues fix
1 parent 81cd583 commit b9b579c

17 files changed

Lines changed: 367 additions & 437 deletions

src/backend/pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@ exclude_lines = [
4545
"raise NotImplementedError",
4646
"if __name__ == .__main__.:",
4747
"if TYPE_CHECKING:",
48-
]
48+
]
49+
50+
[tool.pylint."messages control"]
51+
# C0411 disabled because flake8-import-order requires application imports before
52+
# third-party imports, which conflicts with pylint's default order. Since flake8
53+
# is used in CI, we follow its convention.
54+
disable = ["C0411"]

src/tests/backend/api/api_routes_test.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
"""Tests for API routes module."""
2+
# pylint: disable=redefined-outer-name,unused-argument
23

3-
import io
4-
import json
54
import uuid
65
from unittest.mock import AsyncMock, MagicMock, patch
76

8-
import pytest
9-
from fastapi import FastAPI, HTTPException
10-
from fastapi.testclient import TestClient
11-
from httpx import ASGITransport, AsyncClient
12-
137
from backend.api.api_routes import (
14-
record_exception_to_trace,
15-
router,
16-
start_processing,
8+
delete_all_details,
9+
delete_batch_details,
10+
delete_file_details,
1711
download_files,
1812
get_batch_status,
1913
get_batch_summary,
20-
upload_file,
2114
get_file_details,
22-
delete_batch_details,
23-
delete_file_details,
24-
delete_all_details,
2515
list_batch_history,
16+
record_exception_to_trace,
17+
router,
18+
start_processing,
19+
upload_file,
2620
)
2721

22+
from fastapi import FastAPI, HTTPException
23+
24+
import pytest
25+
2826

2927
@pytest.fixture
3028
def test_app():
@@ -118,7 +116,7 @@ async def test_start_processing_success(self, mock_process_batch, mock_close_con
118116
})
119117

120118
result = await start_processing(mock_request)
121-
119+
122120
assert result["batch_id"] == batch_id
123121
assert result["status"] == "Processing completed"
124122
mock_process_batch.assert_called_once()
@@ -154,7 +152,7 @@ async def test_download_files_success(self, mock_batch_service, mock_track_event
154152
]
155153

156154
response = await download_files(batch_id)
157-
155+
158156
assert response.media_type == "application/zip"
159157
assert "Content-Disposition" in response.headers
160158

@@ -192,7 +190,7 @@ async def test_get_batch_status_success(self, mock_batch_service, mock_auth_user
192190
mock_batch_service.get_batch.return_value = {"batch_id": batch_id, "status": "completed"}
193191

194192
result = await get_batch_status(mock_request, batch_id)
195-
193+
196194
assert result["batch_id"] == batch_id
197195

198196
@pytest.mark.asyncio
@@ -241,7 +239,7 @@ async def test_get_batch_summary_success(self, mock_batch_service, mock_auth_use
241239
mock_batch_service.get_batch_summary.return_value = {"total_files": 5}
242240

243241
result = await get_batch_summary(mock_request, batch_id)
244-
242+
245243
assert result["total_files"] == 5
246244

247245
@pytest.mark.asyncio
@@ -267,7 +265,7 @@ async def test_upload_file_success(self, mock_batch_service, mock_auth_user, moc
267265
mock_batch_service.upload_file_to_batch.return_value = {"file_id": "test-file-id"}
268266

269267
result = await upload_file(mock_request, mock_file, batch_id)
270-
268+
271269
assert result["file_id"] == "test-file-id"
272270

273271
@pytest.mark.asyncio
@@ -307,7 +305,7 @@ async def test_get_file_details_success(self, mock_batch_service, mock_auth_user
307305
mock_batch_service.get_file_report.return_value = {"file_id": file_id}
308306

309307
result = await get_file_details(mock_request, file_id)
310-
308+
311309
assert result["file_id"] == file_id
312310

313311
@pytest.mark.asyncio
@@ -342,7 +340,7 @@ async def test_delete_batch_success(self, mock_batch_service, mock_auth_user, mo
342340
mock_batch_service.delete_batch_and_files.return_value = True
343341

344342
result = await delete_batch_details(mock_request, batch_id)
345-
343+
346344
assert result["message"] == "Batch deleted successfully"
347345

348346
@pytest.mark.asyncio
@@ -367,7 +365,7 @@ async def test_delete_file_success(self, mock_batch_service, mock_auth_user, moc
367365
mock_batch_service.delete_file.return_value = True
368366

369367
result = await delete_file_details(mock_request, file_id)
370-
368+
371369
assert result["message"] == "File deleted successfully"
372370

373371
@pytest.mark.asyncio
@@ -391,7 +389,7 @@ async def test_delete_all_success(self, mock_batch_service, mock_auth_user, mock
391389
mock_batch_service.delete_all_from_storage_cosmos.return_value = True
392390

393391
result = await delete_all_details(mock_request)
394-
392+
395393
assert result["message"] == "All user data deleted successfully"
396394

397395
@pytest.mark.asyncio
@@ -415,7 +413,7 @@ async def test_list_batch_history_success(self, mock_batch_service, mock_auth_us
415413
mock_batch_service.get_batch_history.return_value = [{"batch_id": "test"}]
416414

417415
result = await list_batch_history(mock_request)
418-
416+
419417
assert len(result) == 1
420418

421419
@pytest.mark.asyncio
@@ -424,8 +422,8 @@ async def test_list_batch_history_with_pagination(self, mock_batch_service, mock
424422
mock_request = MagicMock()
425423
mock_batch_service.get_batch_history.return_value = [{"batch_id": "test"}]
426424

427-
result = await list_batch_history(mock_request, offset=10, limit=5)
428-
425+
await list_batch_history(mock_request, offset=10, limit=5)
426+
429427
mock_batch_service.get_batch_history.assert_called_once()
430428

431429
@pytest.mark.asyncio
@@ -435,5 +433,5 @@ async def test_list_batch_history_empty(self, mock_batch_service, mock_auth_user
435433
mock_batch_service.get_batch_history.return_value = None
436434

437435
result = await list_batch_history(mock_request)
438-
436+
439437
assert result.status_code == 404

src/tests/backend/api/event_utils_test.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"""Tests for event_utils module."""
22

33
import os
4-
from unittest.mock import patch, MagicMock
5-
6-
import pytest
4+
from unittest.mock import patch
75

86
from backend.api.event_utils import track_event_if_configured
97

@@ -16,7 +14,7 @@ def test_track_event_with_instrumentation_key(self):
1614
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": "test-key"}):
1715
with patch("backend.api.event_utils.track_event") as mock_track:
1816
track_event_if_configured("TestEvent", {"key": "value"})
19-
17+
2018
mock_track.assert_called_once_with("TestEvent", {"key": "value"})
2119

2220
def test_track_event_without_instrumentation_key(self):
@@ -27,7 +25,7 @@ def test_track_event_without_instrumentation_key(self):
2725
with patch("backend.api.event_utils.track_event") as mock_track:
2826
with patch("backend.api.event_utils.logging.warning") as mock_warning:
2927
track_event_if_configured("TestEvent", {"key": "value"})
30-
28+
3129
mock_track.assert_not_called()
3230
mock_warning.assert_called_once()
3331

@@ -36,7 +34,7 @@ def test_track_event_with_empty_data(self):
3634
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": "test-key"}):
3735
with patch("backend.api.event_utils.track_event") as mock_track:
3836
track_event_if_configured("TestEvent", {})
39-
37+
4038
mock_track.assert_called_once_with("TestEvent", {})
4139

4240
def test_track_event_with_complex_data(self):
@@ -49,7 +47,7 @@ def test_track_event_with_complex_data(self):
4947
"status": "completed",
5048
"nested": {"key": "value"},
5149
}
52-
50+
5351
track_event_if_configured("ComplexEvent", complex_data)
54-
52+
5553
mock_track.assert_called_once_with("ComplexEvent", complex_data)
Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
"""Tests for status_updates module."""
22

3-
import asyncio
43
from unittest.mock import AsyncMock, MagicMock, patch
54

6-
import pytest
7-
85
from backend.api.status_updates import (
96
ConnectionManager,
107
app_connection_manager,
11-
send_status_update_async,
12-
send_status_update,
138
close_connection,
9+
send_status_update,
10+
send_status_update_async,
1411
)
15-
from backend.common.models.api import FileProcessUpdate, ProcessStatus, FileResult
12+
13+
import pytest
1614

1715

1816
class TestConnectionManager:
@@ -21,15 +19,15 @@ class TestConnectionManager:
2119
def test_init(self):
2220
"""Test ConnectionManager initialization."""
2321
manager = ConnectionManager()
24-
assert manager.connections == {}
22+
assert not manager.connections
2523

2624
def test_add_connection(self):
2725
"""Test adding a connection."""
2826
manager = ConnectionManager()
2927
mock_websocket = MagicMock()
30-
28+
3129
manager.add_connection("batch-1", mock_websocket)
32-
30+
3331
assert "batch-1" in manager.connections
3432
assert manager.connections["batch-1"] == mock_websocket
3533

@@ -38,15 +36,15 @@ def test_remove_connection(self):
3836
manager = ConnectionManager()
3937
mock_websocket = MagicMock()
4038
manager.add_connection("batch-1", mock_websocket)
41-
39+
4240
manager.remove_connection("batch-1")
43-
41+
4442
assert "batch-1" not in manager.connections
4543

4644
def test_remove_nonexistent_connection(self):
4745
"""Test removing a connection that doesn't exist."""
4846
manager = ConnectionManager()
49-
47+
5048
# Should not raise an error
5149
manager.remove_connection("nonexistent")
5250

@@ -55,17 +53,17 @@ def test_get_connection(self):
5553
manager = ConnectionManager()
5654
mock_websocket = MagicMock()
5755
manager.add_connection("batch-1", mock_websocket)
58-
56+
5957
result = manager.get_connection("batch-1")
60-
58+
6159
assert result == mock_websocket
6260

6361
def test_get_nonexistent_connection(self):
6462
"""Test getting a connection that doesn't exist."""
6563
manager = ConnectionManager()
66-
64+
6765
result = manager.get_connection("nonexistent")
68-
66+
6967
assert result is None
7068

7169

@@ -76,22 +74,22 @@ class TestSendStatusUpdateAsync:
7674
async def test_send_status_update_async_with_connection(self):
7775
"""Test sending status update when connection exists."""
7876
mock_websocket = AsyncMock()
79-
77+
8078
status = MagicMock()
8179
status.batch_id = "batch-1"
82-
80+
8381
with patch.object(app_connection_manager, 'get_connection', return_value=mock_websocket):
8482
with patch("json.dumps", return_value='{"batch_id": "batch-1"}'):
8583
await send_status_update_async(status)
86-
84+
8785
mock_websocket.send_text.assert_called_once()
8886

8987
@pytest.mark.asyncio
9088
async def test_send_status_update_async_no_connection(self):
9189
"""Test sending status update when no connection exists."""
9290
status = MagicMock()
9391
status.batch_id = "batch-1"
94-
92+
9593
with patch.object(app_connection_manager, 'get_connection', return_value=None):
9694
# Should not raise an error
9795
await send_status_update_async(status)
@@ -104,34 +102,34 @@ def test_send_status_update_with_connection(self):
104102
"""Test sending status update synchronously when connection exists."""
105103
mock_websocket = MagicMock()
106104
mock_loop = MagicMock()
107-
105+
108106
status = MagicMock()
109107
status.batch_id = "batch-1"
110-
108+
111109
with patch.object(app_connection_manager, 'get_connection', return_value=mock_websocket):
112110
with patch('backend.api.status_updates.asyncio.get_event_loop', return_value=mock_loop):
113111
with patch('backend.api.status_updates.asyncio.run_coroutine_threadsafe') as mock_run:
114112
with patch('backend.api.status_updates.json.dumps', return_value='{"batch_id": "batch-1"}'):
115113
send_status_update(status)
116-
114+
117115
mock_run.assert_called_once()
118116

119117
def test_send_status_update_no_connection(self):
120118
"""Test sending status update when no connection exists."""
121119
status = MagicMock()
122120
status.batch_id = "batch-1"
123-
121+
124122
with patch.object(app_connection_manager, 'get_connection', return_value=None):
125123
# Should not raise an error
126124
send_status_update(status)
127125

128126
def test_send_status_update_exception(self):
129127
"""Test sending status update when exception occurs."""
130128
mock_websocket = MagicMock()
131-
129+
132130
status = MagicMock()
133131
status.batch_id = "batch-1"
134-
132+
135133
with patch.object(app_connection_manager, 'get_connection', return_value=mock_websocket):
136134
with patch('backend.api.status_updates.asyncio.get_event_loop', side_effect=RuntimeError("No event loop")):
137135
# Should handle exception gracefully
@@ -146,13 +144,13 @@ async def test_close_connection_with_connection(self):
146144
"""Test closing a connection that exists."""
147145
mock_websocket = MagicMock()
148146
mock_loop = MagicMock()
149-
147+
150148
with patch.object(app_connection_manager, 'get_connection', return_value=mock_websocket):
151149
with patch.object(app_connection_manager, 'remove_connection') as mock_remove:
152150
with patch('asyncio.get_event_loop', return_value=mock_loop):
153151
with patch('asyncio.run_coroutine_threadsafe'):
154152
await close_connection("batch-1")
155-
153+
156154
mock_remove.assert_called_once_with("batch-1")
157155

158156
@pytest.mark.asyncio
@@ -161,6 +159,6 @@ async def test_close_connection_no_connection(self):
161159
with patch.object(app_connection_manager, 'get_connection', return_value=None):
162160
with patch.object(app_connection_manager, 'remove_connection') as mock_remove:
163161
await close_connection("batch-1")
164-
162+
165163
# Should still call remove_connection
166164
mock_remove.assert_called_once_with("batch-1")

src/tests/backend/app_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# pylint: disable=redefined-outer-name
2+
"""Tests for the FastAPI application."""
3+
14
from backend.app import create_app
25

36
from fastapi import FastAPI
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# sql_agents tests package
1+
# sql_agents tests package\n# pylint: disable=duplicate-code

0 commit comments

Comments
 (0)