fix(task2): import templates must not import sample data

This commit is contained in:
Atharv1705
2026-11-17 17:45:27 +05:30
parent 041a4075dd
commit 026da64e71
5 changed files with 136 additions and 22 deletions
+4 -4
View File
@@ -54,8 +54,8 @@ def test_import_template_download_and_rbac(client):
assert 'filename="asset_import_template.xlsx"' in res.headers["content-disposition"]
wb = openpyxl.load_workbook(BytesIO(res.content))
assert "Assets" in wb.sheetnames
assert "Master Reference" in wb.sheetnames
assert wb.sheetnames == ["Assets", "Example", "Master Reference"]
assert wb["Example"].cell(row=1, column=1).value == "Example only - this sheet is ignored on import"
ws = wb["Assets"]
headers = [ws.cell(row=1, column=i).value for i in range(1, 16)]
assert headers[:6] == ["asset_code", "serial_no", "name", "category", "department", "location"]
@@ -71,8 +71,8 @@ def test_bulk_import_dry_run_validation_and_commit(client, db):
# Read a valid sample from template so we match real seeded department/location/category
tmpl_res = client.get("/api/assets/import-template", headers=bio_headers)
wb_tmpl = openpyxl.load_workbook(BytesIO(tmpl_res.content))
ws_tmpl = wb_tmpl["Assets"]
headers = [ws_tmpl.cell(row=1, column=i).value for i in range(1, 16)]
ws_tmpl = wb_tmpl["Example"]
headers = [wb_tmpl["Assets"].cell(row=1, column=i).value for i in range(1, 16)]
cat_name = ws_tmpl.cell(row=2, column=4).value
dept_code = ws_tmpl.cell(row=2, column=5).value
loc_name = ws_tmpl.cell(row=2, column=6).value
+89
View File
@@ -0,0 +1,89 @@
from io import BytesIO
import openpyxl
import pytest
from app.seed.seed import seed_database
@pytest.fixture(scope="module", autouse=True)
def seeded_task2_db(db, tmp_path_factory):
upload_dir = tmp_path_factory.mktemp("task2_test_uploads")
seed_database(db, upload_dir=upload_dir)
return upload_dir
def _auth_headers(client, email: str = "admin@poona.demo") -> dict[str, str]:
res = client.post("/api/auth/login", json={"email": email, "password": "Demo@123"})
assert res.status_code == 200, res.text
return {"Authorization": f"Bearer {res.json()['access_token']}"}
@pytest.mark.parametrize(
"template_url,import_url,file_field,filename,expected_sheet1",
[
(
"/api/assets/import-template",
"/api/assets/bulk-import?dry_run=true",
"file",
"asset_template.xlsx",
"Assets",
),
(
"/api/contracts/import-template",
"/api/contracts/bulk-import?dry_run=true",
"file",
"contracts_template.xlsx",
"Contracts",
),
(
"/api/calibration/import-template",
"/api/calibration/bulk-import?dry_run=true",
"file",
"calibration_template.xlsx",
"Calibration Plans",
),
(
"/api/insurance/import-template",
"/api/insurance/bulk-import?dry_run=true",
"file",
"insurance_template.xlsx",
"Insurance Policies",
),
],
)
def test_download_template_and_upload_unchanged_dry_run_zero_rows(
client, template_url, import_url, file_field, filename, expected_sheet1
):
headers = _auth_headers(client)
# 1. Download template
dl_res = client.get(template_url, headers=headers)
assert dl_res.status_code == 200
content = dl_res.content
# 2. Verify sheet structure
wb = openpyxl.load_workbook(BytesIO(content))
assert wb.sheetnames == [expected_sheet1, "Example", "Master Reference"]
ws1 = wb[expected_sheet1]
# First sheet has only header row (max_row == 1)
assert ws1.max_row == 1
ws2 = wb["Example"]
# Second sheet has disclaimer on row 1
assert ws2.cell(row=1, column=1).value == "Example only - this sheet is ignored on import"
# Second sheet has sample row on row 2
assert ws2.cell(row=2, column=1).value is not None
# 3. Upload unchanged template with dry_run=true: must return total_rows == 0
up_res = client.post(
import_url,
headers=headers,
files={file_field: (filename, content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")},
)
assert up_res.status_code == 200, up_res.text
data = up_res.json()
assert data["total_rows"] == 0
assert data["valid_count"] == 0
assert data["error_count"] == 0
assert data["created_count"] == 0