Skip to content

Commit

Permalink
Improve db blocker error on couch access
Browse files Browse the repository at this point in the history
  • Loading branch information
millerdev committed Nov 22, 2024
1 parent c833f7b commit 3d7465b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion corehq/tests/pytest_plugins/reusedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class CouchSpec(object):

def mock_couch(app):
dbname = dbs.get(app, main_db_url).rsplit("/", 1)[1]
return Mock(name=dbname, dbname=dbname, spec_set=CouchSpec)
return BlockedMock(name=dbname, dbname=dbname, spec_set=CouchSpec)

# register our dbs with the extension document classes
main_db_url = settings.COUCH_DATABASE
Expand All @@ -442,4 +442,15 @@ def unblock():
return block, unblock


class BlockedMock(Mock):
def __getattr__(self, name):
try:
return super().__getattr__(name)
except AttributeError:
raise RuntimeError(
'Database access not allowed, use the "django_db" mark, or '
'the "db" or "transactional_db" fixtures to enable it.'
)


_db_context = DeferredDatabaseContext()
2 changes: 1 addition & 1 deletion corehq/tests/test_reusedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def test_database_blocker():
assert not settings.DB_ENABLED

with pytest.raises(AttributeError, match="Mock object has no attribute 'info'"):
with pytest.raises(RuntimeError, match=re.compile("^Database access not allowed")):
CouchModel.get_db().info

with pytest.raises(RuntimeError, match=re.compile("^Database access not allowed")):
Expand Down

0 comments on commit 3d7465b

Please sign in to comment.