Debian Codegraph @

tests/test_runner/test_debug_sql.py

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