1
0

test_dateutils.py 1.9 KB

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