2015-01-11 06:52:59 +08:00
|
|
|
import unittest
|
2017-01-07 19:11:46 +08:00
|
|
|
from io import StringIO
|
2015-01-11 06:52:59 +08:00
|
|
|
|
|
|
|
from django.db import connection
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.runner import DiscoverRunner
|
|
|
|
|
|
|
|
from .models import Person
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'sqlite', 'Only run on sqlite so we can check output SQL.')
|
|
|
|
class TestDebugSQL(unittest.TestCase):
|
|
|
|
|
|
|
|
class PassingTest(TestCase):
|
|
|
|
def runTest(self):
|
|
|
|
Person.objects.filter(first_name='pass').count()
|
|
|
|
|
|
|
|
class FailingTest(TestCase):
|
|
|
|
def runTest(self):
|
|
|
|
Person.objects.filter(first_name='fail').count()
|
|
|
|
self.fail()
|
|
|
|
|
|
|
|
class ErrorTest(TestCase):
|
|
|
|
def runTest(self):
|
|
|
|
Person.objects.filter(first_name='error').count()
|
|
|
|
raise Exception
|
|
|
|
|
2017-07-05 13:17:16 +08:00
|
|
|
class PassingSubTest(TestCase):
|
|
|
|
def runTest(self):
|
|
|
|
with self.subTest():
|
|
|
|
Person.objects.filter(first_name='subtest-pass').count()
|
|
|
|
|
|
|
|
class FailingSubTest(TestCase):
|
|
|
|
def runTest(self):
|
|
|
|
with self.subTest():
|
|
|
|
Person.objects.filter(first_name='subtest-fail').count()
|
|
|
|
self.fail()
|
|
|
|
|
|
|
|
class ErrorSubTest(TestCase):
|
|
|
|
def runTest(self):
|
|
|
|
with self.subTest():
|
|
|
|
Person.objects.filter(first_name='subtest-error').count()
|
|
|
|
raise Exception
|
|
|
|
|
2015-01-11 06:52:59 +08:00
|
|
|
def _test_output(self, verbosity):
|
|
|
|
runner = DiscoverRunner(debug_sql=True, verbosity=0)
|
|
|
|
suite = runner.test_suite()
|
|
|
|
suite.addTest(self.FailingTest())
|
|
|
|
suite.addTest(self.ErrorTest())
|
|
|
|
suite.addTest(self.PassingTest())
|
2017-07-05 13:17:16 +08:00
|
|
|
suite.addTest(self.PassingSubTest())
|
|
|
|
suite.addTest(self.FailingSubTest())
|
|
|
|
suite.addTest(self.ErrorSubTest())
|
2015-01-11 06:52:59 +08:00
|
|
|
old_config = runner.setup_databases()
|
2017-01-07 19:11:46 +08:00
|
|
|
stream = StringIO()
|
2015-01-11 06:52:59 +08:00
|
|
|
resultclass = runner.get_resultclass()
|
|
|
|
runner.test_runner(
|
|
|
|
verbosity=verbosity,
|
|
|
|
stream=stream,
|
|
|
|
resultclass=resultclass,
|
|
|
|
).run(suite)
|
|
|
|
runner.teardown_databases(old_config)
|
|
|
|
|
2015-02-15 08:47:07 +08:00
|
|
|
return stream.getvalue()
|
2015-01-11 06:52:59 +08:00
|
|
|
|
|
|
|
def test_output_normal(self):
|
|
|
|
full_output = self._test_output(1)
|
|
|
|
for output in self.expected_outputs:
|
|
|
|
self.assertIn(output, full_output)
|
|
|
|
for output in self.verbose_expected_outputs:
|
|
|
|
self.assertNotIn(output, full_output)
|
|
|
|
|
|
|
|
def test_output_verbose(self):
|
|
|
|
full_output = self._test_output(2)
|
|
|
|
for output in self.expected_outputs:
|
|
|
|
self.assertIn(output, full_output)
|
|
|
|
for output in self.verbose_expected_outputs:
|
|
|
|
self.assertIn(output, full_output)
|
|
|
|
|
2015-09-13 15:30:35 +08:00
|
|
|
expected_outputs = [
|
|
|
|
('''SELECT COUNT(*) AS "__count" '''
|
|
|
|
'''FROM "test_runner_person" WHERE '''
|
|
|
|
'''"test_runner_person"."first_name" = 'error';'''),
|
|
|
|
('''SELECT COUNT(*) AS "__count" '''
|
|
|
|
'''FROM "test_runner_person" WHERE '''
|
|
|
|
'''"test_runner_person"."first_name" = 'fail';'''),
|
2017-07-05 13:17:16 +08:00
|
|
|
('''SELECT COUNT(*) AS "__count" '''
|
|
|
|
'''FROM "test_runner_person" WHERE '''
|
|
|
|
'''"test_runner_person"."first_name" = 'subtest-error';'''),
|
|
|
|
('''SELECT COUNT(*) AS "__count" '''
|
|
|
|
'''FROM "test_runner_person" WHERE '''
|
|
|
|
'''"test_runner_person"."first_name" = 'subtest-fail';'''),
|
2015-09-13 15:30:35 +08:00
|
|
|
]
|
2015-01-11 06:52:59 +08:00
|
|
|
|
|
|
|
verbose_expected_outputs = [
|
2017-02-18 08:45:34 +08:00
|
|
|
'runTest (test_runner.test_debug_sql.TestDebugSQL.FailingTest) ... FAIL',
|
|
|
|
'runTest (test_runner.test_debug_sql.TestDebugSQL.ErrorTest) ... ERROR',
|
|
|
|
'runTest (test_runner.test_debug_sql.TestDebugSQL.PassingTest) ... ok',
|
|
|
|
# If there are errors/failures in subtests but not in test itself,
|
|
|
|
# the status is not written. That behavior comes from Python.
|
|
|
|
'runTest (test_runner.test_debug_sql.TestDebugSQL.FailingSubTest) ...',
|
|
|
|
'runTest (test_runner.test_debug_sql.TestDebugSQL.ErrorSubTest) ...',
|
2015-09-13 15:30:35 +08:00
|
|
|
('''SELECT COUNT(*) AS "__count" '''
|
|
|
|
'''FROM "test_runner_person" WHERE '''
|
|
|
|
'''"test_runner_person"."first_name" = 'pass';'''),
|
2017-07-05 13:17:16 +08:00
|
|
|
('''SELECT COUNT(*) AS "__count" '''
|
|
|
|
'''FROM "test_runner_person" WHERE '''
|
|
|
|
'''"test_runner_person"."first_name" = 'subtest-pass';'''),
|
2015-01-11 06:52:59 +08:00
|
|
|
]
|