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

fix: add fake() support to Literal field type #451

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dataclasses_avroschema/fields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ def default_to_avro(self, default: typing.Any):
def validate_default(self, default: typing.Any) -> bool:
return self.avro_field.validate_default(default) # type: ignore

def fake(self) -> typing.Any:
return random.choice(get_args(self.type))


@dataclasses.dataclass
class FixedField(BytesField):
Expand Down
1 change: 1 addition & 0 deletions tests/schemas/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ class User(AvroBaseModel):
favorite_colors: color_enum = color_enum.BLUE
country: str = "Argentina"
address: typing.Optional[Address] = None
role: typing.Literal["admin", "user"] = "user"

# just calling fake is enougt to know that a proper instance was created,
# otherwise a pydantic validation should have been raised
Expand Down
Loading