I'm trying to write some unit tests for some celery tasks in my Django app. These tasks take a model id as the argument, do some stuff, and update the model. When running a devserver and celery worker, everything works great, but when running my tests, it has become clear that the celery task is not using the django test db that gets created and destroyed as part of the test run. Question is, how can I get celery to use the same temporary db as the rest of my tests?
As you can see, I'm using the settings overrides that are suggested in every answer for similar issues.
UPDATE: Discovered that instead of passing the object id to the task and having the task get it from the db, if I simply pass the object itself to task, the tests work correctly with apparently no adverse effects on the functioning of the task. So at least for now, that will be my fix.
In my test:
class JobTest(TestCase):
@override_settings(CELERY_ALWAYS_EAGER=True,
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
BROKER_BACKEND='memory')
def test_Job_Complete(self):
job = models.Job()
job.save()
tasks.do_a_thing(job.id)
self.assertTrue(job.complete)
In my task:
@celery.task
def do_a_thing(job_id):
job = models.Job.objects.get(pk=job_id)
bunch_of_things(job)
job.complete = True
job.save()