Skip to content

Add tests for form data validation and optional fields#14727

Open
Vivansh27 wants to merge 2 commits intofastapi:masterfrom
Vivansh27:patch-1
Open

Add tests for form data validation and optional fields#14727
Vivansh27 wants to merge 2 commits intofastapi:masterfrom
Vivansh27:patch-1

Conversation

@Vivansh27
Copy link

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.

Vivansh27 and others added 2 commits January 18, 2026 01:04
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.
@Vivansh27 Vivansh27 closed this Jan 17, 2026
@Vivansh27 Vivansh27 deleted the patch-1 branch January 17, 2026 19:45
@Vivansh27 Vivansh27 restored the patch-1 branch January 18, 2026 13:08
@Vivansh27 Vivansh27 reopened this Jan 18, 2026
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Comment on lines +157 to +166
def test_alias_field_name_not_accepted():
response = client.post(
"/form/",
data={
"username": "Rick",
"lastname": "Sanchez",
"alias_with": "something",
},
)
assert response.status_code == 422
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have such test:

@pytest.mark.parametrize(
"path",
["/required-alias", "/model-required-alias"],
)
def test_required_alias_by_name(path: str):
client = TestClient(app)
response = client.post(path, data={"p": "hello"})
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"type": "missing",
"loc": ["body", "p_alias"],
"msg": "Field required",
"input": IsOneOf(None, {"p": "hello"}),
}
]
}

Comment on lines +168 to +178
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +144 to +154
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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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..))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants