mardi 1 décembre 2020

Testing Django fire and forget callback

I have a function fire_and_forget for sending emails in Django its working perfectly, but how can I test the mail function

fire and forget function

def fire_and_forget(coro):
    global _loop
    if _loop is None:
        _loop = asyncio.new_event_loop()
        threading.Thread(target=_loop.run_forever, daemon=True).start()
    _loop.call_soon_threadsafe(asyncio.create_task, coro)

test function

class TestEmails(TestCase):
    def setUp(self):
        create_mock_data(self)

    def test_send_mail(self):
        fire_and_forget(send_mail(self.order))
        self.assertEqual(len(mail.outbox), 2)
        self.assertEqual(mail.outbox[0].to, [TEST_EMAIL])
        self.assertEqual(mail.outbox[1].to, [DEFAULT_EMAIL])

is it possible to wait for the thread?

furthermore when the test Destroying test database Im getting following error:

Task exception was never retrieved
sqlite3.OperationalError: database table is locked:

In the send_mail its retrieving data from database...

Aucun commentaire:

Enregistrer un commentaire