芹菜任务在测试时表现异常
Celery task behave strangely while testing
我正在尝试测试我的 Celery 任务是否更新了 Django 模型。它工作正常,但在测试期间表现异常。
# someapp/models.py
class SomeModel(models.Model):
...
hidden = models.BooleanField(default=False)
# someapp/tasks.py
@shared_task()
def my_task(model_id):
model_instance = someapp.models.SomeModel.objects.get(id=model_id)
model_instance.hidden = True
model_instance.save()
logger.info(f'Uncovered model instance with id {model_id]')
为了对此进行测试,我实施了以下工作流程:
- 我通过 factory-boy 工厂创建 SomeModel 对象,因为 SomeModel 依赖于多个模型。
- 我把这个对象赋值给变量model_instance
- 我在本地应用任务
- 我断言 model_instance.hidden 是正确的
下面的代码
# someapp/tests.py
@pytest.mark.django_db
@pytest.mark.celery(task_always_eager=True)
def test_celery_task_uncovers_model_instance() -> None:
SomeModelFactory.create(hidden=False)
some_model = someapp.models.SomeModel.objects.first()
assert some_model.hidden is True
my_task.apply((some_model.id, ))
assert some_model.hidden is True
在最后一行加注。然后我断言:
assert (model_instance.pk, model_instance.hidden) == (someapp.models.SomeModel.objects.first().pk,
someapp.models.SomeModel.objects.first().hidden)
它提出:
E assert (1, True) == (1, False)
E At index 1 diff: True != False
最后,我要检查ids:
assert id(model_instance) == id(authapp.models.SomeModel.objects.first())
它引发了这样的事情:
E AssertionError: assert 139938217188656 == 139938219885184
E + where 139938217188656 = id(<SomeModel: - 2022-02-01>)
E + and 139938219885184 = id(<SomeModel: - 2022-02-01>)
为什么任务不更新我测试中的 some_model
对象?
实例已在任务中更新但未在您的测试中更新,您需要在调用任务后使用Model.refresh_from_db()
从数据库刷新实例的数据
@pytest.mark.django_db
@pytest.mark.celery(task_always_eager=True)
def test_celery_task_uncovers_model_instance() -> None:
SomeModelFactory.create(hidden=False)
some_model = someapp.models.SomeModel.objects.first()
assert some_model.hidden is False
my_task.apply((some_model.id, ))
some_model.refresh_from_db() # <<<
assert some_model.hidden is True
我正在尝试测试我的 Celery 任务是否更新了 Django 模型。它工作正常,但在测试期间表现异常。
# someapp/models.py
class SomeModel(models.Model):
...
hidden = models.BooleanField(default=False)
# someapp/tasks.py
@shared_task()
def my_task(model_id):
model_instance = someapp.models.SomeModel.objects.get(id=model_id)
model_instance.hidden = True
model_instance.save()
logger.info(f'Uncovered model instance with id {model_id]')
为了对此进行测试,我实施了以下工作流程:
- 我通过 factory-boy 工厂创建 SomeModel 对象,因为 SomeModel 依赖于多个模型。
- 我把这个对象赋值给变量model_instance
- 我在本地应用任务
- 我断言 model_instance.hidden 是正确的
下面的代码
# someapp/tests.py
@pytest.mark.django_db
@pytest.mark.celery(task_always_eager=True)
def test_celery_task_uncovers_model_instance() -> None:
SomeModelFactory.create(hidden=False)
some_model = someapp.models.SomeModel.objects.first()
assert some_model.hidden is True
my_task.apply((some_model.id, ))
assert some_model.hidden is True
在最后一行加注。然后我断言:
assert (model_instance.pk, model_instance.hidden) == (someapp.models.SomeModel.objects.first().pk,
someapp.models.SomeModel.objects.first().hidden)
它提出:
E assert (1, True) == (1, False)
E At index 1 diff: True != False
最后,我要检查ids:
assert id(model_instance) == id(authapp.models.SomeModel.objects.first())
它引发了这样的事情:
E AssertionError: assert 139938217188656 == 139938219885184
E + where 139938217188656 = id(<SomeModel: - 2022-02-01>)
E + and 139938219885184 = id(<SomeModel: - 2022-02-01>)
为什么任务不更新我测试中的 some_model
对象?
实例已在任务中更新但未在您的测试中更新,您需要在调用任务后使用Model.refresh_from_db()
从数据库刷新实例的数据
@pytest.mark.django_db
@pytest.mark.celery(task_always_eager=True)
def test_celery_task_uncovers_model_instance() -> None:
SomeModelFactory.create(hidden=False)
some_model = someapp.models.SomeModel.objects.first()
assert some_model.hidden is False
my_task.apply((some_model.id, ))
some_model.refresh_from_db() # <<<
assert some_model.hidden is True