django_dramatiq
django_dramatiq copied to clipboard
Database Query Inside Task During Testing
Thanks again for all of the support. Now that I have made it past the issue in #32, I have reached another question. Currently I am using Celery but trying to convert to dramatiq. My celery tasks take a full django model as one of the arguments. The model has a many-to-many field which is needed in one of my tasks. The test for this task is failing because the many-to-many field is returning an empty list.
I understand that it is better practice to just sent the id of the model instance and let the task query the database. I converted my task to take the model id and to query the database. That query is not returning any results.
I am suspecting that the task is not using the test database during while executing under manage.py test
but I could be wrong. I will try to set up a simple example and link it here if I can get it to recreate the issue.
I have tried to recreate this with a simple project but I have not been able to, yet.
I have dropped into pdb inside the task execution and confirmed that there are no entries in the database. However, dropping into pdb inside the view and inside the test both show the expected result of 1 entry in the database.
I checked the database connection string when in pdb inside the task and it correctly showed the test database name.
I just had a thought...is it possible that the view and test case are running in a transaction that the task can't see?
~~Okay, I was able to produce a failing test~~, please see this repo: https://github.com/idahogray/django-dramatiq-example. ~~I see that dramatiq_test.tests.DramatiqTaskTest.test_send_email_task fails in the same way as my original project.~~
I haven't been able to reproduce it, I just broke my tests and didn't realize it.
I took a quick look to see if I could run your tests and everything went fine on my end. I did notice one small thing that might be problematic: this block is unnecessary since the DbConnectionsMiddleware
already cleans up connections after each task and it seems like your teardown function ends up making it so worker.stop()
gets called twice (your method calls it and then its super method also calls stop
).
I couldn't find any other obvious issues w/ the app.
Thanks for looking. I will make that change and see if that helps in my production application.