Ejemplo n.º 1
0
class FixtureTestCase(testcases.TransactionTestCase):
    """Overrides django's fixture setup and teardown code to use DataSets.

    Starts a transaction at the begining of a test and rolls it back at the
    end.

    See :ref:`Using Fixture With Django <using-fixture-with-django>` for a complete example.
    """
    def __init__(self, *args, **kwargs):
        super(FixtureTestCase, self).__init__(*args, **kwargs)
        self._is_transaction_supported = check_supports_transactions(connection)

    def _fixture_setup(self):
        """Finds a list called :attr:`datasets` and loads them

        This is done in a transaction if possible.
        I'm not using the settings.DATABASE_SUPPORTS_TRANSACTIONS as I don't
        wnat to assume that :meth:`connection.create_test_db` might not have been
        called
        """
        if self._is_transaction_supported:
            transaction.enter_transaction_management()
            transaction.managed(True)
            testcases.disable_transaction_methods()

        import django
        django.setup()
        from django.contrib.sites.models import Site
        Site.objects.clear_cache()

        if not hasattr(self, 'fixture'):
            self.fixture = DjangoFixture()
        if hasattr(self, 'datasets'):
            self.data = self.fixture.data(*self.datasets)
            self.data.setup()

    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()
Ejemplo n.º 2
0
class FixtureTestCase(testcases.TransactionTestCase):
    """Overrides django's fixture setup and teardown code to use DataSets.

    Starts a transaction at the begining of a test and rolls it back at the
    end.

    See :ref:`Using Fixture With Django <using-fixture-with-django>` for a complete example.
    """
    def __init__(self, *args, **kwargs):
        super(FixtureTestCase, self).__init__(*args, **kwargs)
        self._is_transaction_supported = check_supports_transactions(
            connection)

    def _fixture_setup(self):
        """Finds a list called :attr:`datasets` and loads them

        This is done in a transaction if possible.
        I'm not using the settings.DATABASE_SUPPORTS_TRANSACTIONS as I don't
        wnat to assume that :meth:`connection.create_test_db` might not have been
        called
        """
        if self._is_transaction_supported:
            transaction.enter_transaction_management()
            transaction.managed(True)
            testcases.disable_transaction_methods()

        from django.contrib.sites.models import Site
        Site.objects.clear_cache()

        if not hasattr(self, 'fixture'):
            self.fixture = DjangoFixture()
        if hasattr(self, 'datasets'):
            self.data = self.fixture.data(*self.datasets)
            self.data.setup()

    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()
Ejemplo n.º 3
0
def fill_database(fixtures, verbosity=1):
    """
    Fill all datasets listed in ``fixtures`` into database.
    """
    if not isinstance(fixtures, list):
        raise TypeError("Argument fixtures should be of type list.")
    if not len(fixtures):
        raise ValueError("Argument fixtures is empyt.")

    def echo(msg):
        if verbosity:
            print msg

    names = [c.__name__ for c in fixtures]

    echo("Datasets:\n\t%s" % "\n\t".join(names))
    loader = DjangoFixture(style=NamedDataStyle())
    data = loader.data(*fixtures)
    echo("Installing datasets...")
    data.setup()
    echo("Done.")
Ejemplo n.º 4
0
 def setUpTestData(cls):
     super(FixtureTestCase, cls).setUpTestData()
     fixture = DjangoFixture()
     cls.data = fixture.data(*cls.datasets)
     cls.data.setup()
Ejemplo n.º 5
0
 def setUpTestData(cls):
     super(FixtureTestCase, cls).setUpTestData()
     fixture = DjangoFixture()
     cls.data = fixture.data(*cls.datasets)
     cls.data.setup()
Ejemplo n.º 6
0
 def setUpTestData(cls):
     fixture = DjangoFixture()
     data = fixture.data(*cls.datasets)
     data.setup()