예제 #1
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']

    basejanitor = BaseJanitor(fields, data)
    assert basejanitor.is_clean() is True

    class Janitor(BaseJanitor):
        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

    janitor = Janitor(fields, data)
    assert janitor.is_clean() is False
    assert janitor.errors == {
        'name': ['Name must be a string'],
    }
    assert 'name' not in janitor.cleaned_data
예제 #2
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': 'janitor'}
    fields = ['name']

    basejanitor = BaseJanitor(fields, data)
    assert basejanitor.is_clean() is True
    assert basejanitor.cleaned_data['name'] == 'janitor'

    class Janitor(BaseJanitor):
        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]

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

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

    janitor = Janitor(fields, data)
    assert janitor.is_clean() is True
    assert janitor.cleaned_data['name'] is None
예제 #3
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']

    basejanitor = BaseJanitor(fields, data)
    assert basejanitor.is_clean() is True

    class Janitor(BaseJanitor):
        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

    janitor = Janitor(fields, data)
    assert janitor.is_clean() is False
    assert janitor.errors == {
        'name': ['Name must be a string'],
    }
    assert 'name' not in janitor.cleaned_data
예제 #4
0
def test_build_or_update_fail():
    """
    Test build_or_update will fail when there are errors messages defined.
    """
    data = {'name': 4567}
    fields = ['name']

    basejanitor = BaseJanitor(fields, data)
    assert basejanitor.is_clean() is True

    class Janitor(BaseJanitor):
        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

    janitor = Janitor(fields, data)
    assert janitor.is_clean() is False

    with raises(ValueError):
        janitor.build_or_update(object, fields)
예제 #5
0
def test_types_for_data_fail():
    """
    Test objects you cannot use to provide data.
    """
    fields = ['name']
    for data in (
        ('janitor', ),
            namedtuple('tuple', 'name')(name='janitor'),
    ):
        with raises(TypeError):
            BaseJanitor(fields, data)
예제 #6
0
def test_types_for_field_list_fail():
    """
    Test objects you cannot use to provide field names.
    """
    data = {'name': 'janitor'}
    for fields in (
        {
            'field': 'name'
        },
        [('name', )],
            'name',
    ):
        with raises(TypeError):
            BaseJanitor(fields, data)
예제 #7
0
def test_types_for_field_list_ok():
    """
    Test objects you can use to provide field names.
    """
    data = {'name': 'janitor'}
    for fields in (
        ['name'],
        ('name', ),
            set(['name']),
            namedtuple('tuple', 'fields')(fields='name'),
    ):
        try:
            janitor = BaseJanitor(fields, data)
        except TypeError as e:
            fail(str(e))

        assert 'name' in janitor.fields
예제 #8
0
def test_types_for_data_ok():
    """
    Test objects you can use to provide data.
    """
    fields = ['name']
    for data in (
        {
            'name': 'janitor'
        },
            namedtuple('tuple', 'name')(name='janitor')._asdict(),
    ):
        try:
            janitor = BaseJanitor(fields, data)
        except TypeError as e:
            fail(str(e))

        assert janitor.raw_data['name'] == 'janitor'