def field_mappings():
    global __default_field_mappings

    if __default_field_mappings is None:
        # Sized fields are handled in _get_strategy_for_field()
        # URL fields are not yet handled
        __default_field_mappings = {
            dm.SmallIntegerField:
            st.integers(-32768, 32767),
            dm.IntegerField:
            st.integers(-2147483648, 2147483647),
            dm.BigIntegerField:
            st.integers(-9223372036854775808, 9223372036854775807),
            dm.PositiveIntegerField:
            st.integers(0, 2147483647),
            dm.PositiveSmallIntegerField:
            st.integers(0, 32767),
            dm.BinaryField:
            st.binary(),
            dm.BooleanField:
            st.booleans(),
            dm.DateField:
            st.dates(),
            dm.DateTimeField:
            st.datetimes(timezones=get_tz_strat()),
            dm.DurationField:
            st.timedeltas(),
            dm.EmailField:
            emails(),
            dm.FloatField:
            st.floats(),
            dm.NullBooleanField:
            st.one_of(st.none(), st.booleans()),
            dm.TimeField:
            st.times(timezones=get_tz_strat()),
            dm.UUIDField:
            st.uuids(),
        }

        # SQLite does not support timezone-aware times, or timedeltas that
        # don't fit in six bytes of microseconds, so we override those
        db = getattr(django_settings, 'DATABASES', {}).get('default', {})
        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
Example #2
0
def field_mappings():
    global __default_field_mappings

    if __default_field_mappings is None:
        # Sized fields are handled in _get_strategy_for_field()
        # URL fields are not yet handled
        __default_field_mappings = {
            dm.SmallIntegerField: st.integers(-32768, 32767),
            dm.IntegerField: st.integers(-2147483648, 2147483647),
            dm.BigIntegerField:
                st.integers(-9223372036854775808, 9223372036854775807),
            dm.PositiveIntegerField: st.integers(0, 2147483647),
            dm.PositiveSmallIntegerField: st.integers(0, 32767),
            dm.BinaryField: st.binary(),
            dm.BooleanField: st.booleans(),
            dm.DateField: st.dates(),
            dm.DateTimeField: st.datetimes(timezones=get_tz_strat()),
            dm.DurationField: st.timedeltas(),
            dm.EmailField: emails(),
            dm.FloatField: st.floats(),
            dm.NullBooleanField: st.one_of(st.none(), st.booleans()),
            dm.TimeField: st.times(timezones=get_tz_strat()),
            dm.UUIDField: st.uuids(),
        }

        # SQLite does not support timezone-aware times, or timedeltas that
        # don't fit in six bytes of microseconds, so we override those
        db = getattr(django_settings, 'DATABASES', {}).get('default', {})
        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
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
#
# END HEADER

from __future__ import division, print_function, absolute_import

from binascii import unhexlify

from hypothesis import given
from hypothesis.provisional import emails, ip4_addr_strings, \
    ip6_addr_strings


@given(emails())
def test_is_valid_email(address):
    local, at_, domain = address.rpartition('@')
    assert at_ == '@'
    assert local
    assert domain


@given(ip4_addr_strings())
def test_is_IP4_addr(address):
    as_num = [int(n) for n in address.split('.')]
    assert len(as_num) == 4
    assert all(0 <= n <= 255 for n in as_num)


@given(ip6_addr_strings())