Skip to content

Commit 79a895f

Browse files
author
Henry Walshaw
committed
Add failing test to demonstrate issue
From the [docs](https://docs.pydantic.dev/latest/concepts/fields/#exclude): > The `exclude` parameter can be used to control which fields should > be excluded from the model when exporting the model. At the moment the values are excluded as expected, but the headers are not correctly filtered. This is worse than we expect because it also breaks the order of the fields rather than leaving the field blank.
1 parent 3bf18f1 commit 79a895f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

tests/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ def parse_start_date(cls, value):
5151
@pydantic.field_validator("end", mode="before")
5252
def parse_end_date(cls, value):
5353
return datetime.strptime(value, "%d.%m.%Y").date()
54+
55+
56+
class ExcludedPassword(pydantic.BaseModel):
57+
username: str = "Wagstaff"
58+
password: str = Field(default="swordfish", exclude=True)

tests/test_basemodel_csv_writer.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pydantic_csv import BasemodelCSVWriter
66

7-
from .models import NonBaseModelUser, SimpleUser, User
7+
from .models import ExcludedPassword, NonBaseModelUser, SimpleUser, User
88

99

1010
def test_create_csv_file(users_as_csv_buffer, users_from_csv):
@@ -50,3 +50,13 @@ def test_with_wrong_type_in_list(user_list):
5050

5151
def test_header_mapping(users_mapped_as_csv_buffer, users_mapped_from_csv):
5252
assert users_mapped_as_csv_buffer == users_mapped_from_csv
53+
54+
55+
def test_excluded_field():
56+
output = io.StringIO()
57+
user = ExcludedPassword()
58+
59+
w = BasemodelCSVWriter(output, [user], ExcludedPassword)
60+
w.write()
61+
62+
assert output.getvalue() == "username\r\nWagstaff\r\n"

0 commit comments

Comments
 (0)