Issue with Task Execution in Scheduler When Using EsmeraldTestClient for File-Upload API Testing #475
-
Hi Team, I am facing an issue while running test cases using EsmeraldTestClient. I am developing a file-upload API with form-data. Issue Description: below is the code snippet of my implementation. # test_file_upload.py
import json
import pytest
from urllib3.filepost import encode_multipart_formdata
from esmerald import status
from myapp.main import get_application
@pytest.fixture
def app():
"""
"""
app = get_application()
return app
@pytest.fixture
def get_test_client(app):
"""
"""
return EsmeraldTestClient(app)
@pytest.fixture
def valid_json_file():
"""Fixture to provide a valid JSON file for upload testing."""
with open("json_file_path", "rb") as file:
return file.read()
from urllib3.filepost import encode_multipart_formdata
class TestFileUploadAPI:
"""Class to test the file upload API."""
def test_upload_file_with_valid_conf(
self,
get_user_client,
valid_json_file
):
"""
"""
payload = {
"file_context": "E",
"file": ("valid_file.csv", valid_json_file),
"applicable_date": "2025-01-21",
}
body, content_type = encode_multipart_formdata(payload)
get_user_client.headers["Content-Type"] = content_type
response = client.post(url=valid_upload_file_url, data=body)
response_message = json.loads(response.text)
assert response.status_code == status.HTTP_201_CREATED
assert (
response_message.get("detail")
== "File uploaded successfully, file processing started."
)
# upload_file.py
def executable_task(**kwargs):
...
@post(status_code=status.HTTP_201_CREATED)
async def upload_files(request: Request, data: Any = Form(), **kwargs: Any) -> JSONResponse:
# Parse form data
request_file = data.get("file")
# for validating type of file_context it should be str only
context_name = data.get("file_context")
applicable_date = data.get("applicable_date")
# validations part
job = request.app.scheduler_config.handler.add_task(
executable_task,
kwargs={
"context_name ": context_name ,
"applicable_date ": applicable_date
},
)
return JSONResponse({"detail": "File uploaded successfully, file processing started."}) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
@prashikdewtale10 so this is related with EsmeraldTestClient. Can you please upgrade to the latest, please and let me know? There were a lot of improvements and this might have been addressed. |
Beta Was this translation helpful? Give feedback.
So, there many ways. The create_client Indeed you do need but it returns an EsmeraldTestClient object.
I need to understand why using the test client seems not to work for your tests.