Skip to content

Commit

Permalink
fix: error on empty csv (#216)
Browse files Browse the repository at this point in the history
* fix error on csv

* refactor: improve test

* refactor: unused variable

* refactor: unused variable
  • Loading branch information
rivamarco authored Dec 18, 2024
1 parent 66d945e commit 1af295c
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
MetricsError,
ModelError,
SchemaException,
internal_exception_handler,
metrics_exception_handler,
model_exception_handler,
request_validation_exception_handler,
Expand Down Expand Up @@ -136,3 +137,4 @@ async def lifespan(fastapi: FastAPI):
app.add_exception_handler(MetricsError, metrics_exception_handler)
app.add_exception_handler(SchemaException, schema_exception_handler)
app.add_exception_handler(RequestValidationError, request_validation_exception_handler)
app.add_exception_handler(Exception, internal_exception_handler)
8 changes: 8 additions & 0 deletions api/app/models/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,11 @@ def request_validation_exception_handler(_, err: RequestValidationError):
status_code=422, # https://www.rfc-editor.org/rfc/rfc9110.html#name-422-unprocessable-content
content=jsonable_encoder(ErrorOut(error_message)),
)


def internal_exception_handler(_, exc: Exception) -> JSONResponse:
logger.error('Internal error occurred [%s]', exc)
return JSONResponse(
status_code=500,
content=jsonable_encoder(ErrorOut(f'Internal server error occurred {exc}')),
)
2 changes: 1 addition & 1 deletion api/app/services/file_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def validate_file(
) -> None:
file_upload_config = get_config().file_upload_config
_f_name = csv_file.filename
if csv_file.filename is None:
if csv_file.filename is None or csv_file.size == 0:
raise InvalidFileException('The file is empty. Please enter a valid file.')
if csv_file.size is not None and csv_file.size >= file_upload_config.max_bytes:
raise FileTooLargeException(
Expand Down
4 changes: 4 additions & 0 deletions api/tests/commons/csv_file_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def get_uncorrect_sample_csv_file() -> UploadFile:
return UploadFile(BytesIO(df.to_csv(index=False).encode()), filename=None)


def get_empty_sample_csv_file() -> UploadFile:
return UploadFile(BytesIO(), filename='sample.csv', size=0)


def get_file_using_sep(sep: str) -> UploadFile:
df = get_dataframe_with_sep(sep)
return UploadFile(
Expand Down
2 changes: 1 addition & 1 deletion api/tests/routes/healthcheck_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from app import main

client = TestClient(main.app)
client = TestClient(main.app, raise_server_exceptions=False)


def test_healthcheck():
Expand Down
9 changes: 8 additions & 1 deletion api/tests/routes/infer_schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class TestInferSchemaRoute(unittest.TestCase):
def setUp(self):
self.client = TestClient(main.app)
self.client = TestClient(main.app, raise_server_exceptions=False)

def test_infer_schema(self):
file = csv.get_correct_sample_csv_file()
Expand All @@ -28,3 +28,10 @@ def test_infer_schema_invalid_file(self):
def test_infer_schema_no_file(self):
response = self.client.post('/api/schema/infer-schema')
assert response.status_code == 422

def test_infer_schema_empty_file(self):
file = csv.get_empty_sample_csv_file()
response = self.client.post(
'/api/schema/infer-schema', files={'data_file': ('', file.file)}
)
assert response.status_code == 422
2 changes: 1 addition & 1 deletion api/tests/routes/metrics_route_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def setUpClass(cls):
app.add_exception_handler(MetricsError, metrics_exception_handler)
app.include_router(router, prefix=cls.prefix)

cls.client = TestClient(app)
cls.client = TestClient(app, raise_server_exceptions=False)

def test_get_reference_statistics_by_model_by_uuid(self):
model_uuid = uuid.uuid4()
Expand Down
2 changes: 1 addition & 1 deletion api/tests/routes/model_route_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def setUpClass(cls):
app.add_exception_handler(ModelError, model_exception_handler)
app.include_router(router, prefix=cls.prefix)

cls.client = TestClient(app)
cls.client = TestClient(app, raise_server_exceptions=False)

def test_create_model(self):
model_in = db_mock.get_sample_model_in()
Expand Down
2 changes: 1 addition & 1 deletion api/tests/routes/modelin_validation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestValidationModelRoute(unittest.TestCase):
def setUp(self):
self.model_service = MagicMock(spec_set=ModelService)
self.prefix = '/api/models'
self.client = TestClient(main.app)
self.client = TestClient(main.app, raise_server_exceptions=False)

def test_request_validation_exception_handler(self):
modelin_data = get_model_sample_wrong(
Expand Down
2 changes: 1 addition & 1 deletion api/tests/routes/upload_dataset_route_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setUpClass(cls):
app = FastAPI(title='Radicalbit Platform', debug=True)
app.include_router(router, prefix=cls.prefix)

cls.client = TestClient(app)
cls.client = TestClient(app, raise_server_exceptions=False)

def test_upload_reference(self):
file = csv.get_correct_sample_csv_file()
Expand Down
5 changes: 5 additions & 0 deletions api/tests/services/file_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def test_validate_file_error(self):
with pytest.raises(InvalidFileException):
self.files_service.validate_file(file, sep=',', columns=['a', 'b'])

def test_validate_empty_file_error(self):
file = csv.get_empty_sample_csv_file()
with pytest.raises(InvalidFileException):
self.files_service.validate_file(file, sep=',', columns=[])

def test_infer_schema_ok(self):
file = csv.get_correct_sample_csv_file()
schema = FileService.infer_schema(file)
Expand Down

0 comments on commit 1af295c

Please sign in to comment.