test_text.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from celery.utils.text import (
  4. abbr,
  5. abbrtask,
  6. ensure_newlines,
  7. indent,
  8. pretty,
  9. truncate,
  10. )
  11. RANDTEXT = """\
  12. The quick brown
  13. fox jumps
  14. over the
  15. lazy dog\
  16. """
  17. RANDTEXT_RES = """\
  18. The quick brown
  19. fox jumps
  20. over the
  21. lazy dog\
  22. """
  23. QUEUES = {
  24. 'queue1': {
  25. 'exchange': 'exchange1',
  26. 'exchange_type': 'type1',
  27. 'routing_key': 'bind1',
  28. },
  29. 'queue2': {
  30. 'exchange': 'exchange2',
  31. 'exchange_type': 'type2',
  32. 'routing_key': 'bind2',
  33. },
  34. }
  35. QUEUE_FORMAT1 = '.> queue1 exchange=exchange1(type1) key=bind1'
  36. QUEUE_FORMAT2 = '.> queue2 exchange=exchange2(type2) key=bind2'
  37. class test_Info:
  38. def test_textindent(self):
  39. assert indent(RANDTEXT, 4) == RANDTEXT_RES
  40. def test_format_queues(self, app):
  41. app.amqp.queues = app.amqp.Queues(QUEUES)
  42. assert (sorted(app.amqp.queues.format().split('\n')) ==
  43. sorted([QUEUE_FORMAT1, QUEUE_FORMAT2]))
  44. def test_ensure_newlines(self):
  45. assert len(ensure_newlines('foo\nbar\nbaz\n').splitlines()) == 3
  46. assert len(ensure_newlines('foo\nbar').splitlines()) == 2
  47. @pytest.mark.parametrize('s,maxsize,expected', [
  48. ('ABCDEFGHI', 3, 'ABC...'),
  49. ('ABCDEFGHI', 10, 'ABCDEFGHI'),
  50. ])
  51. def test_truncate_text(s, maxsize, expected):
  52. assert truncate(s, maxsize) == expected
  53. @pytest.mark.parametrize('args,expected', [
  54. ((None, 3), '???'),
  55. (('ABCDEFGHI', 6), 'ABC...'),
  56. (('ABCDEFGHI', 20), 'ABCDEFGHI'),
  57. (('ABCDEFGHI', 6, None), 'ABCDEF'),
  58. ])
  59. def test_abbr(args, expected):
  60. assert abbr(*args) == expected
  61. @pytest.mark.parametrize('s,maxsize,expected', [
  62. (None, 3, '???'),
  63. ('feeds.tasks.refresh', 10, '[.]refresh'),
  64. ('feeds.tasks.refresh', 30, 'feeds.tasks.refresh'),
  65. ])
  66. def test_abbrtask(s, maxsize, expected):
  67. assert abbrtask(s, maxsize) == expected
  68. def test_pretty():
  69. assert pretty(('a', 'b', 'c'))