Skip to content

Commit b671fc0

Browse files
committed
INTPYTHON-928 Add helpful error messages for Cursor.execute() & executemany()
1 parent bcbb896 commit b671fc0

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

django_mongodb_backend/base.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from bson import Decimal128
66
from django.core.exceptions import EmptyResultSet, FullResultSet, ImproperlyConfigured
7-
from django.db import DEFAULT_DB_ALIAS
7+
from django.db import DEFAULT_DB_ALIAS, NotSupportedError
88
from django.db.backends.base.base import BaseDatabaseWrapper
99
from django.db.backends.utils import debug_transaction
1010
from django.utils.asyncio import async_unsafe
@@ -28,14 +28,23 @@
2828

2929

3030
class Cursor:
31-
"""A "nodb" cursor that does nothing except work on a context manager."""
31+
"""DB-API style cursors aren't supported by MongoDB."""
3232

3333
def __enter__(self):
34-
pass
34+
return self
3535

3636
def __exit__(self, exception_type, exception_value, exception_traceback):
3737
pass
3838

39+
def execute(self, query, args=None):
40+
raise NotSupportedError("MongoDB does not support cursor.execute().")
41+
42+
def executemany(self, query, args):
43+
raise NotSupportedError("MongoDB does not support cursor.executemany().")
44+
45+
def callproc(self, procname, params=None, kparams=None):
46+
raise NotSupportedError("MongoDB does not support cursor.callproc().")
47+
3948

4049
logger = logging.getLogger("django.db.backends.base")
4150

tests/backend_/test_base.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest.mock import patch
22

33
from django.core.exceptions import ImproperlyConfigured
4-
from django.db import connection
4+
from django.db import NotSupportedError, connection
55
from django.db.backends.signals import connection_created
66
from django.test import SimpleTestCase, TestCase
77

@@ -146,3 +146,26 @@ def receiver(sender, connection, **kwargs): # noqa: ARG001
146146
data.clear()
147147
connection.connect()
148148
self.assertEqual(data, {})
149+
150+
151+
class CursorTests(TestCase):
152+
def test_callproc(self):
153+
msg = "MongoDB does not support cursor.callproc()."
154+
with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor:
155+
cursor.callproc("...")
156+
with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor:
157+
cursor.callproc("...", [])
158+
with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor:
159+
cursor.callproc("...", [], [])
160+
161+
def test_execute(self):
162+
msg = "MongoDB does not support cursor.execute()."
163+
with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor:
164+
cursor.execute("...")
165+
with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor:
166+
cursor.execute("...", [])
167+
168+
def test_executemany(self):
169+
msg = "MongoDB does not support cursor.executemany()."
170+
with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor:
171+
cursor.executemany("...", [])

0 commit comments

Comments
 (0)