61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from app import app
|
|
from openpyxl import Workbook
|
|
import io
|
|
|
|
client = TestClient(app)
|
|
|
|
@pytest.fixture
|
|
def sample_excel_file():
|
|
wb = Workbook()
|
|
ws = wb.active
|
|
ws.title = "APITest"
|
|
ws['A1'] = "API"
|
|
|
|
out = io.BytesIO()
|
|
wb.save(out)
|
|
out.seek(0)
|
|
return out
|
|
|
|
def test_health_check():
|
|
response = client.get("/health")
|
|
assert response.status_code == 200
|
|
assert response.json() == {"status": "ok"}
|
|
|
|
def test_convert_endpoint(sample_excel_file):
|
|
files = {'file': ('test.xlsx', sample_excel_file, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}
|
|
response = client.post("/api/v1/convert", files=files)
|
|
|
|
assert response.status_code == 200
|
|
assert response.headers["content-type"] == "image/png"
|
|
assert len(response.content) > 0
|
|
|
|
def test_convert_invalid_file_type():
|
|
files = {'file': ('test.txt', io.BytesIO(b"dummy"), 'text/plain')}
|
|
response = client.post("/api/v1/convert", files=files)
|
|
|
|
assert response.status_code == 400
|
|
assert "Invalid file format" in response.json()["detail"]
|
|
|
|
def test_convert_specific_sheet(sample_excel_file):
|
|
# Re-create file because previous read might have consumed it if not handled carefully (TestClient usually handles this)
|
|
# But let's be safe and use the fixture which returns a new BytesIO if we construct it that way.
|
|
# Actually the fixture returns the same object, let's seek 0 just in case.
|
|
sample_excel_file.seek(0)
|
|
|
|
files = {'file': ('test.xlsx', sample_excel_file, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}
|
|
data = {'sheet_name': 'APITest'}
|
|
response = client.post("/api/v1/convert", files=files, data=data)
|
|
|
|
assert response.status_code == 200
|
|
|
|
def test_convert_missing_sheet(sample_excel_file):
|
|
sample_excel_file.seek(0)
|
|
files = {'file': ('test.xlsx', sample_excel_file, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}
|
|
data = {'sheet_name': 'MissingSheet'}
|
|
response = client.post("/api/v1/convert", files=files, data=data)
|
|
|
|
assert response.status_code == 400
|
|
assert "Sheet 'MissingSheet' not found" in response.json()["detail"]
|