Skip to content

Commit 27d4b8e

Browse files
committed
Move encrypted router to tests
1 parent b2be223 commit 27d4b8e

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

django_mongodb_backend/routers.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,3 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
1616
except LookupError:
1717
return None
1818
return False if issubclass(model, EmbeddedModel) else None
19-
20-
21-
class EncryptionRouter:
22-
"""
23-
Routes database operations for 'encrypted' models to the 'encryption' DB.
24-
"""
25-
26-
def db_for_read(self, model, **hints):
27-
if getattr(model, "encrypted_fields_map", False):
28-
return "encryption"
29-
return None
30-
31-
def db_for_write(self, model, **hints):
32-
if getattr(model, "encrypted_fields_map", False):
33-
return "encryption"
34-
return None
35-
36-
def allow_migrate(self, db, app_label, model_name=None, **hints):
37-
"""
38-
Ensure that the 'encrypted' models only appear in the 'encryption' DB,
39-
and not in the default DB.
40-
"""
41-
model = hints.get("model")
42-
if model and getattr(model, "encrypted_fields_map", False):
43-
return db == "encryption"
44-
return None

tests/encryption_/routers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from django.db import connection
2+
3+
4+
class EncryptedRouter:
5+
"""
6+
Routes database operations for encrypted models to the encrypted DB.
7+
"""
8+
9+
def db_for_read(self, model, **hints):
10+
with connection.schema_editor() as editor:
11+
if model and getattr(editor._get_encrypted_fields_map(model), False):
12+
return "encrypted"
13+
return None
14+
15+
def db_for_write(self, model, **hints):
16+
with connection.schema_editor() as editor:
17+
if model and getattr(editor._get_encrypted_fields_map(model), False):
18+
return "encrypted"
19+
return None
20+
21+
def allow_migrate(self, db, app_label, model_name=None, **hints):
22+
model = hints.get("model")
23+
with connection.schema_editor() as editor:
24+
if model and getattr(editor._get_encrypted_fields_map(model), False):
25+
return db == "encrypted"
26+
return None

tests/encryption_/tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from django.db import connection
2-
from django.test import TestCase
2+
from django.test import TestCase, override_settings
33

44
from .models import Person
5+
from .routers import EncryptedRouter
56

67

8+
@override_settings(DATABASE_ROUTERS=[EncryptedRouter])
79
class EncryptedModelTests(TestCase):
810
@classmethod
911
def setUpTestData(cls):

0 commit comments

Comments
 (0)