You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_widget.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. from django.test import TestCase
  4. from oscar.forms import widgets
  5. class ImageInputTestCase(TestCase):
  6. def test_unbound_context(self):
  7. i = widgets.ImageInput()
  8. ctx = i.get_context('test_input', None, {'id': 'test-image-id'})
  9. self.assertEqual(ctx['image_id'], 'test-image-id-image')
  10. self.assertEqual(ctx['image_url'], '')
  11. self.assertEqual(
  12. ctx['widget']['attrs'],
  13. {'accept': 'image/*', 'id': 'test-image-id'}
  14. )
  15. def test_bound_context(self):
  16. i = widgets.ImageInput()
  17. ctx = i.get_context('test_input', '/dummy-image-value', {'id': 'test-image-id'})
  18. self.assertEqual(ctx['image_url'], '/dummy-image-value')
  19. class TimePickerInputTestCase(TestCase):
  20. def test_div_attrs_context(self):
  21. i = widgets.TimePickerInput(format='%H:%M')
  22. ctx = i.get_context('test_input', None, {})
  23. self.assertEqual(ctx['div_attrs'], {
  24. 'data-oscarWidget': 'time',
  25. 'data-timeFormat': 'hh:ii',
  26. })
  27. def test_icon_classes_context(self):
  28. i = widgets.TimePickerInput(format='%H:%M')
  29. ctx = i.get_context('test_input', None, {})
  30. self.assertEqual(ctx['icon_classes'], 'icon-time glyphicon-time')
  31. def test_input_format_unicode(self):
  32. # Check that the widget can handle unicode formats
  33. i = widgets.TimePickerInput(format=u'τ-%H:%M')
  34. time = datetime.time(10, 47)
  35. html = i.render('time', time)
  36. self.assertIn(u'value="τ-10:47"', html)
  37. class DatePickerInputTestCase(TestCase):
  38. def test_div_attrs_context(self):
  39. i = widgets.DatePickerInput(format='%d/%m/%Y')
  40. ctx = i.get_context('test_input', None, {})
  41. self.assertEqual(ctx['div_attrs'], {
  42. 'data-oscarWidget': 'date',
  43. 'data-dateFormat': 'dd/mm/yyyy',
  44. })
  45. def test_icon_classes_context(self):
  46. i = widgets.DatePickerInput(format='%H:%M')
  47. ctx = i.get_context('test_input', None, {})
  48. self.assertEqual(ctx['icon_classes'], 'icon-calendar glyphicon-calendar')
  49. def test_datepickerinput_format_unicode(self):
  50. # Check that the widget can handle unicode formats
  51. i = widgets.DatePickerInput(format=u'δ-%d/%m/%Y')
  52. date = datetime.date(2017, 5, 1)
  53. html = i.render('date', date)
  54. self.assertIn(u'value="δ-01/05/2017"', html)
  55. class DateTimePickerInputTestCase(TestCase):
  56. def test_div_attrs_context(self):
  57. i = widgets.DateTimePickerInput(format='%d/%m/%Y %H:%M')
  58. ctx = i.get_context('test_input', None, {})
  59. self.assertEqual(ctx['div_attrs'], {
  60. 'data-oscarWidget': 'datetime',
  61. 'data-datetimeFormat': 'dd/mm/yyyy hh:ii',
  62. })
  63. def test_icon_classes_context(self):
  64. i = widgets.DateTimePickerInput(format='%d/%m/%Y %H:%M')
  65. ctx = i.get_context('test_input', None, {})
  66. self.assertEqual(ctx['icon_classes'], 'icon-calendar glyphicon-calendar')
  67. def test_datetimepickerinput_format_unicode(self):
  68. # Check that the widget can handle unicode formats
  69. i = widgets.DateTimePickerInput(format=u'δ-%d/%m/%Y %H:%M')
  70. date = datetime.datetime(2017, 5, 1, 10, 57)
  71. html = i.render('datetime', date)
  72. self.assertIn(u'value="δ-01/05/2017 10:57"', html)
  73. class TestWidgetsDatetimeFormat(TestCase):
  74. def test_datetime_to_date_format_conversion(self):
  75. format_testcases = (
  76. ('%Y-%m-%d', 'yyyy-mm-dd'),
  77. ('%Y-%m-%d %H:%M', 'yyyy-mm-dd'),
  78. )
  79. for format_, expected in format_testcases:
  80. self.assertEqual(widgets.datetime_format_to_js_date_format(format_), expected)
  81. def test_datetime_to_time_format_conversion(self):
  82. format_testcases = (
  83. ('%Y-%m-%d %H:%M', 'hh:ii'),
  84. ('%H:%M', 'hh:ii'),
  85. )
  86. for format_, expected in format_testcases:
  87. self.assertEqual(widgets.datetime_format_to_js_time_format(format_), expected)
  88. class AdvancedSelectWidgetTestCase(TestCase):
  89. def test_widget_disabled_options(self):
  90. choices = (
  91. ('red', 'Red'),
  92. ('blue', 'Blue'),
  93. ('green', 'Green'),
  94. )
  95. disabled_values = ('red', 'green')
  96. i = widgets.AdvancedSelect(choices=choices, disabled_values=disabled_values)
  97. html = i.render('advselect', [])
  98. self.assertInHTML('<option value="blue">Blue</option>', html, count=1)
  99. self.assertInHTML('<option value="red" disabled>Red</option>', html, count=1)
  100. self.assertInHTML('<option value="green" disabled>Green</option>', html, count=1)