2

I am using Django-Q to send async emails with Django 2.2, DRF 3.9 and Postgres 10.9

The setup works fine except when it comes to unit tests. I refer to the following issue which is the exact same thing I am facing: https://github.com/Koed00/django-q/issues/266

As per the link, one of the solutions was to do change the sync setting to 'True' for testing purposes.

This is what i have in tests.py:

from django.conf import settings
settings.Q_CLUSTER['sync'] = True


class UserAPITestCase(APITransactionTestCase):
    print(settings.Q_CLUSTER)

'print' shows that the 'sync':True has been added, but the async_task still runs in async mode.

However if I were to add the sync setting in the settings file directly everything works as it should and the async_task runs synchronously.

It's like django-q isn't taking in the setting if it is updated later. How do I fix that?

kbsol
  • 512
  • 6
  • 18

2 Answers2

0

You can try this solution

from django_q.conf import Conf

class UserAPITestCase(APITransactionTestCase):
    def setUp(self):
        Conf.SYNC = True
    def test_do_something_with_async_task(self):
        pass
    def tearDown(self):
        Conf.SYNC = False 
tz01x
  • 51
  • 5
-1

Try doing

from django.test import override_settings
from django.conf import settings


@override_settings(Q_CLUSTER={**settings.Q_CLUSTER, 'sync': True})
class UserAPITestCase(APITransactionTestCase):
    pass
Nafees Anwar
  • 6,324
  • 2
  • 23
  • 42
  • I get the same results with this unfortunately. Sync doesn't get updated to True – kbsol Aug 30 '19 at 02:59
  • Seems that there might be an issue with override_settings and DRF – kbsol Aug 30 '19 at 07:06
  • If this solution does not work, I would suggest you to follow best practice for handling settings in different environments, like staging, testing, development and production. It is to create a base settings file and inherit files for other environments from it. Here is a [helpful article](https://medium.com/@ayarshabeer/django-best-practice-settings-file-for-multiple-environments-6d71c6966ee2) about it. – Nafees Anwar Aug 30 '19 at 07:32
  • Thanks. I had the same idea. For now I'm doing this as a workaround. – kbsol Aug 30 '19 at 10:43