Skip to content

Commit

Permalink
Fix #208 - do not assume active DB when not specified (#210).
Browse files Browse the repository at this point in the history
* Do not assume active DB when not specified

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add db routing test case

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* one more test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* use assertRaises instead of expectedFailure

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Catch a general exception for  django < 2.0

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* use `with` instead of callable

* skip test for django < 2.0

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* remove repeated test

Co-authored-by: vaz <vmohan@lenbox.io>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 26, 2022
1 parent 585bf7a commit 23511d0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
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()

0 comments on commit 23511d0

Please sign in to comment.