test_datetime_truncate.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from datetime import datetime
  2. import unittest
  3. from datetime_truncate import truncate
  4. from datetime_truncate import truncate_half_year
  5. from datetime_truncate import truncate_quarter
  6. from datetime_truncate import truncate_week
  7. DEFAULT_DT = datetime(2012, 7, 12, 12, 14, 14, 342)
  8. class TestDatetimeTruncate(unittest.TestCase):
  9. def setUp(self):
  10. self.default_dt = datetime(2012, 7, 12, 12, 14, 14, 342)
  11. def test_truncate_to_second(self):
  12. self.assertEqual(truncate(self.default_dt, 'second'),
  13. self.default_dt.replace(microsecond=0))
  14. def test_truncate_to_minute(self):
  15. self.assertEqual(truncate(self.default_dt, 'minute'),
  16. self.default_dt.replace(second=0, microsecond=0))
  17. def test_truncate_to_hour(self):
  18. self.assertEqual(truncate(self.default_dt, 'hour'),
  19. self.default_dt.replace(minute=0, second=0,
  20. microsecond=0))
  21. def test_truncate_to_day(self):
  22. self.assertEqual(truncate(self.default_dt, 'day'),
  23. self.default_dt.replace(hour=0, minute=0,
  24. second=0, microsecond=0))
  25. def test_truncate_to_month(self):
  26. self.assertEqual(truncate(self.default_dt, 'month'),
  27. self.default_dt.replace(day=1, hour=0, minute=0,
  28. second=0, microsecond=0))
  29. def test_truncate_to_year(self):
  30. self.assertEqual(truncate(self.default_dt, 'year'),
  31. self.default_dt.replace(month=1, day=1, hour=0,
  32. minute=0, second=0,
  33. microsecond=0))
  34. def test_truncate_to_week(self):
  35. self.assertEqual(truncate(self.default_dt, 'week'),
  36. self.default_dt.replace(day=9, hour=0,
  37. minute=0, second=0,
  38. microsecond=0))
  39. self.assertEqual(truncate(self.default_dt.replace(day=9), 'week'),
  40. self.default_dt.replace(day=9, hour=0,
  41. minute=0, second=0,
  42. microsecond=0))
  43. self.assertEqual(truncate(self.default_dt.replace(day=16), 'week'),
  44. self.default_dt.replace(day=16, hour=0,
  45. minute=0, second=0,
  46. microsecond=0))
  47. self.assertEqual(truncate_week(self.default_dt),
  48. self.default_dt.replace(day=9, hour=0,
  49. minute=0, second=0,
  50. microsecond=0))
  51. self.assertEqual(truncate_week(self.default_dt.replace(day=9)),
  52. self.default_dt.replace(day=9, hour=0,
  53. minute=0, second=0,
  54. microsecond=0))
  55. self.assertEqual(truncate_week(self.default_dt.replace(day=16)),
  56. self.default_dt.replace(day=16, hour=0,
  57. minute=0, second=0,
  58. microsecond=0))
  59. def test_truncate_to_quarter(self):
  60. self.assertEqual(truncate(self.default_dt.replace(month=2), 'quarter'),
  61. self.default_dt.replace(month=1, day=1, hour=0,
  62. minute=0, second=0,
  63. microsecond=0))
  64. self.assertEqual(truncate(self.default_dt.replace(month=6), 'quarter'),
  65. self.default_dt.replace(month=4, day=1, hour=0,
  66. minute=0, second=0,
  67. microsecond=0))
  68. self.assertEqual(truncate(self.default_dt, 'quarter'),
  69. self.default_dt.replace(month=7, day=1, hour=0,
  70. minute=0, second=0,
  71. microsecond=0))
  72. self.assertEqual(
  73. truncate(self.default_dt.replace(month=10), 'quarter'),
  74. self.default_dt.replace(month=10, day=1, hour=0,
  75. minute=0, second=0,
  76. microsecond=0))
  77. self.assertEqual(truncate_quarter(self.default_dt.replace(month=2)),
  78. self.default_dt.replace(month=1, day=1, hour=0,
  79. minute=0, second=0,
  80. microsecond=0))
  81. self.assertEqual(truncate_quarter(self.default_dt.replace(month=6)),
  82. self.default_dt.replace(month=4, day=1, hour=0,
  83. minute=0, second=0,
  84. microsecond=0))
  85. self.assertEqual(truncate_quarter(self.default_dt),
  86. self.default_dt.replace(month=7, day=1, hour=0,
  87. minute=0, second=0,
  88. microsecond=0))
  89. self.assertEqual(
  90. truncate_quarter(self.default_dt.replace(month=10)),
  91. self.default_dt.replace(month=10, day=1, hour=0,
  92. minute=0, second=0,
  93. microsecond=0))
  94. def test_truncat_to_half_year(self):
  95. self.assertEqual(
  96. truncate(self.default_dt.replace(month=6), 'half_year'),
  97. self.default_dt.replace(month=1, day=1, hour=0,
  98. minute=0, second=0,
  99. microsecond=0)
  100. )
  101. self.assertEqual(
  102. truncate_half_year(self.default_dt.replace(month=6)),
  103. self.default_dt.replace(month=1, day=1, hour=0,
  104. minute=0, second=0,
  105. microsecond=0)
  106. )
  107. self.assertEqual(truncate(self.default_dt, 'half_year'),
  108. self.default_dt.replace(month=7, day=1, hour=0,
  109. minute=0, second=0,
  110. microsecond=0))
  111. self.assertEqual(truncate_half_year(self.default_dt),
  112. self.default_dt.replace(month=7, day=1, hour=0,
  113. minute=0, second=0,
  114. microsecond=0))