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()
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]
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