Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions django_mongodb_backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand Down
25 changes: 24 additions & 1 deletion tests/backend_/test_base.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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("...", [])