Skip to content

Commit 59865f7

Browse files
committed
Add tests for EncryptedFieldMixin
1 parent 86486eb commit 59865f7

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

django_mongodb_backend/fields/encryption.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def deconstruct(self):
2525
if self.queries is not None:
2626
kwargs["queries"] = self.queries
2727

28-
if path.startswith("django_mongodb_backend.fields.encrypted_model"):
28+
if path.startswith("django_mongodb_backend.fields.encryption"):
2929
path = path.replace(
30-
"django_mongodb_backend.fields.encrypted_model",
30+
"django_mongodb_backend.fields.encryption",
3131
"django_mongodb_backend.fields",
3232
)
3333

tests/encryption_/test_fields.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import datetime
22
from decimal import Decimal
33

4+
from django_mongodb_backend.fields import EncryptedCharField
5+
46
from .models import (
57
Billing,
68
EncryptedBigIntegerTest,
@@ -44,8 +46,6 @@ def test_patient(self):
4446

4547

4648
class EncryptedFieldTests(EncryptionTestCase):
47-
databases = {"default", "encrypted"}
48-
4949
def assertEquality(self, model_cls, val):
5050
model_cls.objects.create(value=val)
5151
fetched = model_cls.objects.get(value=val)
@@ -147,3 +147,37 @@ def test_time(self):
147147
high=datetime.time(15, 0),
148148
threshold=datetime.time(12, 0),
149149
)
150+
151+
152+
class EncryptedFieldMixinTests(EncryptionTestCase):
153+
def test_null_true_raises_error(self):
154+
with self.assertRaisesMessage(
155+
ValueError, "'null=True' is not supported for encrypted fields."
156+
):
157+
EncryptedCharField(max_length=50, null=True)
158+
159+
def test_deconstruct_preserves_queries_and_rewrites_path(self):
160+
field = EncryptedCharField(max_length=50, queries={"field": "value"})
161+
field.name = "ssn"
162+
name, path, args, kwargs = field.deconstruct()
163+
164+
# Name is preserved
165+
self.assertEqual(name, "ssn")
166+
167+
# Path is rewritten from 'encrypted_model' to regular fields path
168+
self.assertEqual(path, "django_mongodb_backend.fields.EncryptedCharField")
169+
170+
# No positional args for CharField
171+
self.assertEqual(args, [])
172+
173+
# Queries value is preserved in kwargs
174+
self.assertIn("queries", kwargs)
175+
self.assertEqual(kwargs["queries"], {"field": "value"})
176+
177+
# Reconstruct from deconstruct output
178+
new_field = EncryptedCharField(*args, **kwargs)
179+
180+
# Reconstructed field is equivalent
181+
self.assertEqual(new_field.queries, field.queries)
182+
self.assertIsNot(new_field, field)
183+
self.assertEqual(new_field.max_length, field.max_length)

0 commit comments

Comments
 (0)