Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix:API] Fix DatetimeFieldOverflow error handling #3343

12 changes: 11 additions & 1 deletion mathesar/api/db/viewsets/records.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from psycopg2.errors import ForeignKeyViolation, InvalidDatetimeFormat
from psycopg2.errors import ForeignKeyViolation, InvalidDatetimeFormat, DatetimeFieldOverflow
from rest_access_policy import AccessViewSetMixin
from rest_framework import status, viewsets
from rest_framework.exceptions import NotFound, MethodNotAllowed
Expand Down Expand Up @@ -108,6 +108,16 @@ def list(self, request, table_pk=None):
e,
status_code=status.HTTP_400_BAD_REQUEST,
)
elif isinstance(e.orig, DatetimeFieldOverflow):
raise database_api_exceptions.InvalidDateAPIException(
e,
status_code=status.HTTP_400_BAD_REQUEST,
)
else:
raise database_api_exceptions.MathesarAPIException(
e,
status_code=status.HTTP_400_BAD_REQUEST
)

serializer = RecordSerializer(
records,
Expand Down
45 changes: 45 additions & 0 deletions mathesar/tests/api/test_record_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,51 @@ def test_record_search(create_patents_table, client):
assert len(response_data['results']) == 1


def test_record_search_invalid_date(create_patents_table, client):
table_name = 'NASA Record Invalid Date'
table = create_patents_table(table_name)
columns_name_id_map = table.get_column_name_id_bidirectional_map()
column_id_with_date_type = table.get_column_name_id_bidirectional_map()['Patent Expiration Date']
column_attnum = table.columns.get(id=column_id_with_date_type).attnum
table.alter_column(column_attnum, {'type': 'date'})
search_columns = [
{'field': columns_name_id_map['Patent Expiration Date'], 'literal': '99/99/9999'},
]

json_search_fuzzy = json.dumps(search_columns)

response = client.get(
f'/api/db/v0/tables/{table.id}/records/?search_fuzzy={json_search_fuzzy}'
)
response_data = response.json()
assert response.status_code == 400
assert response_data[0]['code'] == ErrorCodes.InvalidDateError.value
assert response_data[0]['message'] == 'Invalid date'


def test_record_search_invalid_date_format(create_patents_table, client):
table_name = 'NASA Record Invalid Date Format'
table = create_patents_table(table_name)
columns_name_id_map = table.get_column_name_id_bidirectional_map()
column_id_with_date_type = table.get_column_name_id_bidirectional_map()['Patent Expiration Date']
column_attnum = table.columns.get(id=column_id_with_date_type).attnum
table.alter_column(column_attnum, {'type': 'date'})
search_columns = [
{'field': columns_name_id_map['Patent Expiration Date'], 'literal': '12/31'},
]

json_search_fuzzy = json.dumps(search_columns)

response = client.get(
f'/api/db/v0/tables/{table.id}/records/?search_fuzzy={json_search_fuzzy}'
)
response_data = response.json()

assert response.status_code == 400
assert response_data[0]['code'] == ErrorCodes.InvalidDateFormatError.value
assert response_data[0]['message'] == 'Invalid date format'


grouping_params = [
(
'NASA Record List Group Single',
Expand Down