Skip to content

Commit affa499

Browse files
committed
fix features.supports_queryable_encryption tests
1 parent 796855e commit affa499

File tree

2 files changed

+87
-27
lines changed

2 files changed

+87
-27
lines changed

django_mongodb_backend/features.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -625,16 +625,14 @@ def _supports_transactions(self):
625625
@cached_property
626626
def supports_queryable_encryption(self):
627627
"""
628-
Queryable Encryption is supported if the server is Atlas or Enterprise
629-
and is configured as a replica set or a sharded cluster.
628+
Queryable Encryption requires a MongoDB 7.0 or later replica set or sharded
629+
cluster, as well as MonogDB Atlas or Enterprise.
630630
"""
631631
self.connection.ensure_connection()
632-
client = self.connection.connection.admin
633-
build_info = client.command("buildInfo")
632+
build_info = self.connection.connection.admin.command("buildInfo")
634633
is_enterprise = "enterprise" in build_info.get("modules")
635-
# Queryable Encryption requires transaction support which
636-
# is only available on replica sets or sharded clusters
637-
# which we already check in `supports_transactions`.
638-
supports_transactions = self.supports_transactions
639-
# TODO: check if the server is Atlas
640-
return is_enterprise and supports_transactions and self.is_mongodb_7_0
634+
return (
635+
(is_enterprise or self.supports_atlas_search)
636+
and self._supports_transactions
637+
and self.is_mongodb_7_0
638+
)

tests/backend_/test_features.py

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,82 @@ def mocked_command(command):
4545
with patch("pymongo.synchronous.database.Database.command", wraps=mocked_command):
4646
self.assertIs(connection.features._supports_transactions, False)
4747

48-
# FIXME: On evergreen (again).
49-
# class SupportsQueryableEncryptionTests(TestCase):
50-
# def setUp(self):
51-
# # Clear the cached property.
52-
# connection.features.__dict__.pop("supports_queryable_encryption", None)
53-
#
54-
# def tearDown(self):
55-
# del connection.features.supports_queryable_encryption
56-
#
57-
# def test_supports_queryable_encryption(self):
58-
# def mocked_command(command):
59-
# if command == "buildInfo":
60-
# return {"modules": ["enterprise"]}
61-
# raise Exception("Unexpected command")
62-
#
63-
# with patch("pymongo.synchronous.database.Database.command", wraps=mocked_command):
64-
# self.assertIs(connection.features.supports_queryable_encryption, True)
48+
49+
class SupportsQueryableEncryptionTests(TestCase):
50+
def setUp(self):
51+
# Clear the cached property.
52+
connection.features.__dict__.pop("supports_queryable_encryption", None)
53+
# Must initialize the feature before patching it.
54+
connection.features._supports_transactions # noqa: B018
55+
56+
def tearDown(self):
57+
del connection.features.supports_queryable_encryption
58+
59+
@staticmethod
60+
def enterprise_response(command):
61+
if command == "buildInfo":
62+
return {"modules": ["enterprise"]}
63+
raise Exception("Unexpected command")
64+
65+
@staticmethod
66+
def non_enterprise_response(command):
67+
if command == "buildInfo":
68+
return {"modules": []}
69+
raise Exception("Unexpected command")
70+
71+
def test_supported_on_atlas(self):
72+
"""Supported on MongoDB 7.0+ Atlas replica set or sharded cluster."""
73+
with (
74+
patch(
75+
"pymongo.synchronous.database.Database.command", wraps=self.non_enterprise_response
76+
),
77+
patch("django.db.connection.features.supports_atlas_search", True),
78+
patch("django.db.connection.features._supports_transactions", True),
79+
patch("django.db.connection.features.is_mongodb_7_0", True),
80+
):
81+
self.assertIs(connection.features.supports_queryable_encryption, True)
82+
83+
def test_supported_on_enterprise(self):
84+
"""Supported on MongoDB 7.0+ Enterprise replica set or sharded cluster."""
85+
with (
86+
patch("pymongo.synchronous.database.Database.command", wraps=self.enterprise_response),
87+
patch("django.db.connection.features.supports_atlas_search", False),
88+
patch("django.db.connection.features._supports_transactions", True),
89+
patch("django.db.connection.features.is_mongodb_7_0", True),
90+
):
91+
self.assertIs(connection.features.supports_queryable_encryption, True)
92+
93+
def test_atlas_or_enterprise_required(self):
94+
"""Not supported on MongoDB Community Edition."""
95+
with (
96+
patch(
97+
"pymongo.synchronous.database.Database.command", wraps=self.non_enterprise_response
98+
),
99+
patch("django.db.connection.features.supports_atlas_search", False),
100+
patch("django.db.connection.features._supports_transactions", True),
101+
patch("django.db.connection.features.is_mongodb_7_0", True),
102+
):
103+
self.assertIs(connection.features.supports_queryable_encryption, True)
104+
105+
def test_transactions_required(self):
106+
"""
107+
Not supported if database isn't a replica set or sharded cluster
108+
(i.e. DatabaseFeatures._supports_transactions = False).
109+
"""
110+
with (
111+
patch("pymongo.synchronous.database.Database.command", wraps=self.enterprise_response),
112+
patch("django.db.connection.features.supports_atlas_search", False),
113+
patch("django.db.connection.features._supports_transactions", False),
114+
patch("django.db.connection.features.is_mongodb_7_0", True),
115+
):
116+
self.assertIs(connection.features.supports_queryable_encryption, False)
117+
118+
def test_mongodb_7_0_required(self):
119+
"""Not supported on MongoDB < 7.0"""
120+
with (
121+
patch("pymongo.synchronous.database.Database.command", wraps=self.enterprise_response),
122+
patch("django.db.connection.features.supports_atlas_search", False),
123+
patch("django.db.connection.features._supports_transactions", True),
124+
patch("django.db.connection.features.is_mongodb_7_0", False),
125+
):
126+
self.assertIs(connection.features.supports_queryable_encryption, False)

0 commit comments

Comments
 (0)