Skip to content

Commit a6d0bef

Browse files
committed
[wip] add QuerySet tests
1 parent 479bf87 commit a6d0bef

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

django_mongodb_backend/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def wrapper(*args, **kwargs):
2424
except DuplicateKeyError as e:
2525
raise IntegrityError from e
2626
except PyMongoError as e:
27-
raise DatabaseError from e
27+
raise DatabaseError(str(e)) from e
2828

2929
return wrapper
3030

tests/encryption_/test_fields.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from decimal import Decimal
44

55
from bson import ObjectId
6+
from django.db import DatabaseError
7+
from django.db.models import Avg
68

79
from django_mongodb_backend.fields import (
810
EncryptedArrayField,
@@ -214,6 +216,65 @@ def test_time(self):
214216
self.assertEncrypted(TimeModel, "value")
215217

216218

219+
class QueryTests(EncryptionTestCase):
220+
def test_annotate(self):
221+
msg = (
222+
"Cannot group on field '_id.value' which is encrypted with the "
223+
"random algorithm or whose encryption properties are not known "
224+
"until runtime"
225+
)
226+
with self.assertRaisesMessage(DatabaseError, msg):
227+
list(IntegerModel.objects.annotate(avg=Avg("value")))
228+
229+
def test_count(self):
230+
msg = (
231+
"Aggregation stage $internalFacetTeeConsumer is not allowed or "
232+
"supported with automatic encryption."
233+
)
234+
with self.assertRaisesMessage(DatabaseError, msg):
235+
list(CharModel.objects.count())
236+
237+
def test_dates(self):
238+
msg = (
239+
"If the value type is a date, the type of the index must also be date (and vice versa)."
240+
)
241+
with self.assertRaisesMessage(DatabaseError, msg):
242+
list(DateModel.objects.dates("value", "year"))
243+
244+
def test_datetimes(self):
245+
msg = (
246+
"If the value type is a date, the type of the index must also be date (and vice versa)."
247+
)
248+
with self.assertRaisesMessage(DatabaseError, msg):
249+
list(DateTimeModel.objects.datetimes("value", "year"))
250+
251+
def test_exists(self):
252+
self.assertIs(CharModel.objects.exists(), False)
253+
254+
def test_order_by(self):
255+
msg = "Cannot add an encrypted field as a prefix of another encrypted field"
256+
with self.assertRaisesMessage(DatabaseError, msg):
257+
list(CharModel.objects.order_by("value"))
258+
259+
def test_values(self):
260+
list(CharModel.objects.values("value"))
261+
262+
def test_values_list(self):
263+
list(CharModel.objects.values_list("value"))
264+
265+
def test_distinct(self):
266+
msg = (
267+
"Cannot group on field '_id.value' which is encrypted with the "
268+
"random algorithm or whose encryption properties are not known "
269+
"until runtime"
270+
)
271+
with self.assertRaisesMessage(DatabaseError, msg):
272+
list(CharModel.objects.distinct("value"))
273+
274+
def test_only(self):
275+
list(CharModel.objects.only("value"))
276+
277+
217278
class FieldMixinTests(EncryptionTestCase):
218279
def test_db_index(self):
219280
msg = "'db_index=True' is not supported on encrypted fields."

0 commit comments

Comments
 (0)