def setup(self): """ Creates or sets up test database name, loads fixtures and mocks ``SMTPConnection`` class from ``django.core.mail``. """ # Creates test database create_test_db = True settings.original_DATABASE_ENGINE = settings.DATABASE_ENGINE settings.original_DATABASE_NAME = settings.DATABASE_NAME if self.database_name is None or self.database_name == ':memory:': # Closes current database connection and changes # ``DATABASE_ENGINE`` to ``'sqlite3'`` connection.close() settings.DATABASE_ENGINE = 'sqlite3' settings.TEST_DATABASE_NAME = ':memory:' # Flushes in-memory database if it exists if db_exists(settings.TEST_DATABASE_NAME): flush() elif self.database_name == ':original:': # Disable database flush by default if self.database_flush is None: self.database_flush = False self.database_name = settings.DATABASE_NAME settings.TEST_DATABASE_NAME = settings.DATABASE_NAME # If original database not exist - tries to create it if db_exists(self.database_name): create_test_db = False else: settings.TEST_DATABASE_NAME = self.database_name # Do not re-creates tests database if it exists on # ``database_flush == False`` if self.database_flush == False and \ db_exists(self.database_name): create_test_db = False if create_test_db: self.database_name = \ connection.creation.create_test_db(autoclobber=True) tables = connection.introspection.table_names() if self.database_flush and tables: call_command('flush', interactive=False) # Load data from fixtures if self.fixtures: call_command('loaddata', *self.fixtures) # Mock original SMTPConnection mail.original_SMTPConnection = mail.SMTPConnection mail.SMTPConnection = TestSMTPConnection mail.outbox = []
def teardown(self): # Destroys test database if self.database_name == ":memory:" and self.database_flush != False: flush() if self.database_name != settings.original_DATABASE_NAME and self.database_flush != False: connection.creation.destroy_test_db(self.database_name) settings.DATABASE_ENGINE = settings.original_DATABASE_ENGINE settings.DATABASE_NAME = settings.original_DATABASE_NAME # Unmock original SMTPConnection mail.SMTPConnection = mail.original_SMTPConnection del mail.original_SMTPConnection del mail.outbox
def teardown(self): # Destroys test database if self.database_name == ':memory:' and self.database_flush != False: flush() if self.database_name != settings.original_DATABASE_NAME and \ self.database_flush != False: connection.creation.destroy_test_db(self.database_name) settings.DATABASE_ENGINE = settings.original_DATABASE_ENGINE settings.DATABASE_NAME = settings.original_DATABASE_NAME # Unmock original SMTPConnection mail.SMTPConnection = mail.original_SMTPConnection del mail.original_SMTPConnection del mail.outbox if not self.old_installed_apps == settings.INSTALLED_APPS: settings.INSTALLED_APPS = self.old_installed_apps