| 1 |
from celery.app.task import Context |
| 2 |
|
| 3 |
|
| 4 |
# Retrieve the values of all context attributes as a |
| 5 |
# dictionary in an implementation-agnostic manner. |
| 6 |
def get_context_as_dict(ctx, getter=getattr): |
| 7 |
defaults = {} |
| 8 |
for attr_name in dir(ctx): |
| 9 |
if attr_name.startswith('_'): |
| 10 |
continue # Ignore pseudo-private attributes |
| 11 |
attr = getter(ctx, attr_name) |
| 12 |
if callable(attr): |
| 13 |
continue # Ignore methods and other non-trivial types |
| 14 |
defaults[attr_name] = attr |
| 15 |
return defaults |
| 16 |
|
| 17 |
|
| 18 |
default_context = get_context_as_dict(Context()) |
| 19 |
|
| 20 |
|
| 21 |
class test_Context: |
| 22 |
|
| 23 |
def test_default_context(self): |
| 24 |
# A bit of a tautological test, since it uses the same |
| 25 |
# initializer as the default_context constructor. |
| 26 |
defaults = dict(default_context, children=[]) |
| 27 |
assert get_context_as_dict(Context()) == defaults |
| 28 |
|
| 29 |
def test_updated_context(self): |
| 30 |
expected = dict(default_context) |
| 31 |
changes = {'id': 'unique id', 'args': ['some', 1], 'wibble': 'wobble'} |
| 32 |
ctx = Context() |
| 33 |
expected.update(changes) |
| 34 |
ctx.update(changes) |
| 35 |
assert get_context_as_dict(ctx) == expected |
| 36 |
assert get_context_as_dict(Context()) == default_context |
| 37 |
|
| 38 |
def test_modified_context(self): |
| 39 |
expected = dict(default_context) |
| 40 |
ctx = Context() |
| 41 |
expected['id'] = 'unique id' |
| 42 |
expected['args'] = ['some', 1] |
| 43 |
ctx.id = 'unique id' |
| 44 |
ctx.args = ['some', 1] |
| 45 |
assert get_context_as_dict(ctx) == expected |
| 46 |
assert get_context_as_dict(Context()) == default_context |
| 47 |
|
| 48 |
def test_cleared_context(self): |
| 49 |
changes = {'id': 'unique id', 'args': ['some', 1], 'wibble': 'wobble'} |
| 50 |
ctx = Context() |
| 51 |
ctx.update(changes) |
| 52 |
ctx.clear() |
| 53 |
defaults = dict(default_context, children=[]) |
| 54 |
assert get_context_as_dict(ctx) == defaults |
| 55 |
assert get_context_as_dict(Context()) == defaults |
| 56 |
|
| 57 |
def test_context_get(self): |
| 58 |
expected = dict(default_context) |
| 59 |
changes = {'id': 'unique id', 'args': ['some', 1], 'wibble': 'wobble'} |
| 60 |
ctx = Context() |
| 61 |
expected.update(changes) |
| 62 |
ctx.update(changes) |
| 63 |
ctx_dict = get_context_as_dict(ctx, getter=Context.get) |
| 64 |
assert ctx_dict == expected |
| 65 |
assert get_context_as_dict(Context()) == default_context |
| 66 |
|
| 67 |
def test_extract_headers(self): |
| 68 |
# Should extract custom headers from the request dict |
| 69 |
request = { |
| 70 |
'task': 'test.test_task', |
| 71 |
'id': 'e16eeaee-1172-49bb-9098-5437a509ffd9', |
| 72 |
'custom-header': 'custom-value', |
| 73 |
} |
| 74 |
ctx = Context(request) |
| 75 |
assert ctx.headers == {'custom-header': 'custom-value'} |
| 76 |
|
| 77 |
def test_dont_override_headers(self): |
| 78 |
# Should not override headers if defined in the request |
| 79 |
request = { |
| 80 |
'task': 'test.test_task', |
| 81 |
'id': 'e16eeaee-1172-49bb-9098-5437a509ffd9', |
| 82 |
'headers': {'custom-header': 'custom-value'}, |
| 83 |
'custom-header-2': 'custom-value-2', |
| 84 |
} |
| 85 |
ctx = Context(request) |
| 86 |
assert ctx.headers == {'custom-header': 'custom-value'} |