robinhood / thorn

Easy Webhooks for Python
Other
525 stars 60 forks source link

Change THORN_DISPATCHER during runtime #19

Closed mwisner closed 7 years ago

mwisner commented 7 years ago

I'm working on writing some unit / integration tests for our webhook system and was wondering if there is a way to change the THORN_DISPATCHER setting during individual tests. or maybe just "reload" thorn somehow. My goal is to disable thorn during testing and then turn it on again for specific tests.

Typically I would just do this with something like:

    def setUp(self):
        settings.THORN_DISPATCHER = 'celery'

    def tearDown(self):
        settings.THORN_DISPATCHER = 'disabled'

But when I do this it looks like it still uses the default settings that were originally set. I think think this is because of the cached_property usage.

I looked through the thorn tests and couldn't find anything that resembled this and the google didn't seem very helpful...

mwisner commented 7 years ago

I think I figured it out. Not sure if it's the best approach or not...

from django.test import TransactionTestCase
from thorn import Thorn
from unittest import mock

class MyTests(TransactionTestCase):

    def setUp(self):
        app = Thorn(set_as_current=True)
        app.config.THORN_DISPATCHER = 'celery'

    def tearDown(self):
        app = Thorn(set_as_current=True)
        app.config.THORN_DISPATCHER = 'disabled'

    def test_send_on_change_webhook(self):
        with mock.patch('thorn.tasks.send_event.apply_async') as e:
           # Do model things
            e.assert_called()