Skip to content

Commit 1f49e4e

Browse files
committed
refactor: ruff checkテスト周り
1 parent 88deec3 commit 1f49e4e

7 files changed

Lines changed: 257 additions & 136 deletions

tests/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import pytest
2-
from unittest.mock import Mock, patch
31
import os
2+
from unittest.mock import Mock, patch
3+
4+
import pytest
5+
46
from src.config import SharePointConfig
57

68

tests/test_config.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import pytest
2-
from unittest.mock import patch, mock_open
31
import os
2+
from unittest.mock import patch
3+
4+
import pytest
45

56
from src.config import SharePointConfig
67

@@ -25,8 +26,14 @@ def test_missing_required_env_vars(self):
2526
validation_errors = config.validate()
2627

2728
assert len(validation_errors) > 0
28-
assert any("SHAREPOINT_TENANT_ID is required" in error for error in validation_errors)
29-
assert any("SHAREPOINT_CLIENT_ID is required" in error for error in validation_errors)
29+
assert any(
30+
"SHAREPOINT_TENANT_ID is required" in error
31+
for error in validation_errors
32+
)
33+
assert any(
34+
"SHAREPOINT_CLIENT_ID is required" in error
35+
for error in validation_errors
36+
)
3037

3138
def test_certificate_text_priority_over_file(self):
3239
"""証明書テキストがファイルパスより優先されることのテスト"""
@@ -82,7 +89,9 @@ def test_default_values(self):
8289

8390
assert config.default_max_results == 20
8491
assert "pdf" in config.allowed_file_extensions
85-
assert "Search for documents in SharePoint" in config.search_tool_description
92+
assert (
93+
"Search for documents in SharePoint" in config.search_tool_description
94+
)
8695
assert "Download a file from SharePoint" in config.download_tool_description
8796

8897
@pytest.mark.unit
@@ -119,16 +128,19 @@ def test_parse_onedrive_paths_basic(self):
119128
targets = config.parse_onedrive_paths()
120129

121130
assert len(targets) == 2
122-
131+
123132
# user1@company.com(フォルダー指定なし)
124133
assert targets[0]["email"] == "user1@company.com"
125134
assert targets[0]["folder_path"] == ""
126135
assert targets[0]["onedrive_path"] == "personal/user1_company_com"
127-
136+
128137
# user2@company.com:/Documents/Projects
129138
assert targets[1]["email"] == "user2@company.com"
130139
assert targets[1]["folder_path"] == "/Documents/Projects"
131-
assert targets[1]["onedrive_path"] == "personal/user2_company_com/Documents/Projects"
140+
assert (
141+
targets[1]["onedrive_path"]
142+
== "personal/user2_company_com/Documents/Projects"
143+
)
132144

133145
def test_parse_onedrive_paths_empty(self):
134146
"""OneDriveパス設定が空の場合のテスト"""
@@ -147,21 +159,23 @@ def test_parse_onedrive_paths_empty(self):
147159
def test_email_to_onedrive_path_conversion(self):
148160
"""メールアドレスからOneDriveパスへの変換テスト"""
149161
config = SharePointConfig()
150-
162+
151163
# 基本的な変換
152164
path = config._email_to_onedrive_path("user@company.com")
153165
assert path == "personal/user_company_com"
154-
166+
155167
# フォルダーパス付き
156168
path = config._email_to_onedrive_path("user@company.com", "/Documents/Projects")
157169
assert path == "personal/user_company_com/Documents/Projects"
158-
170+
159171
# 先頭スラッシュの除去
160172
path = config._email_to_onedrive_path("user@company.com", "Documents/Projects")
161173
assert path == "personal/user_company_com/Documents/Projects"
162-
174+
163175
# onmicrosoft.com ドメイン
164-
path = config._email_to_onedrive_path("admin@company.onmicrosoft.com", "/Documents")
176+
path = config._email_to_onedrive_path(
177+
"admin@company.onmicrosoft.com", "/Documents"
178+
)
165179
assert path == "personal/admin_company_onmicrosoft_com/Documents"
166180

167181
def test_include_onedrive_property(self):
@@ -359,7 +373,10 @@ def test_disable_multiple_tools(self):
359373
with patch.dict(os.environ, env_vars, clear=True):
360374
config = SharePointConfig()
361375

362-
assert config.disabled_tools == {"sharepoint_excel", "sharepoint_docs_download"}
376+
assert config.disabled_tools == {
377+
"sharepoint_excel",
378+
"sharepoint_docs_download",
379+
}
363380
assert config.is_tool_enabled("sharepoint_docs_search") is True
364381
assert config.is_tool_enabled("sharepoint_docs_download") is False
365382
assert config.is_tool_enabled("sharepoint_excel") is False
@@ -416,6 +433,9 @@ def test_whitespace_handling(self):
416433
with patch.dict(os.environ, env_vars, clear=True):
417434
config = SharePointConfig()
418435

419-
assert config.disabled_tools == {"sharepoint_excel", "sharepoint_docs_download"}
436+
assert config.disabled_tools == {
437+
"sharepoint_excel",
438+
"sharepoint_docs_download",
439+
}
420440
assert config.is_tool_enabled("sharepoint_excel") is False
421441
assert config.is_tool_enabled("sharepoint_docs_download") is False

tests/test_error_messages.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import zipfile
2+
from unittest.mock import Mock
23

34
import pytest
45
import requests
5-
from unittest.mock import Mock
66

77
from src.error_messages import (
88
ErrorCategory,
@@ -57,7 +57,10 @@ def test_http_error_404(self):
5757
result = handle_sharepoint_error(http_error, "search")
5858

5959
assert "error" in str(result).lower()
60-
assert "configuration" in str(result).lower() or "administrator" in str(result).lower()
60+
assert (
61+
"configuration" in str(result).lower()
62+
or "administrator" in str(result).lower()
63+
)
6164

6265
@pytest.mark.unit
6366
def test_connection_error(self):
@@ -77,7 +80,10 @@ def test_timeout_error(self):
7780
result = handle_sharepoint_error(timeout_error, "search")
7881

7982
assert "error" in str(result).lower()
80-
assert "configuration" in str(result).lower() or "administrator" in str(result).lower()
83+
assert (
84+
"configuration" in str(result).lower()
85+
or "administrator" in str(result).lower()
86+
)
8187

8288
@pytest.mark.unit
8389
def test_certificate_error(self):
@@ -87,7 +93,10 @@ def test_certificate_error(self):
8793
result = handle_sharepoint_error(cert_error, "search")
8894

8995
assert "certificate" in str(result).lower()
90-
assert "configured" in str(result).lower() or "configuration" in str(result).lower()
96+
assert (
97+
"configured" in str(result).lower()
98+
or "configuration" in str(result).lower()
99+
)
91100

92101
@pytest.mark.unit
93102
def test_generic_error(self):
@@ -97,7 +106,10 @@ def test_generic_error(self):
97106
result = handle_sharepoint_error(generic_error, "search")
98107

99108
assert "unexpected error" in str(result).lower()
100-
assert "configuration" in str(result).lower() or "administrator" in str(result).lower()
109+
assert (
110+
"configuration" in str(result).lower()
111+
or "administrator" in str(result).lower()
112+
)
101113

102114
@pytest.mark.unit
103115
def test_download_context_specific_message(self):
@@ -107,7 +119,10 @@ def test_download_context_specific_message(self):
107119
result = handle_sharepoint_error(generic_error, "download")
108120

109121
assert "error" in str(result).lower()
110-
assert "configuration" in str(result).lower() or "administrator" in str(result).lower()
122+
assert (
123+
"configuration" in str(result).lower()
124+
or "administrator" in str(result).lower()
125+
)
111126

112127
@pytest.mark.unit
113128
def test_http_error_without_response(self):
@@ -117,7 +132,10 @@ def test_http_error_without_response(self):
117132
result = handle_sharepoint_error(http_error, "search")
118133

119134
assert "error" in str(result).lower()
120-
assert "configuration" in str(result).lower() or "administrator" in str(result).lower()
135+
assert (
136+
"configuration" in str(result).lower()
137+
or "administrator" in str(result).lower()
138+
)
121139

122140

123141
class TestExcelErrorClassification:
@@ -145,7 +163,9 @@ def test_excel_file_not_found_http_404(self):
145163
@pytest.mark.unit
146164
def test_excel_sheet_not_found(self):
147165
"""シートが見つからないエラーの分類テスト"""
148-
error = ValueError("Sheet 'NonExistent' not found. Available sheets: ['Sheet1']")
166+
error = ValueError(
167+
"Sheet 'NonExistent' not found. Available sheets: ['Sheet1']"
168+
)
149169

150170
result = handle_sharepoint_error(
151171
error,
@@ -208,4 +228,4 @@ def test_excel_context_preserves_file_path(self):
208228
excel_context={"file_path": file_path},
209229
)
210230

211-
assert file_path in str(result)
231+
assert file_path in str(result)

0 commit comments

Comments
 (0)