def _enable_transactions(*args, **kwargs): restore_transaction_methods() try: ret = func(*args, **kwargs) except: raise finally: disable_transaction_methods() return ret
def _fixture_teardown(self): """Finds an attribute called :attr:`data` and runs teardown on it (data is created by :meth:`_fixture_setup`) """ if hasattr(self, 'data'): self.data.teardown() if self._is_transaction_supported: testcases.restore_transaction_methods() transaction.rollback() transaction.leave_transaction_management() connection.close()
def _fixture_teardown(self): """Finds an attribute called :attr:`data` and runs teardown on it (data is created by :meth:`_fixture_setup`) """ if hasattr(self, 'data'): self.data.teardown() if check_supports_transactions(connection): testcases.restore_transaction_methods() transaction.rollback() transaction.leave_transaction_management() connection.close()
def teardown_database(self, transactions = True): if not transactions: try: restore_transaction_methods() except: pass try: transaction.rollback() except: pass try: transaction.leave_transaction_management() except: pass
def __exit__(self, exc_type, exc_value, traceback): databases = connections restore_transaction_methods() for db in databases: transaction.rollback(using=db) if transaction.is_managed(using=db): transaction.leave_transaction_management(using=db) else: print >> sys.stderr, "---> Unable to rollback transaction managent on %r" % db for connection in connections.all(): connection.close()
def _post_teardown(self): if connections_support_transactions(): # If the test case has a multi_db=True flag, teardown all databases. # Otherwise, just teardown default. if getattr(self, 'multi_db', False): databases = connections else: databases = [DEFAULT_DB_ALIAS] restore_transaction_methods() for db in databases: transaction.rollback(using=db) transaction.leave_transaction_management(using=db) for connection in connections.all(): connection.close()
def afterTest(self, test): if not self._apply(test): return # After each test, restore transaction support, and rollback # the current test's transaction. If transactions are not # available, truncate all tables. transaction_support = self._has_transaction_support(test) if transaction_support: restore_transaction_methods() transaction.rollback() transaction.leave_transaction_management() # If connection is not closed Postgres can go wild with # character encodings. connection.close() else: call_command('flush', verbosity=0, interactive=False)
def pytest_runtest_teardown(item): """ Per-test teardown. Roll back the Django ORM transaction and truncates tables in the test PerformanceTestModel database. """ from django.test.testcases import restore_transaction_methods from django.db import transaction from datazilla.model import PerformanceTestModel restore_transaction_methods() transaction.rollback() transaction.leave_transaction_management() ptm = PerformanceTestModel(item.session.perftest_name) truncate(ptm, set(['metric', 'metric_value']))
def pytest_runtest_teardown(self, item): item_obj = self._get_item_obj(item) # This is a Django unittest TestCase if self._is_unittest(item_obj): item_obj.tearDown() item_obj._post_teardown() return elif settings.DATABASE_SUPPORTS_TRANSACTIONS: restore_transaction_methods() transaction.rollback() try: transaction.leave_transaction_management() except transaction.TransactionManagementError: pass connection.close() if hasattr(item, "urls") and self._old_urlconf is not None: settings.ROOT_URLCONF = self._old_urlconf self._old_urlconf = None
def test_rolls_back_and_reraises_if_get_object_raises_with_uncommitted_changes( self, mock_get_object_or_404 ): try: # django.test.TestCase replaces the transaction management functions with nops, # which is generally useful but breaks this test, as we're checking that the # exception we raise isn't masked by one in the leave_transaction_management # saying that there was a pending commit/rollback when the view returned. restore_transaction_methods() transaction.commit() expected_exception = Exception("Expected exception") def set_dirty_and_raise_exception(*args, **kwargs): transaction.set_dirty() raise expected_exception mock_get_object_or_404.side_effect = set_dirty_and_raise_exception try: calculate_and_get_json_for_api(self.request, self.user.username, self.sheet.id) except Exception, e: self.assertEquals(e, expected_exception) else:
def rollback_django_transaction(scenario): restore_transaction_methods() transaction.rollback() transaction.leave_transaction_management()
def tearDownClass(cls): super(OptimizedTestCase, cls).tearDownClass() restore_transaction_methods() transaction.rollback(using='default') transaction.leave_transaction_management(using='default')
def tearDownClass(cls): restore_transaction_methods() transaction.rollback(using=DEFAULT_DB_ALIAS) transaction.leave_transaction_management(using=DEFAULT_DB_ALIAS) for connection in connections.all(): connection.close()
def _end_test_transaction(): restore_transaction_methods() if connections_support_transactions(): transaction.rollback(using='default') transaction.leave_transaction_management(using='default')