File: 0012-Add-Python-3.11-support-for-tests.patch

package info (click to toggle)
python-django 3%3A3.2.19-1%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 56,696 kB
  • sloc: python: 264,418; javascript: 18,362; xml: 193; makefile: 178; sh: 43
file content (132 lines) | stat: -rw-r--r-- 6,142 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
From: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Date: Wed, 1 Feb 2023 08:00:45 -0800
Subject: Fix test_runner/test_utils tests on Python 3.11+.

Origin: upstream, https://github.com/django/django/commit/2ee4caf56b8e000cabbb73ad81ff05738d6d0a35
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/python-django/+bug/2002012
Forwarded: no
Last-Update: 2023-01-06

Python 3.11 uses fully qualified test name in unittest output. See
https://github.com/python/cpython/commit/755be9b1505af591b9f2ee424a6525b6c2b65ce9
Note: This is a temporary patch for Django 3.2 to maintain compatibility with
Python 3.11 while transitioning to Django 4.x
Last-Update: 2023-01-06
---
 django/utils/version.py             |  1 +
 tests/test_runner/test_debug_sql.py | 14 +++++++++-----
 tests/test_runner/test_parallel.py  |  8 ++++++--
 tests/test_utils/tests.py           |  9 +++++++--
 4 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/django/utils/version.py b/django/utils/version.py
index 74c327525e7d..0c2bfc626e21 100644
--- a/django/utils/version.py
+++ b/django/utils/version.py
@@ -15,6 +15,7 @@ PY37 = sys.version_info >= (3, 7)
 PY38 = sys.version_info >= (3, 8)
 PY39 = sys.version_info >= (3, 9)
 PY310 = sys.version_info >= (3, 10)
+PY311 = sys.version_info >= (3, 11)
 
 
 def get_version(version=None):
diff --git a/tests/test_runner/test_debug_sql.py b/tests/test_runner/test_debug_sql.py
index 0e8e4207d6ae..5e133ea10574 100644
--- a/tests/test_runner/test_debug_sql.py
+++ b/tests/test_runner/test_debug_sql.py
@@ -4,6 +4,7 @@ from io import StringIO
 from django.db import connection
 from django.test import TestCase
 from django.test.runner import DiscoverRunner
+from django.utils.version import PY311
 
 from .models import Person
 
@@ -100,14 +101,17 @@ class TestDebugSQL(unittest.TestCase):
             '''"test_runner_person"."first_name" = 'subtest-fail';'''),
     ]
 
+    # Python 3.11 uses fully qualified test name in the output.
+    method_name = ".runTest" if PY311 else ""
+    test_class_path = "test_runner.test_debug_sql.TestDebugSQL"
     verbose_expected_outputs = [
-        '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',
+        f"runTest ({test_class_path}.FailingTest{method_name}) ... FAIL",
+        f"runTest ({test_class_path}.ErrorTest{method_name}) ... ERROR",
+        f"runTest ({test_class_path}.PassingTest{method_name}) ... 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) ...',
+        f"runTest ({test_class_path}.FailingSubTest{method_name}) ...",
+        f"runTest ({test_class_path}.ErrorSubTest{method_name}) ...",
         ('''SELECT COUNT(*) AS "__count" '''
             '''FROM "test_runner_person" WHERE '''
             '''"test_runner_person"."first_name" = 'pass';'''),
diff --git a/tests/test_runner/test_parallel.py b/tests/test_runner/test_parallel.py
index c1a89bd0f09d..5d51cf6e4240 100644
--- a/tests/test_runner/test_parallel.py
+++ b/tests/test_runner/test_parallel.py
@@ -2,7 +2,7 @@ import unittest
 
 from django.test import SimpleTestCase
 from django.test.runner import RemoteTestResult
-from django.utils.version import PY37
+from django.utils.version import PY37, PY311
 
 try:
     import tblib
@@ -79,7 +79,11 @@ class RemoteTestResultTest(SimpleTestCase):
 
         event = events[1]
         self.assertEqual(event[0], 'addSubTest')
-        self.assertEqual(str(event[2]), 'dummy_test (test_runner.test_parallel.SampleFailingSubtest) (index=0)')
+        self.assertEqual(str(event[2]),
+            "dummy_test (test_runner.test_parallel.SampleFailingSubtest%s) (index=0)"
+            # Python 3.11 uses fully qualified test name in the output.
+            % (".dummy_test" if PY311 else ""),
+        )
         trailing_comma = '' if PY37 else ','
         self.assertEqual(repr(event[3][1]), "AssertionError('0 != 1'%s)" % trailing_comma)
 
diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
index 9255315e98dc..8f72057afe96 100644
--- a/tests/test_utils/tests.py
+++ b/tests/test_utils/tests.py
@@ -26,6 +26,7 @@ from django.test.utils import (
 )
 from django.urls import NoReverseMatch, path, reverse, reverse_lazy
 from django.utils.deprecation import RemovedInDjango41Warning
+from django.utils.version import PY311
 
 from .models import Car, Person, PossessedCar
 from .views import empty_response
@@ -78,9 +79,11 @@ class SkippingTestCase(SimpleTestCase):
             SkipTestCase('test_foo').test_foo,
             ValueError,
             "skipUnlessDBFeature cannot be used on test_foo (test_utils.tests."
-            "SkippingTestCase.test_skip_unless_db_feature.<locals>.SkipTestCase) "
+            "SkippingTestCase.test_skip_unless_db_feature.<locals>.SkipTestCase%s) "
             "as SkippingTestCase.test_skip_unless_db_feature.<locals>.SkipTestCase "
             "doesn't allow queries against the 'default' database."
+            # Python 3.11 uses fully qualified test name in the output.
+            % (".test_foo" if PY311 else ""),
         )
 
     def test_skip_if_db_feature(self):
@@ -122,9 +125,11 @@ class SkippingTestCase(SimpleTestCase):
             SkipTestCase('test_foo').test_foo,
             ValueError,
             "skipIfDBFeature cannot be used on test_foo (test_utils.tests."
-            "SkippingTestCase.test_skip_if_db_feature.<locals>.SkipTestCase) "
+            "SkippingTestCase.test_skip_if_db_feature.<locals>.SkipTestCase%s) "
             "as SkippingTestCase.test_skip_if_db_feature.<locals>.SkipTestCase "
             "doesn't allow queries against the 'default' database."
+            # Python 3.11 uses fully qualified test name in the output.
+            % (".test_foo" if PY311 else ""),
         )