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