Search code examples
pythonhypothesis-testpython-hypothesis

Hypothesis.strategies generate string from date


I am using hypothesis to test my application and generate random input data for endpoints. Here is my code:

def generate_upload_data():
    today = datetime.date.today()
    start_date = today - relativedelta(months=1)
    return hypothesis.strategies.builds(
        SomeModelClass,
        date=hypothesis.strategies.dates(
            min_value=start_date, max_value=today
        ),
    )

This generates date as datetime.date object, but I need it in string format (01.01.2020). So I need to convert it like

random_date.strftime("%d.%m.%Y") 

But I can't find any way to do that. Is it possible to generate string from date in hypothesis?


Solution

  • See the docs on adapting strategies. As Azat Ibrakov notes above, you can easily convert dates to strings using

    hypothesis.strategies.dates(...).map(lambda date: date.strftime("%d.%m.%Y"))