django-watchman icon indicating copy to clipboard operation
django-watchman copied to clipboard

Failing test

Open mwarkentin opened this issue 3 years ago • 3 comments

There was some hacky code put in place for this test: https://github.com/mwarkentin/django-watchman/blob/67e470775afdfbaf4c87919c5fa83d781c2e5560/tests/test_views.py#L281-L320

======================================================================
| FAIL: test_db_error_w_atomic_requests (tests.test_views.TestDBError)
| ----------------------------------------------------------------------
| Traceback (most recent call last):
|   File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/test/utils.py", line 437, in inner
|     return func(*args, **kwargs)
|   File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/unittest/mock.py", line 1336, in patched
|     return func(*newargs, **newkeywargs)
|   File "/Users/michael/wave/src/sandbox/django-watchman/tests/test_views.py", line 319, in test_db_error_w_atomic_requests
|     self.assertEqual(response.status_code, 201)
| AssertionError: 200 != 201

Full test class that will be removed for now:

class TestDBError(TransactionTestCase):
    """
    Ensure that we produce a valid response even in case of database
    connection issues with `ATOMIC_REQUESTS` enabled.
    Since overriding `DATABASES` isn't officially supported we need to perform
    some gymnastics here to convince django.
    """
    def setUp(self):
        # Cache current database connections
        self.databases = copy(connections._databases)
        self.connection = getattr(connections._connections, DEFAULT_DB_ALIAS, None)
        del connections.__dict__['databases']  # remove cached_property value
        connections._databases = None
        connections._connections = local()

    def tearDown(self):
        # Restore previous database connections
        connections._databases = self.databases
        setattr(connections._connections, DEFAULT_DB_ALIAS, self.connection)
        del connections.__dict__['databases']  # remove cached_property value

    @override_settings(
        DATABASES={
            'default': {
                "ENGINE": "django.db.backends.mysql",
                "HOST": "no.host.by.this.name.some-tld-that-doesnt-exist",
                "ATOMIC_REQUESTS": True
            },
        }
    )
    # can't use override_settings because of
    # https://github.com/mwarkentin/django-watchman/issues/13
    @patch('watchman.settings.WATCHMAN_ERROR_CODE', 201)
    def test_db_error_w_atomic_requests(self):
        # Ensure we don't trigger django's generic 500 page in case of DB error
        response = Client().get('/', data={
            'check': 'watchman.checks.databases',
        })
        self.assertEqual(response.status_code, 201)

mwarkentin avatar Dec 22 '21 16:12 mwarkentin

CC @ulope if there's any chance you're still using Django-watchman and interested in sorting this out with me. ;)

mwarkentin avatar Dec 22 '21 16:12 mwarkentin

@mwarkentin At the moment I'm not using django at all :/ I'll see if I have some time after Christmas to try remember what my thinking was there, but I can't promise anything ;)

ulope avatar Dec 23 '21 09:12 ulope

No worries, figured it was worth a shot! :)

mwarkentin avatar Dec 23 '21 12:12 mwarkentin