Skip to content

Commit

Permalink
Add future and past date hypothesis strategies (#5850)
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers committed Jun 1, 2023
1 parent ca10302 commit 0813a03
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/5850-bschoenmaeckers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add future and past date hypothesis strategies.
5 changes: 5 additions & 0 deletions pydantic/_hypothesis_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ def add_luhn_digit(card_number: str) -> str:
st.register_type_strategy(pydantic.StrictStr, st.text())


# FutureDate, PastDate
st.register_type_strategy(pydantic.FutureDate, st.dates(min_value=datetime.date.today() + datetime.timedelta(days=1)))
st.register_type_strategy(pydantic.PastDate, st.dates(max_value=datetime.date.today() - datetime.timedelta(days=1)))


# Constrained-type resolver functions
#
# For these ones, we actually want to inspect the type in order to work out a
Expand Down
2 changes: 2 additions & 0 deletions tests/test_hypothesis_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class ConstrainedNumbersModel(pydantic.BaseModel):
class ConstrainedDateModel(pydantic.BaseModel):
condatet: pydantic.condate(gt=date(1980, 1, 1), lt=date(2180, 12, 31))
condatee: pydantic.condate(ge=date(1980, 1, 1), le=date(2180, 12, 31))
future: pydantic.FutureDate
past: pydantic.PastDate

yield from (
MiscModel,
Expand Down

0 comments on commit 0813a03

Please sign in to comment.