Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing test #173

Open
mwarkentin opened this issue Dec 22, 2021 · 3 comments
Open

Failing test #173

mwarkentin opened this issue Dec 22, 2021 · 3 comments

Comments

@mwarkentin
Copy link
Owner

There was some hacky code put in place for this test:

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)

======================================================================
| 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
Copy link
Owner Author

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

@ulope
Copy link
Contributor

ulope commented Dec 23, 2021

@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 ;)

@mwarkentin
Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants