1
0

test_dateutils.py 2.0 KB

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