Bogdanp / django_dramatiq

A Django app that integrates with Dramatiq.
https://dramatiq.io
Other
331 stars 77 forks source link

Database Query Inside Task During Testing #35

Open idahogray opened 5 years ago

idahogray commented 5 years ago

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.

idahogray commented 5 years ago

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?

idahogray commented 5 years ago

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.

Bogdanp commented 5 years ago

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.

idahogray commented 5 years ago

Thanks for looking. I will make that change and see if that helps in my production application.