test_utils_info.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from celery.tests.utils import unittest
  2. from celery import Celery
  3. from celery.utils import textindent
  4. from celery.utils.timeutils import humanize_seconds
  5. RANDTEXT = """\
  6. The quick brown
  7. fox jumps
  8. over the
  9. lazy dog\
  10. """
  11. RANDTEXT_RES = """\
  12. The quick brown
  13. fox jumps
  14. over the
  15. lazy dog\
  16. """
  17. QUEUES = {"queue1": {
  18. "exchange": "exchange1",
  19. "exchange_type": "type1",
  20. "binding_key": "bind1"},
  21. "queue2": {
  22. "exchange": "exchange2",
  23. "exchange_type": "type2",
  24. "binding_key": "bind2"}}
  25. QUEUE_FORMAT1 = """. queue1: exchange:exchange1 (type1) binding:bind1"""
  26. QUEUE_FORMAT2 = """. queue2: exchange:exchange2 (type2) binding:bind2"""
  27. class TestInfo(unittest.TestCase):
  28. def test_humanize_seconds(self):
  29. t = ((4 * 60 * 60 * 24, "4 days"),
  30. (1 * 60 * 60 * 24, "1 day"),
  31. (4 * 60 * 60, "4 hours"),
  32. (1 * 60 * 60, "1 hour"),
  33. (4 * 60, "4 minutes"),
  34. (1 * 60, "1 minute"),
  35. (4, "4.00 seconds"),
  36. (1, "1.00 second"),
  37. (4.3567631221, "4.36 seconds"),
  38. (0, "now"))
  39. for seconds, human in t:
  40. self.assertEqual(humanize_seconds(seconds), human)
  41. self.assertEqual(humanize_seconds(4, prefix="about "),
  42. "about 4.00 seconds")
  43. def test_textindent(self):
  44. self.assertEqual(textindent(RANDTEXT, 4), RANDTEXT_RES)
  45. def test_format_queues(self):
  46. celery = Celery(set_as_current=False)
  47. celery.amqp.queues = QUEUES
  48. self.assertItemsEqual(celery.amqp.queues.format().split("\n"),
  49. [QUEUE_FORMAT1, QUEUE_FORMAT2])