Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Merge in Camilo's PEP 8 fixes and unused import removals.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikrose committed Sep 7, 2012
2 parents e3942c2 + 9d9d704 commit b79291f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
34 changes: 18 additions & 16 deletions django_nose/plugin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import os.path
import sys

from nose.plugins.base import Plugin
from nose.suite import ContextSuite

from django.conf import settings
from django.db.models.loading import get_apps, load_app
from django.test.testcases import TransactionTestCase, TestCase

from django_nose.testcases import FastFixtureTestCase
Expand Down Expand Up @@ -101,7 +98,9 @@ def add(self, test):
# We bucket even FFTCs that don't have any fixtures, but it
# shouldn't matter.
key = (frozenset(getattr(test.context, 'fixtures', [])),
getattr(test.context, 'exempt_from_fixture_bundling', False))
getattr(test.context,
'exempt_from_fixture_bundling',
False))
self.buckets.setdefault(key, []).append(test)
else:
self.remainder.append(test)
Expand Down Expand Up @@ -183,19 +182,19 @@ def filthiness(test):
return ContextSuite(flattened)

def _bundle_fixtures(self, test):
"""Reorder the tests in the suite so classes using identical sets of
fixtures are contiguous.
"""Reorder the tests in the suite so classes using identical
sets of fixtures are contiguous.
I reorder FastFixtureTestCases so ones using identical sets of fixtures run
adjacently. I then put attributes on them to advise them to not reload the
fixtures for each class.
I reorder FastFixtureTestCases so ones using identical sets
of fixtures run adjacently. I then put attributes on them
to advise them to not reload the fixtures for each class.
This takes support.mozilla.com's suite from 123s down to 94s.
FastFixtureTestCases are the only ones we care about, because nobody
else, in practice, pays attention to the ``_fb`` advisory bits. We
return those first, then any remaining tests in the order they were
received.
FastFixtureTestCases are the only ones we care about, because
nobody else, in practice, pays attention to the ``_fb`` advisory
bits. We return those first, then any remaining tests in the
order they were received.
"""
def suite_sorted_by_fixtures(suite):
Expand All @@ -215,22 +214,25 @@ def suite_sorted_by_fixtures(suite):
# Lay the bundles of common-fixture-having test classes end to end
# in a single list so we can make a test suite out of them:
flattened = []
for ((fixtures, is_exempt), fixture_bundle) in bucketer.buckets.iteritems():
for ((fixtures, is_exempt),
fixture_bundle) in bucketer.buckets.iteritems():
# Advise first and last test classes in each bundle to set up
# and tear down fixtures and the rest not to:
if fixtures and not is_exempt:
# Ones with fixtures are sure to be classes, which means
# they're sure to be ContextSuites with contexts.

# First class with this set of fixtures sets up:
fixture_bundle[0].context._fb_should_setup_fixtures = True
first = fixture_bundle[0].context
first._fb_should_setup_fixtures = True

# Set all classes' 1..n should_setup to False:
for cls in fixture_bundle[1:]:
cls.context._fb_should_setup_fixtures = False

# Last class tears down:
fixture_bundle[-1].context._fb_should_teardown_fixtures = True
last = fixture_bundle[-1].context
last._fb_should_teardown_fixtures = True

# Set all classes' 0..(n-1) should_teardown to False:
for cls in fixture_bundle[:-1]:
Expand Down
30 changes: 21 additions & 9 deletions django_nose/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,29 @@ def _get_test_db_name(self):


def _get_plugins_from_settings():
for plg_path in list(getattr(settings, 'NOSE_PLUGINS', [])) + ['django_nose.plugin.TestReorderer']:
plugins = (list(getattr(settings, 'NOSE_PLUGINS', [])) +
['django_nose.plugin.TestReorderer'])
for plug_path in plugins:
try:
dot = plg_path.rindex('.')
dot = plug_path.rindex('.')
except ValueError:
raise exceptions.ImproperlyConfigured(
"%s isn't a Nose plugin module" % plg_path)
p_mod, p_classname = plg_path[:dot], plg_path[dot+1:]
"%s isn't a Nose plugin module" % plug_path)
p_mod, p_classname = plug_path[:dot], plug_path[dot + 1:]

try:
mod = import_module(p_mod)
except ImportError, e:
raise exceptions.ImproperlyConfigured(
'Error importing Nose plugin module %s: "%s"' % (p_mod, e))

try:
p_class = getattr(mod, p_classname)
except AttributeError:
raise exceptions.ImproperlyConfigured(
'Nose plugin module "%s" does not define a "%s"' %
(p_mod, p_classname))

yield p_class()


Expand Down Expand Up @@ -158,8 +163,11 @@ def run_tests(self, test_labels, extra_tests=None):


_old_handle = Command.handle


def _foreign_key_ignoring_handle(self, *fixture_labels, **options):
"""Wrap the the stock loaddata to ignore foreign key checks so we can load circular references from fixtures.
"""Wrap the the stock loaddata to ignore foreign key
checks so we can load circular references from fixtures.
This is monkeypatched into place in setup_databases().
Expand Down Expand Up @@ -209,7 +217,8 @@ def _reusing_db():


def _can_support_reuse_db(connection):
"""Return whether it makes any sense to use REUSE_DB with the backend of a connection."""
"""Return whether it makes any sense to
use REUSE_DB with the backend of a connection."""
# Perhaps this is a SQLite in-memory DB. Those are created implicitly when
# you try to connect to them, so our usual test doesn't work.
return not connection.creation._get_test_db_name() == ':memory:'
Expand Down Expand Up @@ -239,7 +248,8 @@ def _should_create_database(connection):


def _mysql_reset_sequences(style, connection):
"""Return a list of SQL statements needed to reset all sequences for Django tables."""
"""Return a list of SQL statements needed to
reset all sequences for Django tables."""
tables = connection.introspection.django_table_names(only_existing=True)
flush_statements = connection.ops.sql_flush(
style, tables, connection.introspection.sequence_list())
Expand Down Expand Up @@ -277,7 +287,8 @@ def setup_databases(self):
connection.settings_dict['NAME'] = test_db_name

if _should_create_database(connection):
# We're not using _skip_create_test_db, so put the DB name back:
# We're not using _skip_create_test_db, so put the DB name
# back:
connection.settings_dict['NAME'] = orig_db_name

# Since we replaced the connection with the test DB, closing
Expand All @@ -294,7 +305,8 @@ def setup_databases(self):
style = no_style()

if uses_mysql(connection):
reset_statements = _mysql_reset_sequences(style, connection)
reset_statements = _mysql_reset_sequences(
style, connection)
else:
reset_statements = connection.ops.sequence_reset_sql(
style, cache.get_models())
Expand Down

0 comments on commit b79291f

Please sign in to comment.