diff --git a/django_mongodb_backend/base.py b/django_mongodb_backend/base.py index 9307cea1c..e7ecb5582 100644 --- a/django_mongodb_backend/base.py +++ b/django_mongodb_backend/base.py @@ -4,7 +4,7 @@ from bson import Decimal128 from django.core.exceptions import EmptyResultSet, FullResultSet, ImproperlyConfigured -from django.db import DEFAULT_DB_ALIAS +from django.db import DEFAULT_DB_ALIAS, NotSupportedError from django.db.backends.base.base import BaseDatabaseWrapper from django.db.backends.utils import debug_transaction from django.utils.asyncio import async_unsafe @@ -28,14 +28,23 @@ class Cursor: - """A "nodb" cursor that does nothing except work on a context manager.""" + """DB-API style cursors aren't supported by MongoDB.""" def __enter__(self): - pass + return self def __exit__(self, exception_type, exception_value, exception_traceback): pass + def execute(self, query, args=None): + raise NotSupportedError("MongoDB does not support cursor.execute().") + + def executemany(self, query, args): + raise NotSupportedError("MongoDB does not support cursor.executemany().") + + def callproc(self, procname, params=None, kparams=None): + raise NotSupportedError("MongoDB does not support cursor.callproc().") + logger = logging.getLogger("django.db.backends.base") diff --git a/tests/backend_/test_base.py b/tests/backend_/test_base.py index 00993ef8f..760f6cbc3 100644 --- a/tests/backend_/test_base.py +++ b/tests/backend_/test_base.py @@ -1,7 +1,7 @@ from unittest.mock import patch from django.core.exceptions import ImproperlyConfigured -from django.db import connection +from django.db import NotSupportedError, connection from django.db.backends.signals import connection_created from django.test import SimpleTestCase, TestCase @@ -146,3 +146,26 @@ def receiver(sender, connection, **kwargs): # noqa: ARG001 data.clear() connection.connect() self.assertEqual(data, {}) + + +class CursorTests(TestCase): + def test_callproc(self): + msg = "MongoDB does not support cursor.callproc()." + with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor: + cursor.callproc("...") + with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor: + cursor.callproc("...", []) + with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor: + cursor.callproc("...", [], []) + + def test_execute(self): + msg = "MongoDB does not support cursor.execute()." + with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor: + cursor.execute("...") + with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor: + cursor.execute("...", []) + + def test_executemany(self): + msg = "MongoDB does not support cursor.executemany()." + with self.assertRaisesMessage(NotSupportedError, msg), connection.cursor() as cursor: + cursor.executemany("...", [])