-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
585bf7a
commit 23511d0
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |