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)
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)
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)
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)
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)
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)