test_debug_sql.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import unittest
  2. from io import StringIO
  3. from django.db import connection
  4. from django.test import TestCase
  5. from django.test.runner import DiscoverRunner
  6. from django.utils.version import PY311
  7. from .models import Person
  8. @unittest.skipUnless(
  9. connection.vendor == "sqlite", "Only run on sqlite so we can check output SQL."
  10. )
  11. class TestDebugSQL(unittest.TestCase):
  12. class PassingTest(TestCase):
  13. def runTest(self):
  14. Person.objects.filter(first_name="pass").count()
  15. class FailingTest(TestCase):
  16. def runTest(self):
  17. Person.objects.filter(first_name="fail").count()
  18. self.fail()
  19. class ErrorTest(TestCase):
  20. def runTest(self):
  21. Person.objects.filter(first_name="error").count()
  22. raise Exception
  23. class ErrorSetUpTestDataTest(TestCase):
  24. @classmethod
  25. def setUpTestData(cls):
  26. raise Exception
  27. def runTest(self):
  28. pass
  29. class PassingSubTest(TestCase):
  30. def runTest(self):
  31. with self.subTest():
  32. Person.objects.filter(first_name="subtest-pass").count()
  33. class FailingSubTest(TestCase):
  34. def runTest(self):
  35. with self.subTest():
  36. Person.objects.filter(first_name="subtest-fail").count()
  37. self.fail()
  38. class ErrorSubTest(TestCase):
  39. def runTest(self):
  40. with self.subTest():
  41. Person.objects.filter(first_name="subtest-error").count()
  42. raise Exception
  43. def _test_output(self, verbosity):
  44. runner = DiscoverRunner(debug_sql=True, verbosity=0)
  45. suite = runner.test_suite()
  46. suite.addTest(self.FailingTest())
  47. suite.addTest(self.ErrorTest())
  48. suite.addTest(self.PassingTest())
  49. suite.addTest(self.PassingSubTest())
  50. suite.addTest(self.FailingSubTest())
  51. suite.addTest(self.ErrorSubTest())
  52. old_config = runner.setup_databases()
  53. stream = StringIO()
  54. resultclass = runner.get_resultclass()
  55. runner.test_runner(
  56. verbosity=verbosity,
  57. stream=stream,
  58. resultclass=resultclass,
  59. ).run(suite)
  60. runner.teardown_databases(old_config)
  61. return stream.getvalue()
  62. def test_output_normal(self):
  63. full_output = self._test_output(1)
  64. for output in self.expected_outputs:
  65. self.assertIn(output, full_output)
  66. for output in self.verbose_expected_outputs:
  67. self.assertNotIn(output, full_output)
  68. def test_output_verbose(self):
  69. full_output = self._test_output(2)
  70. for output in self.expected_outputs:
  71. self.assertIn(output, full_output)
  72. for output in self.verbose_expected_outputs:
  73. self.assertIn(output, full_output)
  74. expected_outputs = [
  75. (
  76. """SELECT COUNT(*) AS "__count"\n"""
  77. """FROM "test_runner_person"\n"""
  78. """WHERE "test_runner_person"."first_name" = 'error';"""
  79. ),
  80. (
  81. """SELECT COUNT(*) AS "__count"\n"""
  82. """FROM "test_runner_person"\n"""
  83. """WHERE "test_runner_person"."first_name" = 'fail';"""
  84. ),
  85. (
  86. """SELECT COUNT(*) AS "__count"\n"""
  87. """FROM "test_runner_person"\n"""
  88. """WHERE "test_runner_person"."first_name" = 'subtest-error';"""
  89. ),
  90. (
  91. """SELECT COUNT(*) AS "__count"\n"""
  92. """FROM "test_runner_person"\n"""
  93. """WHERE "test_runner_person"."first_name" = 'subtest-fail';"""
  94. ),
  95. ]
  96. # Python 3.11 uses fully qualified test name in the output.
  97. method_name = ".runTest" if PY311 else ""
  98. test_class_path = "test_runner.test_debug_sql.TestDebugSQL"
  99. verbose_expected_outputs = [
  100. f"runTest ({test_class_path}.FailingTest{method_name}) ... FAIL",
  101. f"runTest ({test_class_path}.ErrorTest{method_name}) ... ERROR",
  102. f"runTest ({test_class_path}.PassingTest{method_name}) ... ok",
  103. # If there are errors/failures in subtests but not in test itself,
  104. # the status is not written. That behavior comes from Python.
  105. f"runTest ({test_class_path}.FailingSubTest{method_name}) ...",
  106. f"runTest ({test_class_path}.ErrorSubTest{method_name}) ...",
  107. (
  108. """SELECT COUNT(*) AS "__count" """
  109. """FROM "test_runner_person" WHERE """
  110. """"test_runner_person"."first_name" = 'pass';"""
  111. ),
  112. (
  113. """SELECT COUNT(*) AS "__count" """
  114. """FROM "test_runner_person" WHERE """
  115. """"test_runner_person"."first_name" = 'subtest-pass';"""
  116. ),
  117. ]
  118. def test_setupclass_exception(self):
  119. runner = DiscoverRunner(debug_sql=True, verbosity=0)
  120. suite = runner.test_suite()
  121. suite.addTest(self.ErrorSetUpTestDataTest())
  122. old_config = runner.setup_databases()
  123. stream = StringIO()
  124. runner.test_runner(
  125. verbosity=0,
  126. stream=stream,
  127. resultclass=runner.get_resultclass(),
  128. ).run(suite)
  129. runner.teardown_databases(old_config)
  130. output = stream.getvalue()
  131. self.assertIn(
  132. "ERROR: setUpClass "
  133. "(test_runner.test_debug_sql.TestDebugSQL.ErrorSetUpTestDataTest)",
  134. output,
  135. )