lycantropos / hypothesis_sqlalchemy

hypothesis strategies for generating SQLAlchemy objects
MIT License
28 stars 8 forks source link

Add max size to email strategy #29

Closed Mec-iS closed 4 years ago

Mec-iS commented 4 years ago

It should be possible to call strategies.emails(max_size=N) as database column for emails may have constraint much smaller than 255.

Currently I have worked it around with:

@defines_strategy_with_reusable_values
def emails(max_size: int = None,) -> SearchStrategy[str]:
    """A strategy for generating email addresses as unicode strings. The
    address format is specified in :rfc:`5322#section-3.4.1`. Values shrink
    towards shorter local-parts and host domains.

    This strategy is useful for generating "user data" for tests, as
    mishandling of email addresses is a common source of bugs.
    """
    from hypothesis.provisional import domains

    local_chars = string.ascii_letters + string.digits + "!#$%&'*+-/=^_`{|}~"
    local_part = text(local_chars, min_size=1, max_size=64)
    # TODO: include dot-atoms, quoted strings, escaped chars, etc in local part
    return builds("{}@{}".format, local_part, domains()).filter(
        lambda addr: len(addr) <= (max_size if max_size is not None else 254)
    )
lycantropos commented 4 years ago

Are you talking about hypothesis.strategies.emails? If yes -- it should be an improvement request to hypothesis repository since it is not a part of hypothesis_sqlalchemy API.

Mec-iS commented 4 years ago

right sorry my bad