Ejemplo n.º 1
0
def test_build_or_update_fail():
    """
    Test build_or_update will fail when there are errors messages defined.
    """
    data = {'name': 4567}
    fields = ['name']

    baseswamper = BaseSwamper(fields, data)
    assert baseswamper.is_clean() is True

    class Swamper(BaseSwamper):
        def clean_name(self, value, is_blank):
            """
            Raise an exception to validate this error is added for this field
            and removed from cleaned data.
            """
            if not isinstance(value, six.string_types):
                raise self.error_class('Name must be a string')

            return value

    swamper = Swamper(fields, data)
    assert swamper.is_clean() is False

    with raises(ValueError):
        swamper.build_or_update(object, fields)
Ejemplo n.º 2
0
def test_clean_add_error():
    """
    Test adding an error for a field inside the global clean method will remove
    that field from cleaned data and add an error message for this field.
    """
    data = {'name': 4567}
    fields = ['name']

    baseswamper = BaseSwamper(fields, data)
    assert baseswamper.is_clean() is True

    class Swamper(BaseSwamper):
        def clean(self):
            """
            Add errors for a field can be done in the global clean.
            """
            name = self.cleaned_data['name']
            if not isinstance(name, six.string_types):
                self.add_error('name', 'Name must be a string')

            return self.cleaned_data

    swamper = Swamper(fields, data)
    assert swamper.is_clean() is False
    assert swamper.errors == {
        'name': ['Name must be a string'],
    }
    assert 'name' not in swamper.cleaned_data
Ejemplo n.º 3
0
def test_clean_field_ok_return_value():
    """
    Test field clean methods are called and its output is used to replace the
    value for field in cleaned data and if nothing is return the value is
    also replacing with None ('nothing').
    """
    data = {'name': 'swamper'}
    fields = ['name']

    baseswamper = BaseSwamper(fields, data)
    assert baseswamper.is_clean() is True
    assert baseswamper.cleaned_data['name'] == 'swamper'

    class Swamper(BaseSwamper):
        def clean_name(self, value, is_blank):
            """
            Deliberately change the value for this field for the purpose of
            testing that this method has been called.
            """
            return [value]

    swamper = Swamper(fields, data)
    assert swamper.is_clean() is True
    assert swamper.cleaned_data['name'] == ['swamper']

    class Swamper(BaseSwamper):
        def clean_name(self, value, is_blank):
            """
            Modifying cleaned_data instead of returning does not work.
            """
            self.cleaned_data['name'] = 'rotinaj'

    swamper = Swamper(fields, data)
    assert swamper.is_clean() is True
    assert swamper.cleaned_data['name'] is None
Ejemplo n.º 4
0
def test_clean_field_raise_error():
    """
    Test raising an error inside a field's clean method will remove it from
    cleaned data and add an error message for this field.
    """
    data = {'name': 4567}
    fields = ['name']

    baseswamper = BaseSwamper(fields, data)
    assert baseswamper.is_clean() is True

    class Swamper(BaseSwamper):
        def clean_name(self, value, is_blank):
            """
            Raise an exception to validate this error is added for this field
            and removed from cleaned data.
            """
            if not isinstance(value, six.string_types):
                raise self.error_class('Name must be a string')

            return value

    swamper = Swamper(fields, data)
    assert swamper.is_clean() is False
    assert swamper.errors == {
        'name': ['Name must be a string'],
    }
    assert 'name' not in swamper.cleaned_data
Ejemplo n.º 5
0
def test_types_for_data_fail():
    """
    Test objects you cannot use to provide data.
    """
    fields = ['name']
    for data in (
        ('swamper',),
        namedtuple('tuple', 'name')(name='swamper'),
    ):
        with raises(TypeError):
            BaseSwamper(fields, data)
Ejemplo n.º 6
0
def test_types_for_field_list_fail():
    """
    Test objects you cannot use to provide field names.
    """
    data = {'name': 'swamper'}
    for fields in (
        {'field': 'name'},
        [('name',)],
        'name',
    ):
        with raises(TypeError):
            BaseSwamper(fields, data)
Ejemplo n.º 7
0
def test_types_for_data_ok():
    """
    Test objects you can use to provide data.
    """
    fields = ['name']
    for data in (
        {'name': 'swamper'},
        namedtuple('tuple', 'name')(name='swamper')._asdict(),
    ):
        try:
            swamper = BaseSwamper(fields, data)
        except TypeError as e:
            fail(str(e))

        assert swamper.raw_data['name'] == 'swamper'
Ejemplo n.º 8
0
def test_types_for_field_list_ok():
    """
    Test objects you can use to provide field names.
    """
    data = {'name': 'swamper'}
    for fields in (
        ['name'],
        ('name',),
        set(['name']),
        namedtuple('tuple', 'fields')(fields='name'),
    ):
        try:
            swamper = BaseSwamper(fields, data)
        except TypeError as e:
            fail(str(e))

        assert 'name' in swamper.fields