Mike Mazur avatar Mike Mazur committed acbc654

Add Django 1.2 multiple database support

Comments (0)

Files changed (1)

nosedjango/nosedjango.py

         from django.core import mail
         self.mail = mail
         from django.conf import settings
-        from django.core import management
         from django.test.utils import setup_test_environment
-        from django.db import connection
-
-        self.old_db = settings.DATABASE_NAME
 
         # setup the test env for each test case
         setup_test_environment()
+
+        if not settings.DATABASES: # pre-Django 1.2
+            self.setup_single_test_db(settings)
+        else:
+            self.setup_multi_test_db(settings)
+
+        # exit the setup phase and let nose do it's thing
+
+    def setup_single_test_db(self, settings):
+        from django.db import connection
+        self.old_db = settings.DATABASE_NAME
         connection.creation.create_test_db(verbosity=self.verbosity)
 
-        # exit the setup phase and let nose do it's thing
-            
+    def setup_multi_test_db(self, settings):
+        from django.db import connections
+        old_names = []
+        mirrors = []
+        for alias in connections:
+            connection = connections[alias]
+            # If the database is a test mirror, redirect it's connection
+            # instead of creating a test database.
+            if connection.settings_dict['TEST_MIRROR']:
+                mirrors.append((alias, connection))
+                mirror_alias = connection.settings_dict['TEST_MIRROR']
+                connections._connections[alias] = connections[mirror_alias]
+            else:
+                old_names.append((connection, connection.settings_dict['NAME']))
+                connection.creation.create_test_db(verbosity=self.verbosity)
+        self.old_db = (old_names, mirrors)
+
     def beforeTest(self, test):
 
         if not SETTINGS_PATH:
             return
 
         from django.test.utils import teardown_test_environment
-        from django.db import connection
         from django.conf import settings
-        connection.creation.destroy_test_db(self.old_db, verbosity=self.verbosity)   
+
+        if not settings.DATABASES: # pre-Django 1.2
+            self.teardown_single_test_db()
+        else:
+            self.teardown_multi_test_db()
+
         teardown_test_environment()
 
         if hasattr(self, 'old_urlconf'):
             settings.ROOT_URLCONF = self.old_urlconf
             clear_url_caches()
 
+    def teardown_single_test_db(self):
+        from django.db import connection
+        connection.creation.destroy_test_db(self.old_db, verbosity=self.verbosity)
+
+    def teardown_multi_test_db(self):
+        from django.db import connections
+        old_names, mirrors = self.old_db
+        # Point all the mirrors back to the originals
+        for alias, connection in mirrors:
+            connections._connections[alias] = connection
+        # Destroy all the non-mirror databases
+        for connection, old_name in old_names:
+            connection.creation.destroy_test_db(old_name, self.verbosity)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.