PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/forms/localflavor/generic.py

https://code.google.com/p/mango-py/
Python | 88 lines | 79 code | 7 blank | 2 comment | 0 complexity | 831191bd2b5b7a42a2b39f045cf0b0d3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import datetime
  2. from django.contrib.localflavor.generic.forms import DateField, DateTimeField
  3. from utils import LocalFlavorTestCase
  4. class GenericLocalFlavorTests(LocalFlavorTestCase):
  5. def test_GenericDateField(self):
  6. error_invalid = [u'Enter a valid date.']
  7. valid = {
  8. datetime.date(2006, 10, 25): datetime.date(2006, 10, 25),
  9. datetime.datetime(2006, 10, 25, 14, 30): datetime.date(2006, 10, 25),
  10. datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.date(2006, 10, 25),
  11. datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.date(2006, 10, 25),
  12. '2006-10-25': datetime.date(2006, 10, 25),
  13. '25/10/2006': datetime.date(2006, 10, 25),
  14. '25/10/06': datetime.date(2006, 10, 25),
  15. 'Oct 25 2006': datetime.date(2006, 10, 25),
  16. 'October 25 2006': datetime.date(2006, 10, 25),
  17. 'October 25, 2006': datetime.date(2006, 10, 25),
  18. '25 October 2006': datetime.date(2006, 10, 25),
  19. '25 October, 2006': datetime.date(2006, 10, 25),
  20. }
  21. invalid = {
  22. '2006-4-31': error_invalid,
  23. '200a-10-25': error_invalid,
  24. '10/25/06': error_invalid,
  25. }
  26. self.assertFieldOutput(DateField, valid, invalid, empty_value=None)
  27. # DateField with optional input_formats parameter
  28. valid = {
  29. datetime.date(2006, 10, 25): datetime.date(2006, 10, 25),
  30. datetime.datetime(2006, 10, 25, 14, 30): datetime.date(2006, 10, 25),
  31. '2006 10 25': datetime.date(2006, 10, 25),
  32. }
  33. invalid = {
  34. '2006-10-25': error_invalid,
  35. '25/10/2006': error_invalid,
  36. '25/10/06': error_invalid,
  37. }
  38. kwargs = {'input_formats':['%Y %m %d'],}
  39. self.assertFieldOutput(DateField,
  40. valid, invalid, field_kwargs=kwargs, empty_value=None
  41. )
  42. def test_GenericDateTimeField(self):
  43. error_invalid = [u'Enter a valid date/time.']
  44. valid = {
  45. datetime.date(2006, 10, 25): datetime.datetime(2006, 10, 25, 0, 0),
  46. datetime.datetime(2006, 10, 25, 14, 30): datetime.datetime(2006, 10, 25, 14, 30),
  47. datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.datetime(2006, 10, 25, 14, 30, 59),
  48. datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.datetime(2006, 10, 25, 14, 30, 59, 200),
  49. '2006-10-25 14:30:45': datetime.datetime(2006, 10, 25, 14, 30, 45),
  50. '2006-10-25 14:30:00': datetime.datetime(2006, 10, 25, 14, 30),
  51. '2006-10-25 14:30': datetime.datetime(2006, 10, 25, 14, 30),
  52. '2006-10-25': datetime.datetime(2006, 10, 25, 0, 0),
  53. '25/10/2006 14:30:45': datetime.datetime(2006, 10, 25, 14, 30, 45),
  54. '25/10/2006 14:30:00': datetime.datetime(2006, 10, 25, 14, 30),
  55. '25/10/2006 14:30': datetime.datetime(2006, 10, 25, 14, 30),
  56. '25/10/2006': datetime.datetime(2006, 10, 25, 0, 0),
  57. '25/10/06 14:30:45': datetime.datetime(2006, 10, 25, 14, 30, 45),
  58. '25/10/06 14:30:00': datetime.datetime(2006, 10, 25, 14, 30),
  59. '25/10/06 14:30': datetime.datetime(2006, 10, 25, 14, 30),
  60. '25/10/06': datetime.datetime(2006, 10, 25, 0, 0),
  61. }
  62. invalid = {
  63. 'hello': error_invalid,
  64. '2006-10-25 4:30 p.m.': error_invalid,
  65. }
  66. self.assertFieldOutput(DateTimeField, valid, invalid, empty_value=None)
  67. # DateTimeField with optional input_formats paramter
  68. valid = {
  69. datetime.date(2006, 10, 25): datetime.datetime(2006, 10, 25, 0, 0),
  70. datetime.datetime(2006, 10, 25, 14, 30): datetime.datetime(2006, 10, 25, 14, 30),
  71. datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.datetime(2006, 10, 25, 14, 30, 59),
  72. datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.datetime(2006, 10, 25, 14, 30, 59, 200),
  73. '2006 10 25 2:30 PM': datetime.datetime(2006, 10, 25, 14, 30),
  74. }
  75. invalid = {
  76. '2006-10-25 14:30:45': error_invalid,
  77. }
  78. kwargs = {'input_formats':['%Y %m %d %I:%M %p'],}
  79. self.assertFieldOutput(DateTimeField,
  80. valid, invalid, field_kwargs=kwargs, empty_value=None
  81. )