Add tests for form data validation and optional fields#14727
Add tests for form data validation and optional fields#14727Vivansh27 wants to merge 2 commits intofastapi:masterfrom
Conversation
This PR adds unit tests for form data handling in the application: 1. `test_list_field_single_value`: Ensures that a single value in a list field is correctly wrapped as a list. 2. `test_alias_field_name_not_accepted`: Ensures that fields with unrecognized aliases return a 422 validation error. 3. `test_optional_int_empty_string`: Ensures that empty strings for optional integer fields are correctly converted to None. These tests improve coverage for form input validation and ensure consistent API behavior.
YuriiMotov
left a comment
There was a problem hiding this comment.
@Vivansh27, thanks for your interest and efforts!
Please, take a look at my comments.
I suggest we close this PR.
I will make sure we add tests for the case with sending empty string to optional field - there is actually an issue with it and we are going to fix it. So, we will add such tests in that PR
| def test_alias_field_name_not_accepted(): | ||
| response = client.post( | ||
| "/form/", | ||
| data={ | ||
| "username": "Rick", | ||
| "lastname": "Sanchez", | ||
| "alias_with": "something", | ||
| }, | ||
| ) | ||
| assert response.status_code == 422 |
There was a problem hiding this comment.
We already have such test:
fastapi/tests/test_request_params/test_form/test_required_str.py
Lines 139 to 156 in 0c7f2b6
| def test_optional_int_empty_string(): | ||
| response = client.post( | ||
| "/form/", | ||
| data={ | ||
| "username": "Rick", | ||
| "lastname": "Sanchez", | ||
| "age": "", | ||
| }, | ||
| ) | ||
| assert response.status_code == 200, response.text | ||
| assert response.json()["age"] is None |
There was a problem hiding this comment.
I think we should add such tests, but we should do it in more consistent way (different type of field, cover form declared as Pydantic model, different default values (not only None)).
I have a plan to add such set of tests
| def test_list_field_single_value(): | ||
| response = client.post( | ||
| "/form/", | ||
| data={ | ||
| "username": "Rick", | ||
| "lastname": "Sanchez", | ||
| "tags": "single", | ||
| }, | ||
| ) | ||
| assert response.status_code == 200 | ||
| assert response.json()["tags"] == ["single"] |
There was a problem hiding this comment.
Something tells me that if we search in the code base we will find out that such test already exists..
I think we don't need to add this, but if we decide to add it we should do it in more consistent way (for different types (source) of parameters (e.g. Form, Query, Cookie, etc..))
This PR adds unit tests for form data handling in the application:
test_list_field_single_value: Ensures that a single value in a list field is correctly wrapped as a list.test_alias_field_name_not_accepted: Ensures that fields with unrecognized aliases return a 422 validation error.test_optional_int_empty_string: Ensures that empty strings for optional integer fields are correctly converted to None.These tests improve coverage for form input validation and ensure consistent API behavior.