test_fields.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Tests for the utils/fields.py file."""
  2. import pytest
  3. from wtforms.form import Form
  4. from flaskbb.utils.fields import SelectBirthdayWidget, BirthdayField
  5. def test_birthday_field():
  6. class F(Form):
  7. birthday = BirthdayField(format='%d %m %Y')
  8. a = ["04 02 2015"]
  9. b = ["None", "None", "2015"] # this one should fail
  10. c = ["None", "None", "None"]
  11. form = F()
  12. assert form.birthday.process_formdata(a) is None
  13. assert form.birthday.process_formdata(c) is None
  14. with pytest.raises(ValueError):
  15. form.birthday.process_formdata(b)
  16. def test_select_birthday_widget():
  17. """Test the SelectDateWidget."""
  18. assert SelectBirthdayWidget.FORMAT_CHOICES['%d'] == [
  19. (x, str(x)) for x in range(1, 32)
  20. ]
  21. assert SelectBirthdayWidget.FORMAT_CHOICES['%m'] == [
  22. (x, str(x)) for x in range(1, 13)
  23. ]
  24. assert SelectBirthdayWidget.FORMAT_CLASSES == {
  25. '%d': 'select_date_day',
  26. '%m': 'select_date_month',
  27. '%Y': 'select_date_year'
  28. }
  29. select_birthday_widget = SelectBirthdayWidget(years=[0, 1])
  30. assert select_birthday_widget.FORMAT_CHOICES['%Y'] == [(0, '0'), (1, '1')]
  31. class Field(object):
  32. id = 'world'
  33. name = 'helloWorld'
  34. format = '%d %m %Y'
  35. data = None
  36. html = select_birthday_widget(field=Field(), surrounded_div="test-div")
  37. assert 'world' in html
  38. assert 'helloWorld' in html
  39. assert 'class="select_date_day"' in html
  40. assert 'class="select_date_month"' in html
  41. assert 'class="select_date_year"' in html
  42. assert '<div class="test-div">' in html