Advertisement
Guest User

Untitled

a guest
Sep 7th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. TITLE_LENGTH_ERROR = "This title is too long, please make it 200 characters or less."
  2. TITLE_EMPTY_ERROR = "You’ll have to add a title."
  3. TEXT_EMPTY_ERROR = "Please enter some text."
  4. NO_CATEGORY_ERROR = "Please select a category."
  5. NO_CITY_ERROR = "Please select a city."
  6.  
  7.  
  8. class ArticleForm(ModelForm):
  9. text = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
  10. class Meta:
  11. model = Article
  12. fields = ['title', 'text', 'categories', 'city']
  13. widgets = {'title': forms.TextInput(attrs={
  14. 'placeholder': 'Enter a descriptive title'}),
  15. 'categories': forms.CheckboxSelectMultiple(choices=Category.CATEGORY_CHOICES),
  16. 'city': forms.RadioSelect(choices=City.CITY_CHOICES),
  17. }
  18. error_messages = {
  19. 'title': {
  20. 'max_length': TITLE_LENGTH_ERROR,
  21. 'required': TITLE_EMPTY_ERROR,
  22. },
  23. 'text': {
  24. 'required': TEXT_EMPTY_ERROR,
  25. },
  26. 'categories': {
  27. 'required': NO_CATEGORY_ERROR,
  28. },
  29. 'city': {
  30. 'required': NO_CITY_ERROR,
  31. }
  32. }
  33.  
  34. from articles.models import Article, Category, City
  35. from articles.forms import (
  36. ArticleForm,
  37. TITLE_LENGTH_ERROR,
  38. TITLE_EMPTY_ERROR,
  39. TEXT_EMPTY_ERROR,
  40. NO_CATEGORY_ERROR,
  41. NO_CITY_ERROR,
  42. )
  43.  
  44.  
  45. class ArticleFormTest(TestCase):
  46.  
  47. def setUp(self):
  48. self.user = User.objects.create(username='testuser')
  49. self.user.set_password('12345')
  50. self.user.save()
  51. self.client.login(username='testuser', password='12345')
  52.  
  53. def test_form_validation_for_blank_inputs(self):
  54. form = ArticleForm(data={'title': '', 'text': '', 'categories': '', 'city': '', 'author': self.user})
  55. self.assertFalse(form.is_valid())
  56.  
  57. self.assertEqual(
  58. form.errors['text'],
  59. [TEXT_EMPTY_ERROR]
  60. )
  61.  
  62. (venv) Robins-MacBook-Pro:togethere robin$ python manage.py test articles/
  63. Creating test database for alias 'default'...
  64. .F....................
  65. ======================================================================
  66. FAIL: test_form_validation_for_blank_inputs (articles.tests.test_forms.ArticleFormTest)
  67. ----------------------------------------------------------------------
  68. Traceback (most recent call last):
  69. File "/Users/robin/work/2016-06-04_togethere/togethere/articles/tests/test_forms.py", line 36, in test_form_validation_for_blank_inputs
  70. [TEXT_EMPTY_ERROR]
  71. AssertionError: ['This field is required.'] != ['Please enter some text.']
  72.  
  73. ----------------------------------------------------------------------
  74. Ran 22 tests in 4.171s
  75.  
  76. FAILED (failures=1)
  77. Destroying test database for alias 'default'...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement