|
3 | 3 | from decimal import Decimal |
4 | 4 |
|
5 | 5 | from bson import ObjectId |
| 6 | +from django.db import DatabaseError |
| 7 | +from django.db.models import Avg |
6 | 8 |
|
7 | 9 | from django_mongodb_backend.fields import ( |
8 | 10 | EncryptedArrayField, |
@@ -214,6 +216,65 @@ def test_time(self): |
214 | 216 | self.assertEncrypted(TimeModel, "value") |
215 | 217 |
|
216 | 218 |
|
| 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 | + |
217 | 278 | class FieldMixinTests(EncryptionTestCase): |
218 | 279 | def test_db_index(self): |
219 | 280 | msg = "'db_index=True' is not supported on encrypted fields." |
|
0 commit comments