|
@@ -26,45 +26,28 @@ def temp_context():
|
|
|
ctx.correlation_id = None
|
|
|
|
|
|
|
|
|
-def test_apply_async(mocked_apply_async):
|
|
|
- BaseTask().apply_async(args="foo", kwargs="bar")
|
|
|
+@mock.patch(
|
|
|
+ "clean_python.celery.base_task.uuid4",
|
|
|
+ return_value=UUID("479156af-a302-48fc-89ed-8c426abadc4c"),
|
|
|
+)
|
|
|
+def test_apply_async(uuid4, mocked_apply_async):
|
|
|
+ BaseTask().apply_async(args=("foo",), kwargs={"a": "bar"})
|
|
|
|
|
|
assert mocked_apply_async.call_count == 1
|
|
|
- args, kwargs = mocked_apply_async.call_args
|
|
|
- assert args == ("foo", "bar")
|
|
|
- assert kwargs["headers"][HEADER_FIELD]["tenant"] is None
|
|
|
- UUID(kwargs["headers"][HEADER_FIELD]["correlation_id"]) # generated
|
|
|
+ (args, kwargs), _ = mocked_apply_async.call_args
|
|
|
+ assert args == ("foo",)
|
|
|
+ assert kwargs["a"] == "bar"
|
|
|
+ assert kwargs[HEADER_FIELD] == {
|
|
|
+ "tenant": None,
|
|
|
+ "correlation_id": "479156af-a302-48fc-89ed-8c426abadc4c",
|
|
|
+ }
|
|
|
|
|
|
|
|
|
def test_apply_async_with_context(mocked_apply_async, temp_context):
|
|
|
- BaseTask().apply_async(args="foo", kwargs="bar")
|
|
|
+ BaseTask().apply_async(args=("foo",), kwargs={"a": "bar"})
|
|
|
|
|
|
assert mocked_apply_async.call_count == 1
|
|
|
- _, kwargs = mocked_apply_async.call_args
|
|
|
- assert kwargs["headers"][HEADER_FIELD]["tenant"] == temp_context.tenant.model_dump(
|
|
|
- mode="json"
|
|
|
- )
|
|
|
- kwargs["headers"][HEADER_FIELD]["correlation_id"] == str(
|
|
|
- temp_context.correlation_id
|
|
|
- )
|
|
|
-
|
|
|
-
|
|
|
-def test_apply_async_headers_extended(mocked_apply_async):
|
|
|
- headers = {"baz": 2}
|
|
|
- BaseTask().apply_async(args="foo", kwargs="bar", headers=headers)
|
|
|
-
|
|
|
- assert mocked_apply_async.call_count == 1
|
|
|
- _, kwargs = mocked_apply_async.call_args
|
|
|
- assert kwargs["headers"]["baz"] == 2
|
|
|
- assert kwargs["headers"][HEADER_FIELD]["tenant"] is None
|
|
|
- UUID(kwargs["headers"][HEADER_FIELD]["correlation_id"]) # generated
|
|
|
-
|
|
|
- assert headers == {"baz": 2} # not changed inplace
|
|
|
-
|
|
|
-
|
|
|
-def test_apply_async_headers_already_present(mocked_apply_async):
|
|
|
- BaseTask().apply_async(args="foo", kwargs="bar", headers={HEADER_FIELD: "foo"})
|
|
|
-
|
|
|
- assert mocked_apply_async.call_count == 1
|
|
|
- _, kwargs = mocked_apply_async.call_args
|
|
|
- assert kwargs["headers"] == {HEADER_FIELD: "foo"}
|
|
|
+ (_, kwargs), _ = mocked_apply_async.call_args
|
|
|
+ assert kwargs["a"] == "bar"
|
|
|
+ assert kwargs[HEADER_FIELD]["tenant"] == temp_context.tenant.model_dump(mode="json")
|
|
|
+ assert kwargs[HEADER_FIELD]["correlation_id"] == str(temp_context.correlation_id)
|