Пример #1
0
    def test_common_string_kwargs(self, validators):
        """
        Test setting common string arguments.

        Tests :py:meth:`.DocumentFieldConverter.set_common_string_kwargs`.
        """
        validators.Length.return_value = 'length-validator'
        validators.Regexp.return_value = 'regexp-validator'

        class DocumentFieldMock(object):
            max_length = 10
            min_length = 5
            regex = 'my-regex'

        kwargs = {'foo': 'bar', 'validators': ['test']}

        converter = DocumentFieldConverter(Mock())
        converter.set_common_string_kwargs(DocumentFieldMock(), kwargs)

        self.assertEqual({
            'foo': 'bar',
            'validators': ['test', 'length-validator', 'regexp-validator'],
        }, kwargs)
        validators.Length.assert_called_once_with(max=10, min=5)
        validators.Regexp.assert_called_once_with(regex='my-regex')
Пример #2
0
    def test_convert_choices(self, fields):
        """
        Test ``convert`` with choices.

        Tests :py:meth:`.DocumentFieldConverter.convert`.
        """
        fields.SelectField.return_value = 'select-field'

        class DocumentFieldMock(object):
            verbose_name = 'test field'
            required = False
            default = 'empty'
            choices = [('a', 'Choice A'), ('b', 'Choice B')]
            help_text = 'Make your choice'

        converter = DocumentFieldConverter(Mock())

        result = converter.convert(DocumentFieldMock())

        fields.SelectField.assert_called_once_with(
            label='test field',
            validators=[],
            default='empty',
            choices=[('a', 'Choice A'), ('b', 'Choice B')],
            description='Make your choice'
        )

        self.assertEqual('select-field', result)
Пример #3
0
    def test___new__(self, DocumentFieldConverter):
        """
        Test that ``__new__`` is creating the new class properly.
        """
        converter = Mock()
        converter.fields = {
            'field_a': 'a-value',
            'field_b': 'b-value'
        }
        DocumentFieldConverter.return_value = converter

        class TestClass(object):
            __metaclass__ = DocumentFormMetaClassBase

            class Meta:
                fields = ('title', 'body',)
                exclude = ('author', 'timestamp',)
                document_class = 'a-document'

        DocumentFieldConverter.assert_called_once_with(
            'a-document',
            ('title', 'body',),
            ('author', 'timestamp',),
        )
        self.assertEqual('a-value', TestClass.field_a)
        self.assertEqual('b-value', TestClass.field_b)
Пример #4
0
    def test_convert(self, validators):
        """
        Test ``convert`` without choices.

        Tests :py:meth:`.DocumentFieldConverter.convert`.
        """
        validators.Required.return_value = 'required'

        class DocumentFieldMock(object):
            verbose_name = 'test field'
            required = True
            default = 'empty'
            choices = None
            help_text = 'This is a field for testing purpose'

        document_field = DocumentFieldMock()

        converter = DocumentFieldConverter(Mock())
        converter.from_documentfieldmock = Mock(return_value='wtfield')

        result = converter.convert(document_field)

        converter.from_documentfieldmock.assert_called_once_with(
            document_field,
            label='test field',
            validators=['required'],
            default='empty',
            description='This is a field for testing purpose'
        )

        self.assertEqual('wtfield', result)
Пример #5
0
    def test_from_objectidfield(self):
        """
        Test :py:meth:`.DocumentFieldConverter.from_objectidfield`.

        This tests that this method returns ``None``.

        """
        converter = DocumentFieldConverter(Mock())
        self.assertEqual(None, converter.from_objectidfield(Mock()))
Пример #6
0
    def test_from_datetimefield(self, fields):
        """
        Test :py:meth:`.DocumentFieldConverter.from_datetimefield`.
        """
        fields.DateTimeField.return_value = 'datetime-field'
        document_field = Mock()

        converter = DocumentFieldConverter(Mock())
        result = converter.from_datetimefield(document_field, validators=[])

        fields.DateTimeField.assert_called_once_with(validators=[])
        self.assertEqual('datetime-field', result)
Пример #7
0
    def test_fields(self):
        """
        Test :py:meth:`.DocumentFieldConverter.fields`.
        """
        converter = DocumentFieldConverter(self.document_class)
        converter.convert = self.convert

        self.assertEqual({
            'title': 'title-value',
            'body': 'body-value',
            'author': 'author-value',
            'timestamp': 'timestamp-value',
        }, converter.fields)
Пример #8
0
    def test_fields_only_fields(self):
        """
        Test :py:meth:`.DocumentFieldConverter.fields` with only fields.
        """
        converter = DocumentFieldConverter(
            self.document_class,
            fields=['title', 'author']
        )
        converter.convert = self.convert

        self.assertEqual({
            'title': 'title-value',
            'author': 'author-value',
        }, converter.fields)
Пример #9
0
    def test_fields_exclude_fields(self):
        """
        Test :py:meth:`.DocumentFieldConverter.fields` with excluded fields.
        """
        converter = DocumentFieldConverter(
            self.document_class,
            exclude=['body', 'author']
        )
        converter.convert = self.convert

        self.assertEqual({
            'title': 'title-value',
            'timestamp': 'timestamp-value',
        }, converter.fields)
Пример #10
0
    def test_from_decimalfield(self, fields):
        """
        Test :py:meth:`.DocumentFieldConverter.from_decimalfield`.
        """
        fields.DecimalField.return_value = 'decimal-field'
        document_field = Mock()

        converter = DocumentFieldConverter(Mock())
        converter.set_common_number_kwargs = Mock()
        result = converter.from_decimalfield(document_field, validators=[])

        converter.set_common_number_kwargs.assert_called_once_with(
            document_field, {'validators': []})
        fields.DecimalField.assert_called_once_with(validators=[])
        self.assertEqual('decimal-field', result)
Пример #11
0
    def test_from_stringfield(self, fields):
        """
        Test :py:meth:`.DocumentFieldConverter.from_stringfield`.
        """
        fields.TextField.return_value = 'text-field'
        document_field = Mock()

        converter = DocumentFieldConverter(Mock())
        converter.set_common_string_kwargs = Mock()
        result = converter.from_stringfield(document_field, foo='bar')

        converter.set_common_string_kwargs.assert_called_once_with(
            document_field, {'foo': 'bar'})
        fields.TextField.assert_called_once_with(foo='bar')
        self.assertEqual('text-field', result)
Пример #12
0
    def test_common_string_kwargs_min_length_min_one(self, validators):
        """
        Test that when there is no ``min_length``, it is set to ``-1``.

        Tests :py:meth:`.DocumentFieldConverter.set_common_string_kwargs`.
        """
        document_field = Mock()
        document_field.max_length = 10
        document_field.min_length = None

        kwargs = {'validators': []}

        converter = DocumentFieldConverter(Mock())
        converter.set_common_string_kwargs(document_field, kwargs)

        validators.Length.assert_called_once_with(max=10, min=-1)
Пример #13
0
    def test_from_urlfield(self, fields, validators):
        """
        Test :py:meth:`.DocumentFieldConverter.from_urlfield`.
        """
        validators.URL.return_value = 'url-validator'

        fields.TextField.return_value = 'text-field'
        document_field = Mock()

        converter = DocumentFieldConverter(Mock())
        converter.set_common_string_kwargs = Mock()
        result = converter.from_urlfield(document_field, validators=[])

        converter.set_common_string_kwargs.assert_called_once_with(
            document_field, {'validators': ['url-validator']})
        fields.TextField.assert_called_once_with(validators=['url-validator'])
        self.assertEqual('text-field', result)
Пример #14
0
    def test_common_number_kwargs_no_max(self, validators):
        """
        Test :py:meth:`.DocumentFieldConverter.set_common_number_kwargs`.
        """
        validators.NumberRange.return_value = 'number-range-validator'

        document_field = Mock()
        document_field.max_value = 20
        document_field.min_value = 10

        kwargs = {'validators': []}

        converter = DocumentFieldConverter(Mock(Mock()))
        converter.set_common_number_kwargs(document_field, kwargs)

        validators.NumberRange.assert_called_once_with(max=20, min=10)
        self.assertEqual({'validators': ['number-range-validator']}, kwargs)
Пример #15
0
    def test_convert_return_none(self):
        """
        Test the situation where ``convert`` returns ``None``.

        This happens when the field is not convertable to a WTForms field.

        Tests :py:meth:`.DocumentFieldConverter.convert`.
        """
        class DocumentFieldMock(object):
            verbose_name = 'test field'
            required = False
            default = ''
            choices = []
            help_text = ''

        converter = DocumentFieldConverter(Mock())
        result = converter.convert(DocumentFieldMock())
        self.assertEqual(None, result)