Package couchdbkit :: Package ext :: Package django :: Module testrunner
[hide private]
[frames] | no frames]

Source Code for Module couchdbkit.ext.django.testrunner

 1  # -*- coding: utf-8 - 
 2  # 
 3  # This file is part of couchdbkit released under the MIT license.  
 4  # See the NOTICE for more information. 
 5   
 6  from django.test.simple import DjangoTestSuiteRunner 
 7  from django.conf import settings 
 8   
 9  from . import loading 
10  from ...exceptions import ResourceNotFound 
11   
12 -class CouchDbKitTestSuiteRunner(DjangoTestSuiteRunner):
13 """ 14 A test suite runner for couchdbkit. This offers the exact same functionality 15 as the default django test suite runner, except that it connects all the couchdbkit 16 django-extended models to a test database. The test database is deleted at the 17 end of the tests. To use this, just add this file to your project and the following 18 line to your settings.py file: 19 20 TEST_RUNNER = 'myproject.testrunner.CouchDbKitTestSuiteRunner' 21 """ 22 23 dbs = [] 24
25 - def get_test_db_name(self, dbname):
26 return "%s_test" % dbname
27
28 - def setup_databases(self, **kwargs):
29 print "overridding the couchdbkit database settings to use a test database!" 30 31 # first pass: just implement this as a monkey-patch to the loading module 32 # overriding all the existing couchdb settings 33 self.dbs = [(app, self.get_test_db_name(url)) for app, url in getattr(settings, "COUCHDB_DATABASES", [])] 34 old_handler = loading.couchdbkit_handler 35 couchdbkit_handler = loading.CouchdbkitHandler(self.dbs) 36 loading.couchdbkit_handler = couchdbkit_handler 37 loading.register_schema = couchdbkit_handler.register_schema 38 loading.get_schema = couchdbkit_handler.get_schema 39 loading.get_db = couchdbkit_handler.get_db 40 41 # register our dbs with the extension document classes 42 for app, value in old_handler.app_schema.items(): 43 for name, cls in value.items(): 44 cls.set_db(loading.get_db(app)) 45 46 47 return super(CouchDbKitTestSuiteRunner, self).setup_databases(**kwargs)
48
49 - def teardown_databases(self, old_config, **kwargs):
50 deleted_databases = [] 51 skipcount = 0 52 for app, item in self.dbs: 53 app_label = app.split('.')[-1] 54 db = loading.get_db(app_label) 55 if db.dbname in deleted_databases: 56 skipcount += 1 57 continue 58 try: 59 db.server.delete_db(db.dbname) 60 deleted_databases.append(db.dbname) 61 print "deleted database %s for %s" % (db.dbname, app_label) 62 except ResourceNotFound: 63 print "database %s not found for %s! it was probably already deleted." % (db.dbname, app_label) 64 if skipcount: 65 print "skipped deleting %s app databases that were already deleted" % skipcount 66 return super(CouchDbKitTestSuiteRunner, self).teardown_databases(old_config, **kwargs)
67