diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2020-05-10 13:03:39 -0700 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-05-11 12:01:28 +0200 |
| commit | d6aff369ad33457ae2355b5b210faf1c4890ff35 (patch) | |
| tree | 4c9d43311078bd81098e8a9fe9ff89fe007e921e /tests | |
| parent | 23f6fbdd93cd668740e3a1cd6d8c8259f380c0fe (diff) | |
Refs #30116 -- Simplified regex match group access with Match.__getitem__().
The method has been available since Python 3.6. The shorter syntax is
also marginally faster.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/admin_views/tests.py | 16 | ||||
| -rw-r--r-- | tests/admin_widgets/tests.py | 18 | ||||
| -rw-r--r-- | tests/auth_tests/client.py | 2 | ||||
| -rw-r--r-- | tests/auth_tests/test_views.py | 6 | ||||
| -rw-r--r-- | tests/backends/sqlite/tests.py | 2 | ||||
| -rw-r--r-- | tests/csrf_tests/tests.py | 2 | ||||
| -rw-r--r-- | tests/gis_tests/test_geoforms.py | 2 | ||||
| -rw-r--r-- | tests/inspectdb/tests.py | 2 | ||||
| -rw-r--r-- | tests/template_backends/test_dummy.py | 2 | ||||
| -rw-r--r-- | tests/test_client_regress/views.py | 2 | ||||
| -rw-r--r-- | tests/view_tests/tests/test_debug.py | 2 |
11 files changed, 28 insertions, 28 deletions
diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index f768172909..db96fbebef 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -5030,7 +5030,7 @@ class RawIdFieldsTest(TestCase): # Find the link m = re.search(br'<a href="([^"]*)"[^>]* id="lookup_id_inquisition"', response.content) self.assertTrue(m) # Got a match - popup_url = m.groups()[0].decode().replace("&", "&") + popup_url = m[1].decode().replace('&', '&') # Handle relative links popup_url = urljoin(response.request['PATH_INFO'], popup_url) @@ -5053,7 +5053,7 @@ class RawIdFieldsTest(TestCase): # Find the link m = re.search(br'<a href="([^"]*)"[^>]* id="lookup_id_defendant0"', response.content) self.assertTrue(m) # Got a match - popup_url = m.groups()[0].decode().replace("&", "&") + popup_url = m[1].decode().replace('&', '&') # Handle relative links popup_url = urljoin(response.request['PATH_INFO'], popup_url) @@ -5073,7 +5073,7 @@ class RawIdFieldsTest(TestCase): # Find the link m = re.search(br'<a href="([^"]*)"[^>]* id="lookup_id_defendant1"', response.content) self.assertTrue(m) # Got a match - popup_url = m.groups()[0].decode().replace("&", "&") + popup_url = m[1].decode().replace('&', '&') # Handle relative links popup_url = urljoin(response.request['PATH_INFO'], popup_url) @@ -5924,7 +5924,7 @@ class AdminKeepChangeListFiltersTests(TestCase): '<a href="(.*?)">{}</a>'.format(self.joepublicuser.username), response.content.decode() ) - self.assertURLEqual(detail_link.group(1), self.get_change_url()) + self.assertURLEqual(detail_link[1], self.get_change_url()) def test_change_view(self): # Get the `change_view`. @@ -5936,21 +5936,21 @@ class AdminKeepChangeListFiltersTests(TestCase): '<form action="(.*?)" method="post" id="user_form" novalidate>', response.content.decode() ) - self.assertURLEqual(form_action.group(1), '?%s' % self.get_preserved_filters_querystring()) + self.assertURLEqual(form_action[1], '?%s' % self.get_preserved_filters_querystring()) # Check the history link. history_link = re.search( '<a href="(.*?)" class="historylink">History</a>', response.content.decode() ) - self.assertURLEqual(history_link.group(1), self.get_history_url()) + self.assertURLEqual(history_link[1], self.get_history_url()) # Check the delete link. delete_link = re.search( '<a href="(.*?)" class="deletelink">Delete</a>', response.content.decode() ) - self.assertURLEqual(delete_link.group(1), self.get_delete_url()) + self.assertURLEqual(delete_link[1], self.get_delete_url()) # Test redirect on "Save". post_data = { @@ -5993,7 +5993,7 @@ class AdminKeepChangeListFiltersTests(TestCase): '<form action="(.*?)" method="post" id="user_form" novalidate>', response.content.decode() ) - self.assertURLEqual(form_action.group(1), '?%s' % self.get_preserved_filters_querystring()) + self.assertURLEqual(form_action[1], '?%s' % self.get_preserved_filters_querystring()) post_data = { 'username': 'dummy', diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index b46753f95f..34166f986c 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -392,42 +392,42 @@ class AdminURLWidgetTest(SimpleTestCase): w = widgets.AdminURLFieldWidget() output = w.render('test', 'http://example.com/<sometag>some-text</sometag>') self.assertEqual( - HREF_RE.search(output).groups()[0], + HREF_RE.search(output)[1], 'http://example.com/%3Csometag%3Esome-text%3C/sometag%3E', ) self.assertEqual( - TEXT_RE.search(output).groups()[0], + TEXT_RE.search(output)[1], 'http://example.com/<sometag>some-text</sometag>', ) self.assertEqual( - VALUE_RE.search(output).groups()[0], + VALUE_RE.search(output)[1], 'http://example.com/<sometag>some-text</sometag>', ) output = w.render('test', 'http://example-äüö.com/<sometag>some-text</sometag>') self.assertEqual( - HREF_RE.search(output).groups()[0], + HREF_RE.search(output)[1], 'http://xn--example--7za4pnc.com/%3Csometag%3Esome-text%3C/sometag%3E', ) self.assertEqual( - TEXT_RE.search(output).groups()[0], + TEXT_RE.search(output)[1], 'http://example-äüö.com/<sometag>some-text</sometag>', ) self.assertEqual( - VALUE_RE.search(output).groups()[0], + VALUE_RE.search(output)[1], 'http://example-äüö.com/<sometag>some-text</sometag>', ) output = w.render('test', 'http://www.example.com/%C3%A4"><script>alert("XSS!")</script>"') self.assertEqual( - HREF_RE.search(output).groups()[0], + HREF_RE.search(output)[1], 'http://www.example.com/%C3%A4%22%3E%3Cscript%3Ealert(%22XSS!%22)%3C/script%3E%22', ) self.assertEqual( - TEXT_RE.search(output).groups()[0], + TEXT_RE.search(output)[1], 'http://www.example.com/%C3%A4"><script>' 'alert("XSS!")</script>"' ) self.assertEqual( - VALUE_RE.search(output).groups()[0], + VALUE_RE.search(output)[1], 'http://www.example.com/%C3%A4"><script>alert("XSS!")</script>"', ) diff --git a/tests/auth_tests/client.py b/tests/auth_tests/client.py index 42740bb0e8..c2ce1102ce 100644 --- a/tests/auth_tests/client.py +++ b/tests/auth_tests/client.py @@ -9,7 +9,7 @@ from django.test import Client def extract_token_from_url(url): token_search = re.search(r'/reset/.*/(.+?)/', url) if token_search: - return token_search.group(1) + return token_search[1] class PasswordResetConfirmClient(Client): diff --git a/tests/auth_tests/test_views.py b/tests/auth_tests/test_views.py index 48278e23f9..2f27fa7271 100644 --- a/tests/auth_tests/test_views.py +++ b/tests/auth_tests/test_views.py @@ -201,7 +201,7 @@ class PasswordResetTest(AuthViewsTestCase): def _read_signup_email(self, email): urlmatch = re.search(r"https?://[^/]*(/.*reset/\S*)", email.body) self.assertIsNotNone(urlmatch, "No URL found in sent email") - return urlmatch.group(), urlmatch.groups()[0] + return urlmatch[0], urlmatch[1] def test_confirm_valid(self): url, path = self._test_confirm_start() @@ -414,7 +414,7 @@ class CustomUserPasswordResetTest(AuthViewsTestCase): def _read_signup_email(self, email): urlmatch = re.search(r"https?://[^/]*(/.*reset/\S*)", email.body) self.assertIsNotNone(urlmatch, "No URL found in sent email") - return urlmatch.group(), urlmatch.groups()[0] + return urlmatch[0], urlmatch[1] def test_confirm_valid_custom_user(self): url, path = self._test_confirm_start() @@ -1215,7 +1215,7 @@ class ChangelistTests(AuthViewsTestCase): rel_link = re.search( r'you can change the password using <a href="([^"]*)">this form</a>', response.content.decode() - ).groups()[0] + )[1] self.assertEqual( os.path.normpath(user_change_url + rel_link), os.path.normpath(password_change_url) diff --git a/tests/backends/sqlite/tests.py b/tests/backends/sqlite/tests.py index 3898c8f13c..c014519910 100644 --- a/tests/backends/sqlite/tests.py +++ b/tests/backends/sqlite/tests.py @@ -131,7 +131,7 @@ class SchemaTests(TransactionTestCase): self.assertIsNotNone(match) self.assertEqual( 'integer NOT NULL PRIMARY KEY AUTOINCREMENT', - match.group(1), + match[1], 'Wrong SQL used to create an auto-increment column on SQLite' ) diff --git a/tests/csrf_tests/tests.py b/tests/csrf_tests/tests.py index 63cbd08c94..63a2d478b4 100644 --- a/tests/csrf_tests/tests.py +++ b/tests/csrf_tests/tests.py @@ -64,7 +64,7 @@ class CsrfViewMiddlewareTestMixin: match = re.search('name="csrfmiddlewaretoken" value="(.*?)"', text) csrf_token = csrf_id or self._csrf_id self.assertTrue( - match and equivalent_tokens(csrf_token, match.group(1)), + match and equivalent_tokens(csrf_token, match[1]), "Could not find csrfmiddlewaretoken to match %s" % csrf_token ) diff --git a/tests/gis_tests/test_geoforms.py b/tests/gis_tests/test_geoforms.py index c6fb2f71fb..0ce3b5f66b 100644 --- a/tests/gis_tests/test_geoforms.py +++ b/tests/gis_tests/test_geoforms.py @@ -136,7 +136,7 @@ class GeometryFieldTest(SimpleTestCase): # The first point can't use assertInHTML() due to non-deterministic # ordering of the rendered dictionary. - pt1_serialized = re.search(r'<textarea [^>]*>({[^<]+})<', output).groups()[0] + pt1_serialized = re.search(r'<textarea [^>]*>({[^<]+})<', output)[1] pt1_json = pt1_serialized.replace('"', '"') pt1_expected = GEOSGeometry(form.data['pt1']).transform(3857, clone=True) self.assertJSONEqual(pt1_json, pt1_expected.json) diff --git a/tests/inspectdb/tests.py b/tests/inspectdb/tests.py index afe89e0dda..910082510a 100644 --- a/tests/inspectdb/tests.py +++ b/tests/inspectdb/tests.py @@ -52,7 +52,7 @@ class InspectDBTestCase(TestCase): output = out.getvalue() def assertFieldType(name, definition): - out_def = re.search(r'^\s*%s = (models.*)$' % name, output, re.MULTILINE).groups()[0] + out_def = re.search(r'^\s*%s = (models.*)$' % name, output, re.MULTILINE)[1] self.assertEqual(definition, out_def) return assertFieldType diff --git a/tests/template_backends/test_dummy.py b/tests/template_backends/test_dummy.py index 598397a711..4a181d8cff 100644 --- a/tests/template_backends/test_dummy.py +++ b/tests/template_backends/test_dummy.py @@ -84,7 +84,7 @@ class TemplateStringsTests(SimpleTestCase): expected = '<input type="hidden" name="csrfmiddlewaretoken" value="([^"]+)">' match = re.match(expected, content) or re.match(expected.replace('"', "'"), content) self.assertTrue(match, "hidden csrftoken field not found in output") - self.assertTrue(equivalent_tokens(match.group(1), get_token(request))) + self.assertTrue(equivalent_tokens(match[1], get_token(request))) def test_no_directory_traversal(self): with self.assertRaises(TemplateDoesNotExist): diff --git a/tests/test_client_regress/views.py b/tests/test_client_regress/views.py index db4206ce75..0b238381dd 100644 --- a/tests/test_client_regress/views.py +++ b/tests/test_client_regress/views.py @@ -117,7 +117,7 @@ def return_text_file(request): "A view that parses and returns text as a file." match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) if match: - charset = match.group(1) + charset = match[1] else: charset = settings.DEFAULT_CHARSET diff --git a/tests/view_tests/tests/test_debug.py b/tests/view_tests/tests/test_debug.py index a5301b88b7..a305b77a1b 100644 --- a/tests/view_tests/tests/test_debug.py +++ b/tests/view_tests/tests/test_debug.py @@ -154,7 +154,7 @@ class DebugViewTests(SimpleTestCase): self.assertContains(response, '<div class="context" id="', status_code=500) match = re.search(b'<div class="context" id="(?P<id>[^"]+)">', response.content) self.assertIsNotNone(match) - id_repr = match.group('id') + id_repr = match['id'] self.assertFalse( re.search(b'[^c0-9]', id_repr), "Numeric IDs in debug response HTML page shouldn't be localized (value: %s)." % id_repr.decode() |
