test_text.py 1.9 KB

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