104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
import uuid
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app
|
|
from app.auth.security import get_current_user
|
|
from app.database.base import SessionLocal
|
|
from app.models.flexible import FlexibleImport
|
|
|
|
|
|
def test_batch_import_unknown_csv_saves_flexible_rows():
|
|
# Override auth to bypass JWT for this test
|
|
app.dependency_overrides[get_current_user] = lambda: {
|
|
"id": "test",
|
|
"username": "tester",
|
|
"is_admin": True,
|
|
"is_active": True,
|
|
}
|
|
|
|
client = TestClient(app)
|
|
|
|
unique_suffix = uuid.uuid4().hex[:8]
|
|
filename = f"UNKNOWN_TEST_{unique_suffix}.csv"
|
|
csv_content = "col1,col2\nA,B\nC,D\n"
|
|
|
|
try:
|
|
resp = client.post(
|
|
"/api/import/batch-upload",
|
|
files=[("files", (filename, csv_content, "text/csv"))],
|
|
)
|
|
assert resp.status_code == 200
|
|
payload = resp.json()
|
|
assert "batch_results" in payload
|
|
result = next((r for r in payload["batch_results"] if r["file_type"] == filename), None)
|
|
assert result is not None
|
|
assert result["status"] == "success"
|
|
assert result["imported_count"] == 2
|
|
assert result["auto_mapping"]["flexible_saved_rows"] == 2
|
|
|
|
# Verify rows persisted
|
|
db = SessionLocal()
|
|
try:
|
|
count = (
|
|
db.query(FlexibleImport)
|
|
.filter(FlexibleImport.file_type == filename)
|
|
.count()
|
|
)
|
|
assert count == 2
|
|
finally:
|
|
# Clean up created rows to keep DB tidy
|
|
db.query(FlexibleImport).filter(FlexibleImport.file_type == filename).delete()
|
|
db.commit()
|
|
db.close()
|
|
finally:
|
|
# Restore dependencies
|
|
app.dependency_overrides.pop(get_current_user, None)
|
|
|
|
|
|
def test_single_upload_flexible_creates_rows():
|
|
# Override auth to bypass JWT for this test
|
|
app.dependency_overrides[get_current_user] = lambda: {
|
|
"id": "test",
|
|
"username": "tester",
|
|
"is_admin": True,
|
|
"is_active": True,
|
|
}
|
|
|
|
client = TestClient(app)
|
|
|
|
filename = "SINGLE_UNKNOWN.csv"
|
|
csv_content = "a,b\n1,2\n3,4\n"
|
|
|
|
try:
|
|
# Upload via flexible-only endpoint
|
|
resp = client.post(
|
|
"/api/import/upload-flexible",
|
|
files={"file": (filename, csv_content, "text/csv")},
|
|
data={"replace_existing": "false"},
|
|
)
|
|
assert resp.status_code == 200
|
|
payload = resp.json()
|
|
assert payload["file_type"] == filename
|
|
assert payload["imported_count"] == 2
|
|
assert payload["auto_mapping"]["flexible_saved_rows"] == 2
|
|
|
|
# Verify rows persisted
|
|
db = SessionLocal()
|
|
try:
|
|
count = (
|
|
db.query(FlexibleImport)
|
|
.filter(FlexibleImport.file_type == filename)
|
|
.count()
|
|
)
|
|
assert count == 2
|
|
finally:
|
|
# Clean up created rows to keep DB tidy
|
|
db.query(FlexibleImport).filter(FlexibleImport.file_type == filename).delete()
|
|
db.commit()
|
|
db.close()
|
|
finally:
|
|
# Restore dependencies
|
|
app.dependency_overrides.pop(get_current_user, None)
|
|
|