diff options
author | Florian Apolloner <florian@apolloner.eu> | 2013-09-24 10:27:43 +0200 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2013-09-24 10:29:21 +0200 |
commit | 16ceb05b4751425ed16ee0eddd0c533f067344b2 (patch) | |
tree | 64713086fa304acb8d3a9bab466bff339537fbc3 /tests/multiple_database | |
parent | 4c5bc1ac30fea526259026bf762b4806dd5f1459 (diff) | |
download | django-16ceb05b4751425ed16ee0eddd0c533f067344b2.tar.gz |
Isolated select_for_update tests a bit more.
This change prevents including the multiple_database test models without
duplicating the router code (we probably should do this at one point).
Refs #21148
Diffstat (limited to 'tests/multiple_database')
-rw-r--r-- | tests/multiple_database/routers.py | 61 | ||||
-rw-r--r-- | tests/multiple_database/tests.py | 56 |
2 files changed, 62 insertions, 55 deletions
diff --git a/tests/multiple_database/routers.py b/tests/multiple_database/routers.py new file mode 100644 index 0000000000..5d15b5d6b0 --- /dev/null +++ b/tests/multiple_database/routers.py @@ -0,0 +1,61 @@ +from __future__ import unicode_literals + +from django.db import DEFAULT_DB_ALIAS + + +class TestRouter(object): + # A test router. The behavior is vaguely master/slave, but the + # databases aren't assumed to propagate changes. + def db_for_read(self, model, instance=None, **hints): + if instance: + return instance._state.db or 'other' + return 'other' + + def db_for_write(self, model, **hints): + return DEFAULT_DB_ALIAS + + def allow_relation(self, obj1, obj2, **hints): + return obj1._state.db in ('default', 'other') and obj2._state.db in ('default', 'other') + + def allow_migrate(self, db, model): + return True + + +class AuthRouter(object): + """A router to control all database operations on models in + the contrib.auth application""" + + def db_for_read(self, model, **hints): + "Point all read operations on auth models to 'default'" + if model._meta.app_label == 'auth': + # We use default here to ensure we can tell the difference + # between a read request and a write request for Auth objects + return 'default' + return None + + def db_for_write(self, model, **hints): + "Point all operations on auth models to 'other'" + if model._meta.app_label == 'auth': + return 'other' + return None + + def allow_relation(self, obj1, obj2, **hints): + "Allow any relation if a model in Auth is involved" + if obj1._meta.app_label == 'auth' or obj2._meta.app_label == 'auth': + return True + return None + + def allow_migrate(self, db, model): + "Make sure the auth app only appears on the 'other' db" + if db == 'other': + return model._meta.app_label == 'auth' + elif model._meta.app_label == 'auth': + return False + return None + + +class WriteRouter(object): + # A router that only expresses an opinion on writes + def db_for_write(self, model, **hints): + return 'writer' + diff --git a/tests/multiple_database/tests.py b/tests/multiple_database/tests.py index b1c0978385..8a31f5f530 100644 --- a/tests/multiple_database/tests.py +++ b/tests/multiple_database/tests.py @@ -15,6 +15,7 @@ from django.test.utils import override_settings from django.utils.six import StringIO from .models import Book, Person, Pet, Review, UserProfile +from .routers import TestRouter, AuthRouter, WriteRouter class QueryTestCase(TestCase): @@ -919,61 +920,6 @@ class QueryTestCase(TestCase): extra_arg=True) -class TestRouter(object): - # A test router. The behavior is vaguely master/slave, but the - # databases aren't assumed to propagate changes. - def db_for_read(self, model, instance=None, **hints): - if instance: - return instance._state.db or 'other' - return 'other' - - def db_for_write(self, model, **hints): - return DEFAULT_DB_ALIAS - - def allow_relation(self, obj1, obj2, **hints): - return obj1._state.db in ('default', 'other') and obj2._state.db in ('default', 'other') - - def allow_migrate(self, db, model): - return True - -class AuthRouter(object): - """A router to control all database operations on models in - the contrib.auth application""" - - def db_for_read(self, model, **hints): - "Point all read operations on auth models to 'default'" - if model._meta.app_label == 'auth': - # We use default here to ensure we can tell the difference - # between a read request and a write request for Auth objects - return 'default' - return None - - def db_for_write(self, model, **hints): - "Point all operations on auth models to 'other'" - if model._meta.app_label == 'auth': - return 'other' - return None - - def allow_relation(self, obj1, obj2, **hints): - "Allow any relation if a model in Auth is involved" - if obj1._meta.app_label == 'auth' or obj2._meta.app_label == 'auth': - return True - return None - - def allow_migrate(self, db, model): - "Make sure the auth app only appears on the 'other' db" - if db == 'other': - return model._meta.app_label == 'auth' - elif model._meta.app_label == 'auth': - return False - return None - -class WriteRouter(object): - # A router that only expresses an opinion on writes - def db_for_write(self, model, **hints): - return 'writer' - - class ConnectionRouterTestCase(TestCase): @override_settings(DATABASE_ROUTERS=[ 'multiple_database.tests.TestRouter', |