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 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.")
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()
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()
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 check_supports_transactions(connection): 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 test_schema_conformance(): valid_rels = ValidNoRelationsData() invalid_rels = InvalidNoRelationsData() class NoRelations(django_models.Model): char = django_models.CharField(max_length=10) num = django_models.IntegerField() for dataset, model, callable in \ [ (valid_rels, NoRelations, _check_row), (invalid_rels, NoRelations, raises(ValueError)(_check_row)) ]: djm = DjangoFixture.DjangoMedium(NoRelations, dataset) rows = [(row[0], list(get_column_vals(row[1]))) for row in dataset] for row in rows: callable.description = "schema.conformance: %s %s in %s" % ( row[0], row[1], dataset.__class__.__name__) yield callable, djm, row[1]
from fixture import DataSet, DjangoFixture from fixture.style import NamedDataStyle from fixture.django_testcase import FixtureTestCase from datetime import datetime from django.contrib.auth.models import User from fixture.examples.django_example.blog.models import Post, Category from fixture.examples.django_example.blog.datasets.blog_data import ( UserData, PostData, CategoryData) db_fixture = DjangoFixture(style=NamedDataStyle()) class TestBlogWithData(FixtureTestCase): fixture = db_fixture datasets = [PostData] def test_blog_posts(self): self.assertEquals(Post.objects.all().count(), 3, "There are 3 blog posts") post = Post.objects.get(title=PostData.third_post.title) self.assertEquals(post.categories.count(), 2, "The 3rd test post is in 2 categories") def test_posts_by_category(self): py = Category.objects.get(title=CategoryData.python.title) self.assertEquals(py.post_set.count(), 3, "There are 3 posts in python category") def test_posts_by_author(self): ben = User.objects.get(username=UserData.ben.username) self.assertEquals(ben.post_set.all().count(), 3,
def setUpTestData(cls): super(FixtureTestCase, cls).setUpTestData() fixture = DjangoFixture() cls.data = fixture.data(*cls.datasets) cls.data.setup()
class BookData(DataSet): class dune: title = "Dune" reviewers = [ReviewerData.ben] class AuthorData(DataSet): class frank_herbert: first_name = "Frank" last_name = "Herbert" books = BookData.dune dj_fixture = DjangoFixture(env=models, style=style.NamedDataStyle()) def test_wrong_relation_declaration(): assert 'reviewers' in models.Book._meta.get_all_field_names() data = dj_fixture.data(BookData) assert_raises(LoadError, data.setup) data.teardown() def test_invalid_m2m(): class ReviewerData(DataSet): class ben: name = 'ben' reviewed = [BookData.dune, AuthorData.frank_herbert]
def setUpTestData(cls): fixture = DjangoFixture() data = fixture.data(*cls.datasets) data.setup()
from app.models import Author, Book, Reviewer from fixture import DjangoFixture from fixture.test.test_loadable.test_django.util import assert_empty from fixture.test.test_loadable.test_django.fixtures import DjangoDataSetWithMeta, AuthorData, BookData, ReviewerData dj_fixture = DjangoFixture() def test_fk_rels(): assert_empty('app') try: data = dj_fixture.data(AuthorData, BookData) data.setup() assert Author.objects.get(first_name='Frank').books.count() == 1 finally: data.teardown() assert_empty('app') def test_m2m(): assert_empty('app') try: data = dj_fixture.data(AuthorData, BookData, ReviewerData) data.setup() ben = Reviewer.objects.all()[0] # Reviewed have been added as a list assert ben.reviewed.count() == 2 dune = Book.objects.get(title='Dune') # Reverse relations work assert ben in dune.reviewers.all() # A single object passed to a many to many also works