Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 16ceb05b4751425ed16ee0eddd0c533f067344b2 1 parent 4c5bc1a
Florian Apolloner authored September 24, 2013
61  tests/multiple_database/routers.py
... ...
@@ -0,0 +1,61 @@
  1
+from __future__ import unicode_literals
  2
+
  3
+from django.db import DEFAULT_DB_ALIAS
  4
+
  5
+
  6
+class TestRouter(object):
  7
+    # A test router. The behavior is vaguely master/slave, but the
  8
+    # databases aren't assumed to propagate changes.
  9
+    def db_for_read(self, model, instance=None, **hints):
  10
+        if instance:
  11
+            return instance._state.db or 'other'
  12
+        return 'other'
  13
+
  14
+    def db_for_write(self, model, **hints):
  15
+        return DEFAULT_DB_ALIAS
  16
+
  17
+    def allow_relation(self, obj1, obj2, **hints):
  18
+        return obj1._state.db in ('default', 'other') and obj2._state.db in ('default', 'other')
  19
+
  20
+    def allow_migrate(self, db, model):
  21
+        return True
  22
+
  23
+
  24
+class AuthRouter(object):
  25
+    """A router to control all database operations on models in
  26
+    the contrib.auth application"""
  27
+
  28
+    def db_for_read(self, model, **hints):
  29
+        "Point all read operations on auth models to 'default'"
  30
+        if model._meta.app_label == 'auth':
  31
+            # We use default here to ensure we can tell the difference
  32
+            # between a read request and a write request for Auth objects
  33
+            return 'default'
  34
+        return None
  35
+
  36
+    def db_for_write(self, model, **hints):
  37
+        "Point all operations on auth models to 'other'"
  38
+        if model._meta.app_label == 'auth':
  39
+            return 'other'
  40
+        return None
  41
+
  42
+    def allow_relation(self, obj1, obj2, **hints):
  43
+        "Allow any relation if a model in Auth is involved"
  44
+        if obj1._meta.app_label == 'auth' or obj2._meta.app_label == 'auth':
  45
+            return True
  46
+        return None
  47
+
  48
+    def allow_migrate(self, db, model):
  49
+        "Make sure the auth app only appears on the 'other' db"
  50
+        if db == 'other':
  51
+            return model._meta.app_label == 'auth'
  52
+        elif model._meta.app_label == 'auth':
  53
+            return False
  54
+        return None
  55
+
  56
+
  57
+class WriteRouter(object):
  58
+    # A router that only expresses an opinion on writes
  59
+    def db_for_write(self, model, **hints):
  60
+        return 'writer'
  61
+
56  tests/multiple_database/tests.py
@@ -15,6 +15,7 @@
15 15
 from django.utils.six import StringIO
16 16
 
17 17
 from .models import Book, Person, Pet, Review, UserProfile
  18
+from .routers import TestRouter, AuthRouter, WriteRouter
18 19
 
19 20
 
20 21
 class QueryTestCase(TestCase):
@@ -919,61 +920,6 @@ def test_related_manager(self):
919 920
                                   extra_arg=True)
920 921
 
921 922
 
922  
-class TestRouter(object):
923  
-    # A test router. The behavior is vaguely master/slave, but the
924  
-    # databases aren't assumed to propagate changes.
925  
-    def db_for_read(self, model, instance=None, **hints):
926  
-        if instance:
927  
-            return instance._state.db or 'other'
928  
-        return 'other'
929  
-
930  
-    def db_for_write(self, model, **hints):
931  
-        return DEFAULT_DB_ALIAS
932  
-
933  
-    def allow_relation(self, obj1, obj2, **hints):
934  
-        return obj1._state.db in ('default', 'other') and obj2._state.db in ('default', 'other')
935  
-
936  
-    def allow_migrate(self, db, model):
937  
-        return True
938  
-
939  
-class AuthRouter(object):
940  
-    """A router to control all database operations on models in
941  
-    the contrib.auth application"""
942  
-
943  
-    def db_for_read(self, model, **hints):
944  
-        "Point all read operations on auth models to 'default'"
945  
-        if model._meta.app_label == 'auth':
946  
-            # We use default here to ensure we can tell the difference
947  
-            # between a read request and a write request for Auth objects
948  
-            return 'default'
949  
-        return None
950  
-
951  
-    def db_for_write(self, model, **hints):
952  
-        "Point all operations on auth models to 'other'"
953  
-        if model._meta.app_label == 'auth':
954  
-            return 'other'
955  
-        return None
956  
-
957  
-    def allow_relation(self, obj1, obj2, **hints):
958  
-        "Allow any relation if a model in Auth is involved"
959  
-        if obj1._meta.app_label == 'auth' or obj2._meta.app_label == 'auth':
960  
-            return True
961  
-        return None
962  
-
963  
-    def allow_migrate(self, db, model):
964  
-        "Make sure the auth app only appears on the 'other' db"
965  
-        if db == 'other':
966  
-            return model._meta.app_label == 'auth'
967  
-        elif model._meta.app_label == 'auth':
968  
-            return False
969  
-        return None
970  
-
971  
-class WriteRouter(object):
972  
-    # A router that only expresses an opinion on writes
973  
-    def db_for_write(self, model, **hints):
974  
-        return 'writer'
975  
-
976  
-
977 923
 class ConnectionRouterTestCase(TestCase):
978 924
     @override_settings(DATABASE_ROUTERS=[
979 925
         'multiple_database.tests.TestRouter',
2  tests/select_for_update/tests.py
@@ -10,7 +10,7 @@
10 10
 from django.test import (TransactionTestCase, skipIfDBFeature,
11 11
     skipUnlessDBFeature)
12 12
 
13  
-from multiple_database.tests import TestRouter
  13
+from multiple_database.routers import TestRouter
14 14
 
15 15
 from .models import Person
16 16
 

0 notes on commit 16ceb05

Please sign in to comment.
Something went wrong with that request. Please try again.