Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/apps/data_training/api/data_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def inner():
for sheet_name in sheet_names:

if get_excel_column_count(save_path, sheet_name) < len(use_cols):
raise Exception(trans("i18n_excel_import.col_num_not_match"))
raise HTTPException(400, trans("i18n_excel_import.col_num_not_match"))

df = pd.read_excel(
save_path,
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/terminology/api/terminology.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def inner():
for sheet_name in sheet_names:

if get_excel_column_count(save_path, sheet_name) < len(use_cols):
raise Exception(trans("i18n_excel_import.col_num_not_match"))
raise HTTPException(400, trans("i18n_excel_import.col_num_not_match"))

df = pd.read_excel(
save_path,
Expand Down
35 changes: 35 additions & 0 deletions backend/tests/test_excel_http_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,41 @@ def test_valid_knowledge_workbook_reaches_batch_import(
assert batch.call_args.args[:3] == (session, [], 1)


@pytest.mark.parametrize(
"module,batch_name,column_count",
[
(terminology, "batch_create_terminology", 6),
(data_training, "batch_create_training", 4),
],
)
@pytest.mark.parametrize("empty", [False, True])
@pytest.mark.parametrize(
"language,message",
[
("zh-CN", "EXCEL列数量不匹配"),
("en", "Number of columns in Excel does not match"),
],
)
def test_insufficient_excel_columns_return_localized_400(
api, monkeypatch, module, batch_name, column_count, empty, language, message
):
client, session = api
client.app.dependency_overrides.pop(get_i18n)
monkeypatch.setattr(module, "session_maker", lambda: session)
batch = Mock()
monkeypatch.setattr(module, batch_name, batch)
columns = [] if empty else range(column_count - 1)
response = client.post(
module.router.prefix + "/uploadExcel",
files={"file": ("invalid.xlsx", workbook(columns))},
headers={"Accept-Language": language},
)
assert response.status_code == 400, response.text
assert response.json() == message
batch.assert_not_called()
session.commit.assert_not_called()


@pytest.mark.parametrize(
"filename,exists,status,message",
[
Expand Down
Loading