test_dateutils.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for salt.utils.dateutils
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import datetime
  8. # Import Salt libs
  9. import salt.utils.dateutils
  10. from tests.support.mock import patch
  11. # Import Salt Testing libs
  12. from tests.support.unit import TestCase, skipIf
  13. # Import 3rd-party libs
  14. try:
  15. import timelib # pylint: disable=import-error,unused-import
  16. HAS_TIMELIB = True
  17. except ImportError:
  18. HAS_TIMELIB = False
  19. class DateutilsTestCase(TestCase):
  20. def test_date_cast(self):
  21. now = datetime.datetime.now()
  22. with patch("datetime.datetime"):
  23. datetime.datetime.now.return_value = now
  24. self.assertEqual(now, salt.utils.dateutils.date_cast(None))
  25. self.assertEqual(now, salt.utils.dateutils.date_cast(now))
  26. try:
  27. ret = salt.utils.dateutils.date_cast("Mon Dec 23 10:19:15 MST 2013")
  28. expected_ret = datetime.datetime(2013, 12, 23, 10, 19, 15)
  29. self.assertEqual(ret, expected_ret)
  30. except RuntimeError:
  31. if not HAS_TIMELIB:
  32. # Unparseable without timelib installed
  33. self.skipTest("'timelib' is not installed")
  34. else:
  35. raise
  36. @skipIf(not HAS_TIMELIB, "'timelib' is not installed")
  37. def test_strftime(self):
  38. # Taken from doctests
  39. expected_ret = "2002-12-25"
  40. src = datetime.datetime(2002, 12, 25, 12, 00, 00, 00)
  41. ret = salt.utils.dateutils.strftime(src)
  42. self.assertEqual(ret, expected_ret)
  43. src = "2002/12/25"
  44. ret = salt.utils.dateutils.strftime(src)
  45. self.assertEqual(ret, expected_ret)
  46. src = 1040814000
  47. ret = salt.utils.dateutils.strftime(src)
  48. self.assertEqual(ret, expected_ret)
  49. src = "1040814000"
  50. ret = salt.utils.dateutils.strftime(src)
  51. self.assertEqual(ret, expected_ret)