Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TITLE_LENGTH_ERROR = "This title is too long, please make it 200 characters or less."
- TITLE_EMPTY_ERROR = "You’ll have to add a title."
- TEXT_EMPTY_ERROR = "Please enter some text."
- NO_CATEGORY_ERROR = "Please select a category."
- NO_CITY_ERROR = "Please select a city."
- class ArticleForm(ModelForm):
- text = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
- class Meta:
- model = Article
- fields = ['title', 'text', 'categories', 'city']
- widgets = {'title': forms.TextInput(attrs={
- 'placeholder': 'Enter a descriptive title'}),
- 'categories': forms.CheckboxSelectMultiple(choices=Category.CATEGORY_CHOICES),
- 'city': forms.RadioSelect(choices=City.CITY_CHOICES),
- }
- error_messages = {
- 'title': {
- 'max_length': TITLE_LENGTH_ERROR,
- 'required': TITLE_EMPTY_ERROR,
- },
- 'text': {
- 'required': TEXT_EMPTY_ERROR,
- },
- 'categories': {
- 'required': NO_CATEGORY_ERROR,
- },
- 'city': {
- 'required': NO_CITY_ERROR,
- }
- }
- from articles.models import Article, Category, City
- from articles.forms import (
- ArticleForm,
- TITLE_LENGTH_ERROR,
- TITLE_EMPTY_ERROR,
- TEXT_EMPTY_ERROR,
- NO_CATEGORY_ERROR,
- NO_CITY_ERROR,
- )
- class ArticleFormTest(TestCase):
- def setUp(self):
- self.user = User.objects.create(username='testuser')
- self.user.set_password('12345')
- self.user.save()
- self.client.login(username='testuser', password='12345')
- def test_form_validation_for_blank_inputs(self):
- form = ArticleForm(data={'title': '', 'text': '', 'categories': '', 'city': '', 'author': self.user})
- self.assertFalse(form.is_valid())
- self.assertEqual(
- form.errors['text'],
- [TEXT_EMPTY_ERROR]
- )
- (venv) Robins-MacBook-Pro:togethere robin$ python manage.py test articles/
- Creating test database for alias 'default'...
- .F....................
- ======================================================================
- FAIL: test_form_validation_for_blank_inputs (articles.tests.test_forms.ArticleFormTest)
- ----------------------------------------------------------------------
- Traceback (most recent call last):
- File "/Users/robin/work/2016-06-04_togethere/togethere/articles/tests/test_forms.py", line 36, in test_form_validation_for_blank_inputs
- [TEXT_EMPTY_ERROR]
- AssertionError: ['This field is required.'] != ['Please enter some text.']
- ----------------------------------------------------------------------
- Ran 22 tests in 4.171s
- FAILED (failures=1)
- Destroying test database for alias 'default'...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement