Beispiel #1
0
        if db.get('ENGINE', '').endswith('.sqlite3'):  # pragma: no branch
            sqlite_delta = timedelta(microseconds=2 ** 47 - 1)
            __default_field_mappings.update({
                dm.TimeField: st.times(),
                dm.DurationField: st.timedeltas(-sqlite_delta, sqlite_delta),
            })

    return __default_field_mappings


def add_default_field_mapping(field_type, strategy):
    # type: (Type[dm.Field], st.SearchStrategy[Any]) -> None
    field_mappings()[field_type] = strategy


default_value = DefaultValueType(u'default_value')


def validator_to_filter(f):
    # type: (Type[dm.Field]) -> Callable[[Any], bool]
    """Converts the field run_validators method to something suitable for use
    in filter."""
    def validate(value):
        try:
            f.run_validators(value)
            return True
        except ValidationError:
            return False

    return validate
Beispiel #2
0
if False:
    from typing import Any, Type, List, Text, Union  # noqa


def add_default_field_mapping(field_type, strategy):
    # type: (Type[dm.Field], st.SearchStrategy[Any]) -> None
    note_deprecation(
        "`hypothesis.extra.django.models.add_default_field_mapping` is deprecated; use `hypothesis.extra.django."
        "register_field_strategy` instead.",
        since="2019-01-10",
    )
    register_field_strategy(field_type, strategy)


default_value = DefaultValueType(u"default_value")


@st.defines_strategy
def models(
    model,  # type: Type[dm.Model]
    **field_strategies  # type: Union[st.SearchStrategy[Any], DefaultValueType]
):
    # type: (...) -> st.SearchStrategy[Any]
    """Return a strategy for examples of ``model``.

    .. warning::
        Hypothesis creates saved models. This will run inside your testing
        transaction when using the test runner, but if you use the dev console
        this will leave debris in your database.