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