2009-12-13 17:08:36 +00:00
|
|
|
"""
|
|
|
|
Tests for django test runner
|
|
|
|
"""
|
2013-02-16 12:26:36 +00:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2013-01-09 18:59:24 +00:00
|
|
|
import sys
|
2011-06-10 08:26:05 +00:00
|
|
|
from optparse import make_option
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-12-05 00:44:34 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-06-10 08:26:05 +00:00
|
|
|
from django.core.management import call_command
|
2012-03-14 10:58:23 +00:00
|
|
|
from django import db
|
2012-05-22 20:46:27 +00:00
|
|
|
from django.test import simple, TransactionTestCase, skipUnlessDBFeature
|
2012-01-20 23:15:22 +00:00
|
|
|
from django.test.simple import DjangoTestSuiteRunner, get_tests
|
2012-03-13 17:52:48 +00:00
|
|
|
from django.test.testcases import connections_support_transactions
|
2010-10-11 12:55:17 +00:00
|
|
|
from django.utils import unittest
|
2011-06-12 12:34:10 +00:00
|
|
|
from django.utils.importlib import import_module
|
|
|
|
|
2013-02-26 12:19:18 +00:00
|
|
|
from admin_scripts.tests import AdminScriptTestCase
|
2012-04-24 16:47:31 +00:00
|
|
|
from .models import Person
|
2009-12-22 06:00:57 +00:00
|
|
|
|
2011-04-02 13:26:39 +00:00
|
|
|
|
2013-02-26 12:19:18 +00:00
|
|
|
TEST_APP_OK = 'test_runner.valid_app.models'
|
|
|
|
TEST_APP_ERROR = 'test_runner.invalid_app.models'
|
2011-06-12 12:34:10 +00:00
|
|
|
|
|
|
|
|
2010-12-05 00:44:34 +00:00
|
|
|
class DependencyOrderingTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_simple_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 14:02:14 +00:00
|
|
|
('s1', ('s1_db', ['alpha'])),
|
|
|
|
('s2', ('s2_db', ['bravo'])),
|
|
|
|
('s3', ('s3_db', ['charlie'])),
|
2010-12-05 00:44:34 +00:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
'alpha': ['charlie'],
|
|
|
|
'bravo': ['charlie'],
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered = simple.dependency_ordered(raw, dependencies=dependencies)
|
2011-02-02 14:02:14 +00:00
|
|
|
ordered_sigs = [sig for sig,value in ordered]
|
2010-12-05 00:44:34 +00:00
|
|
|
|
|
|
|
self.assertIn('s1', ordered_sigs)
|
|
|
|
self.assertIn('s2', ordered_sigs)
|
|
|
|
self.assertIn('s3', ordered_sigs)
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
|
|
|
|
|
|
|
|
def test_chained_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 14:02:14 +00:00
|
|
|
('s1', ('s1_db', ['alpha'])),
|
|
|
|
('s2', ('s2_db', ['bravo'])),
|
|
|
|
('s3', ('s3_db', ['charlie'])),
|
2010-12-05 00:44:34 +00:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
'alpha': ['bravo'],
|
|
|
|
'bravo': ['charlie'],
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered = simple.dependency_ordered(raw, dependencies=dependencies)
|
2011-02-02 14:02:14 +00:00
|
|
|
ordered_sigs = [sig for sig,value in ordered]
|
2010-12-05 00:44:34 +00:00
|
|
|
|
|
|
|
self.assertIn('s1', ordered_sigs)
|
|
|
|
self.assertIn('s2', ordered_sigs)
|
|
|
|
self.assertIn('s3', ordered_sigs)
|
|
|
|
|
|
|
|
# Explicit dependencies
|
|
|
|
self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
|
|
|
|
|
|
|
|
# Implied dependencies
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
|
|
|
|
|
|
|
|
def test_multiple_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 14:02:14 +00:00
|
|
|
('s1', ('s1_db', ['alpha'])),
|
|
|
|
('s2', ('s2_db', ['bravo'])),
|
|
|
|
('s3', ('s3_db', ['charlie'])),
|
|
|
|
('s4', ('s4_db', ['delta'])),
|
2010-12-05 00:44:34 +00:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
'alpha': ['bravo','delta'],
|
|
|
|
'bravo': ['charlie'],
|
|
|
|
'delta': ['charlie'],
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered = simple.dependency_ordered(raw, dependencies=dependencies)
|
|
|
|
ordered_sigs = [sig for sig,aliases in ordered]
|
|
|
|
|
|
|
|
self.assertIn('s1', ordered_sigs)
|
|
|
|
self.assertIn('s2', ordered_sigs)
|
|
|
|
self.assertIn('s3', ordered_sigs)
|
|
|
|
self.assertIn('s4', ordered_sigs)
|
|
|
|
|
|
|
|
# Explicit dependencies
|
|
|
|
self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
|
|
|
|
self.assertLess(ordered_sigs.index('s4'), ordered_sigs.index('s1'))
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s4'))
|
|
|
|
|
|
|
|
# Implicit dependencies
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
|
|
|
|
|
|
|
|
def test_circular_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 14:02:14 +00:00
|
|
|
('s1', ('s1_db', ['alpha'])),
|
|
|
|
('s2', ('s2_db', ['bravo'])),
|
2010-12-05 00:44:34 +00:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
'bravo': ['alpha'],
|
|
|
|
'alpha': ['bravo'],
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertRaises(ImproperlyConfigured, simple.dependency_ordered, raw, dependencies=dependencies)
|
|
|
|
|
2012-04-24 16:05:47 +00:00
|
|
|
def test_own_alias_dependency(self):
|
|
|
|
raw = [
|
|
|
|
('s1', ('s1_db', ['alpha', 'bravo']))
|
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
'alpha': ['bravo']
|
|
|
|
}
|
|
|
|
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
simple.dependency_ordered(raw, dependencies=dependencies)
|
|
|
|
|
|
|
|
# reordering aliases shouldn't matter
|
|
|
|
raw = [
|
|
|
|
('s1', ('s1_db', ['bravo', 'alpha']))
|
|
|
|
]
|
|
|
|
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
simple.dependency_ordered(raw, dependencies=dependencies)
|
|
|
|
|
2011-06-10 08:26:05 +00:00
|
|
|
|
|
|
|
class MockTestRunner(object):
|
|
|
|
invoked = False
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def run_tests(self, test_labels, extra_tests=None, **kwargs):
|
|
|
|
MockTestRunner.invoked = True
|
|
|
|
|
|
|
|
|
|
|
|
class ManageCommandTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_custom_test_runner(self):
|
|
|
|
call_command('test', 'sites',
|
2013-02-26 12:19:18 +00:00
|
|
|
testrunner='test_runner.tests.MockTestRunner')
|
2011-06-10 08:26:05 +00:00
|
|
|
self.assertTrue(MockTestRunner.invoked,
|
|
|
|
"The custom test runner has not been invoked")
|
|
|
|
|
2013-02-16 12:26:36 +00:00
|
|
|
def test_bad_test_runner(self):
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
call_command('test', 'sites',
|
2013-02-26 12:19:18 +00:00
|
|
|
testrunner='test_runner.NonExistentRunner')
|
2013-02-16 12:26:36 +00:00
|
|
|
|
2011-06-10 08:26:05 +00:00
|
|
|
|
|
|
|
class CustomOptionsTestRunner(simple.DjangoTestSuiteRunner):
|
|
|
|
option_list = (
|
|
|
|
make_option('--option_a','-a', action='store', dest='option_a', default='1'),
|
|
|
|
make_option('--option_b','-b', action='store', dest='option_b', default='2'),
|
|
|
|
make_option('--option_c','-c', action='store', dest='option_c', default='3'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, verbosity=1, interactive=True, failfast=True, option_a=None, option_b=None, option_c=None, **kwargs):
|
|
|
|
super(CustomOptionsTestRunner, self).__init__(verbosity=verbosity, interactive=interactive,
|
|
|
|
failfast=failfast)
|
|
|
|
self.option_a = option_a
|
|
|
|
self.option_b = option_b
|
|
|
|
self.option_c = option_c
|
|
|
|
|
|
|
|
def run_tests(self, test_labels, extra_tests=None, **kwargs):
|
2012-04-28 16:02:01 +00:00
|
|
|
print("%s:%s:%s" % (self.option_a, self.option_b, self.option_c))
|
2011-06-10 08:26:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomTestRunnerOptionsTests(AdminScriptTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
settings = {
|
2013-02-26 12:19:18 +00:00
|
|
|
'TEST_RUNNER': '\'test_runner.tests.CustomOptionsTestRunner\'',
|
2011-06-10 08:26:05 +00:00
|
|
|
}
|
|
|
|
self.write_settings('settings.py', sdict=settings)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_default_options(self):
|
2013-02-26 15:02:24 +00:00
|
|
|
args = ['test', '--settings=test_project.settings']
|
2011-06-10 08:26:05 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, '1:2:3')
|
|
|
|
|
|
|
|
def test_default_and_given_options(self):
|
2013-02-26 15:02:24 +00:00
|
|
|
args = ['test', '--settings=test_project.settings', '--option_b=foo']
|
2011-06-10 08:26:05 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, '1:foo:3')
|
|
|
|
|
|
|
|
def test_option_name_and_value_separated(self):
|
2013-02-26 15:02:24 +00:00
|
|
|
args = ['test', '--settings=test_project.settings', '--option_b', 'foo']
|
2011-06-10 08:26:05 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, '1:foo:3')
|
|
|
|
|
|
|
|
def test_all_options_given(self):
|
2013-02-26 15:02:24 +00:00
|
|
|
args = ['test', '--settings=test_project.settings', '--option_a=bar',
|
|
|
|
'--option_b=foo', '--option_c=31337']
|
2011-06-10 08:26:05 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, 'bar:foo:31337')
|
2011-06-12 12:34:10 +00:00
|
|
|
|
|
|
|
|
2011-12-29 01:18:30 +00:00
|
|
|
class Ticket17477RegressionTests(AdminScriptTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.write_settings('settings.py')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_ticket_17477(self):
|
|
|
|
"""'manage.py help test' works after r16352."""
|
|
|
|
args = ['help', 'test']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
|
|
|
|
|
2011-06-12 12:34:10 +00:00
|
|
|
class ModulesTestsPackages(unittest.TestCase):
|
|
|
|
def test_get_tests(self):
|
|
|
|
"Check that the get_tests helper function can find tests in a directory"
|
|
|
|
module = import_module(TEST_APP_OK)
|
|
|
|
tests = get_tests(module)
|
|
|
|
self.assertIsInstance(tests, type(module))
|
|
|
|
|
|
|
|
def test_import_error(self):
|
|
|
|
"Test for #12658 - Tests with ImportError's shouldn't fail silently"
|
|
|
|
module = import_module(TEST_APP_ERROR)
|
|
|
|
self.assertRaises(ImportError, get_tests, module)
|
2012-03-13 17:52:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Sqlite3InMemoryTestDbs(unittest.TestCase):
|
2012-03-14 10:58:23 +00:00
|
|
|
|
|
|
|
@unittest.skipUnless(all(db.connections[conn].vendor == 'sqlite' for conn in db.connections),
|
|
|
|
"This is a sqlite-specific issue")
|
2012-03-13 17:52:48 +00:00
|
|
|
def test_transaction_support(self):
|
|
|
|
"""Ticket #16329: sqlite3 in-memory test databases"""
|
|
|
|
old_db_connections = db.connections
|
|
|
|
for option in ('NAME', 'TEST_NAME'):
|
|
|
|
try:
|
|
|
|
db.connections = db.ConnectionHandler({
|
|
|
|
'default': {
|
|
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
|
|
option: ':memory:',
|
|
|
|
},
|
|
|
|
'other': {
|
|
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
|
|
option: ':memory:',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
other = db.connections['other']
|
|
|
|
DjangoTestSuiteRunner(verbosity=0).setup_databases()
|
2012-03-14 10:58:23 +00:00
|
|
|
msg = "DATABASES setting '%s' option set to sqlite3's ':memory:' value shouldn't interfere with transaction support detection." % option
|
2012-03-13 17:52:48 +00:00
|
|
|
# Transaction support should be properly initialised for the 'other' DB
|
2012-06-09 15:22:24 +00:00
|
|
|
self.assertTrue(other.features.supports_transactions, msg)
|
2012-03-13 17:52:48 +00:00
|
|
|
# And all the DBs should report that they support transactions
|
2012-03-14 10:58:23 +00:00
|
|
|
self.assertTrue(connections_support_transactions(), msg)
|
2012-03-13 17:52:48 +00:00
|
|
|
finally:
|
|
|
|
db.connections = old_db_connections
|
2012-04-24 16:47:31 +00:00
|
|
|
|
|
|
|
|
2013-01-04 12:55:20 +00:00
|
|
|
class DummyBackendTest(unittest.TestCase):
|
|
|
|
def test_setup_databases(self):
|
|
|
|
"""
|
|
|
|
Test that setup_databases() doesn't fail with dummy database backend.
|
|
|
|
"""
|
|
|
|
runner = DjangoTestSuiteRunner(verbosity=0)
|
|
|
|
old_db_connections = db.connections
|
|
|
|
try:
|
|
|
|
db.connections = db.ConnectionHandler({})
|
|
|
|
old_config = runner.setup_databases()
|
|
|
|
runner.teardown_databases(old_config)
|
|
|
|
except Exception as e:
|
|
|
|
self.fail("setup_databases/teardown_databases unexpectedly raised "
|
|
|
|
"an error: %s" % e)
|
|
|
|
finally:
|
|
|
|
db.connections = old_db_connections
|
|
|
|
|
|
|
|
|
2013-01-03 00:00:39 +00:00
|
|
|
class DeprecationDisplayTest(AdminScriptTestCase):
|
|
|
|
# tests for 19546
|
|
|
|
def setUp(self):
|
2013-02-26 12:19:18 +00:00
|
|
|
settings = {'INSTALLED_APPS': '("test_runner.deprecation_app",)',
|
2013-01-09 22:32:51 +00:00
|
|
|
'DATABASES': '{"default": {"ENGINE":"django.db.backends.sqlite3", "NAME":":memory:"}}' }
|
2013-01-03 00:00:39 +00:00
|
|
|
self.write_settings('settings.py', sdict=settings)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_runner_deprecation_verbosity_default(self):
|
2013-02-26 15:02:24 +00:00
|
|
|
args = ['test', '--settings=test_project.settings']
|
2013-01-03 00:00:39 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertTrue("DeprecationWarning: warning from test" in err)
|
|
|
|
|
2013-01-09 18:59:24 +00:00
|
|
|
@unittest.skipIf(sys.version_info[:2] == (2, 6),
|
|
|
|
"On Python 2.6, DeprecationWarnings are visible anyway")
|
2013-01-03 00:00:39 +00:00
|
|
|
def test_runner_deprecation_verbosity_zero(self):
|
2013-02-26 12:19:18 +00:00
|
|
|
args = ['test', '--settings=settings', '--verbosity=0']
|
2013-01-03 00:00:39 +00:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertFalse("DeprecationWarning: warning from test" in err)
|
|
|
|
|
2013-01-09 18:59:24 +00:00
|
|
|
|
2012-04-24 16:47:31 +00:00
|
|
|
class AutoIncrementResetTest(TransactionTestCase):
|
|
|
|
"""
|
|
|
|
Here we test creating the same model two times in different test methods,
|
|
|
|
and check that both times they get "1" as their PK value. That is, we test
|
|
|
|
that AutoField values start from 1 for each transactional test case.
|
|
|
|
"""
|
2012-07-24 20:24:16 +00:00
|
|
|
|
|
|
|
reset_sequences = True
|
|
|
|
|
2012-05-22 20:46:27 +00:00
|
|
|
@skipUnlessDBFeature('supports_sequence_reset')
|
2012-04-24 16:47:31 +00:00
|
|
|
def test_autoincrement_reset1(self):
|
|
|
|
p = Person.objects.create(first_name='Jack', last_name='Smith')
|
2012-05-03 14:39:16 +00:00
|
|
|
self.assertEqual(p.pk, 1)
|
2012-04-24 16:47:31 +00:00
|
|
|
|
2012-05-22 20:46:27 +00:00
|
|
|
@skipUnlessDBFeature('supports_sequence_reset')
|
2012-04-24 16:47:31 +00:00
|
|
|
def test_autoincrement_reset2(self):
|
|
|
|
p = Person.objects.create(first_name='Jack', last_name='Smith')
|
2012-05-03 14:39:16 +00:00
|
|
|
self.assertEqual(p.pk, 1)
|