Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable FastFixtureTestCase on Django 1.8+ #258

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions django_nose/fixture_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@
import zipfile
from itertools import product

from django.apps import apps
from django.conf import settings
from django.core import serializers
from django.db import router, DEFAULT_DB_ALIAS

try:
from django.db.models import get_apps
except ImportError:
from django.apps import apps

def get_apps():
"""Emulate get_apps in Django 1.9 and later."""
return [a.models_module for a in apps.get_app_configs()]
def get_apps():
"""Emulate get_apps in Django 1.9 and later."""
return [a.models_module for a in apps.get_app_configs()]

try:
import bz2
Expand Down Expand Up @@ -63,6 +60,12 @@ def read(self):

app_module_paths = []
for app in get_apps():

# *Hopefully* just an artifact of testing, but at least one NoneType is
# returned by get_apps
if app is None:
continue

if hasattr(app, '__path__'):
# It's a 'models/' subpackage
for path in app.__path__:
Expand Down
20 changes: 3 additions & 17 deletions django_nose/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,14 @@ class FastFixtureTestCase(test.TransactionTestCase):
"""

cleans_up_after_itself = True # This is the good kind of puppy.
fixtures = [] # Otherwise default is None & non-iterable.

@classmethod
def setUpClass(cls):
"""Turn on manual commits. Load and commit the fixtures."""
if not test.testcases.connections_support_transactions():
raise NotImplementedError('%s supports only DBs with transaction '
'capabilities.' % cls.__name__)
for db in cls._databases():
# These MUST be balanced with one leave_* each:
transaction.enter_transaction_management(using=db)
# Don't commit unless we say so:
transaction.managed(True, using=db)

cls._fixture_setup()

@classmethod
Expand All @@ -59,15 +54,13 @@ def tearDownClass(cls):
for db in cls._databases():
# Finish off any transactions that may have happened in
# tearDownClass in a child method.
if transaction.is_dirty(using=db):
transaction.commit(using=db)
transaction.leave_transaction_management(using=db)
transaction.commit(using=db)

@classmethod
def _fixture_setup(cls):
"""Load fixture data, and commit."""
for db in cls._databases():
if (hasattr(cls, 'fixtures') and
if (getattr(cls, 'fixtures', None) and
getattr(cls, '_fb_should_setup_fixtures', True)):
# Iff the fixture-bundling test runner tells us we're the first
# suite having these fixtures, set them up:
Expand Down Expand Up @@ -122,11 +115,7 @@ def _pre_setup(self):
cache.cache.clear()
settings.TEMPLATE_DEBUG = settings.DEBUG = False

test.testcases.disable_transaction_methods()

self.client = self.client_class()
# self._fixture_setup()
self._urlconf_setup()
mail.outbox = []

# Clear site cache in case somebody's mutated Site objects and then
Expand All @@ -142,12 +131,9 @@ def _post_teardown(self):

"""
# Rollback any mutations made by tests:
test.testcases.restore_transaction_methods()
for db in self._databases():
transaction.rollback(using=db)

self._urlconf_teardown()

# We do not need to close the connection here to prevent
# http://code.djangoproject.com/ticket/7572, since we commit, not
# rollback, the test fixtures and thus any cursor startup statements.
Expand Down
2 changes: 1 addition & 1 deletion runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ django_test() {
fi
}

TESTAPP_COUNT=6
TESTAPP_COUNT=8

reset_env
django_test "./manage.py test $NOINPUT" $TESTAPP_COUNT 'normal settings'
Expand Down
1 change: 1 addition & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def rel_path(*subpaths):
MIDDLEWARE_CLASSES = ()

INSTALLED_APPS = [
'django.contrib.sites',
'django_nose',
'testapp',
]
Expand Down
35 changes: 35 additions & 0 deletions testapp/test_fast_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Tests for FastFixtureTestCase.

These tests primarily verify that the TestCases load and tests run rather than
specifically verifying how the TestCase handles the fixtures themselves.
"""

from datetime import datetime

from django_nose.testcases import FastFixtureTestCase
from testapp.models import Question


class HasFixturesTestCase(FastFixtureTestCase):
"""Tests that use a test fixture."""

fixtures = ["testdata.json"]

def test_fixture_loaded(self):
"""Test that a FAST fixture was loaded."""
question = Question.objects.get()
self.assertEqual(
'What is your favorite color?', question.question_text)
self.assertEqual(datetime(1975, 4, 9), question.pub_date)
choice = question.choice_set.get()
self.assertEqual("Blue.", choice.choice_text)
self.assertEqual(3, choice.votes)


class MissingFixturesTestCase(FastFixtureTestCase):
"""No fixtures defined."""

def test_anything(self):
"""Run any test to ensure Testcase is loaded without fixtures."""
assert True