Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not assume active DB when not specified #210

Merged
merged 14 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions admin_interface/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ def pre_save_handler(instance, **kwargs):
pass

@staticmethod
def get_active_theme(database="default"):
objs_manager = Theme.objects.using(database)
def get_active_theme(database=None):
objs_manager = (
Theme.objects if database is None else Theme.objects.using(database)
)
objs_active_qs = objs_manager.filter(active=True)
objs_active_ls = list(objs_active_qs)
objs_active_count = len(objs_active_ls)
Expand Down
45 changes: 45 additions & 0 deletions tests/routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
DATABASE_APPS_MAPPING = {
"admin_interface": "default",
}


class DatabaseAppsRouter(object):
"""
arouter to control all database operations on models for different
databases.

in case an app is not set in DATABASE_APPS_MAPPING, the router
will fallback to the `default` database.

Settings example:

DATABASE_APPS_MAPPING = {'app1': 'db1', 'app2': 'db2'}
"""

def __init__(self, db_map=DATABASE_APPS_MAPPING):
"""
If routers is not specified, default to DATABASE_APPS_MAPPING
"""
self.db_map = db_map

def db_for_read(self, model, **hints):
"""Point all read operations to the specific database"""
if model._meta.app_label in self.db_map:
return self.db_map[model._meta.app_label]

return None

def db_for_write(self, model, **hints):
"""Point all write operations to the specific database"""
if model._meta.app_label in self.db_map:
return self.db_map[model._meta.app_label]

return None

def allow_relation(self, obj1, obj2, **hints):
"""Allow any relation between apps that use the same database"""
return None

def allow_migrate(self, db, app_label, model_name=None, **hints):
"""Make sure that apps only appear in the related database"""
return None
3 changes: 3 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
"replica": database_config.get(replica_engine),
}


DATABASE_ROUTERS = ["tests.routers.DatabaseAppsRouter"]

USE_I18N = True
LANGUAGES = (
("en", "English"),
Expand Down
40 changes: 40 additions & 0 deletions tests/test_multidb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest import skipIf

from django import VERSION
from django.test import TestCase

from admin_interface.models import Theme

from .routers import DatabaseAppsRouter


class AdminInterfaceModelsWithDBRoutingTestCase(TestCase):
databases = ["replica"]

def test_standard_dbrouter(self):
router = DatabaseAppsRouter()
db_for_theme = router.db_for_read(Theme)
assert db_for_theme == "default"

def test_dbrouter_selects_correct_db(self):
DATABASE_APPS_MAPPING = {
"admin_interface": "replica",
}
router = DatabaseAppsRouter(db_map=DATABASE_APPS_MAPPING)
db_for_theme = router.db_for_read(Theme)
assert db_for_theme == "replica"

@skipIf(
VERSION[0] < 2, "TestCase does not respect database param on older versions"
)
def test_dbrouter_errors_when_fetching_from_default(self):
with self.assertRaises(Exception):
Theme.get_active_theme()

def test_dbrouter_fetches_db(self):
DATABASE_APPS_MAPPING = {
"admin_interface": "replica",
}
router = DatabaseAppsRouter(db_map=DATABASE_APPS_MAPPING)
with self.settings(DATABASE_ROUTERS=[router]):
Theme.get_active_theme()