Skip to content

Commit 9463c2b

Browse files
jk-ozlabsstephenfin
authored andcommitted
tests: ensure we don't see database errors during duplicate insert
Currently, the parser causes IntegrityErrors while inserting duplicate patches; these tend to pollute database logs. This change adds a check, which currently fails, to ensure we do not cause errors during a duplicate patch parse. Conflicts: patchwork/tests/test_parser.py NOTE(stephenfin): Conflicts are once again due to import reordering. In addition, we have to skip these tests on Django 1.11 since the 'connection.execute_wrapper' context manager was first added in Django 2.0 [1]. [1] https://docs.djangoproject.com/en/dev/releases/2.0/#models Signed-off-by: Jeremy Kerr <jk@ozlabs.org> Signed-off-by: Stephen Finucane <stephen@that.guru> [stephenfin: Add 'expectedFailure' marker to keep all tests green] (cherry picked from commit a60e75e)
1 parent 1493073 commit 9463c2b

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

patchwork/tests/test_parser.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import sys
1313
import unittest
1414

15+
import django
1516
from django.db.transaction import atomic
17+
from django.db import connection
1618
from django.test import TestCase
1719
from django.test import TransactionTestCase
1820
from django.utils import six
@@ -1125,22 +1127,41 @@ def test_x_face(self):
11251127
self._test_patch('x-face.mbox')
11261128

11271129

1130+
@unittest.skipIf(
1131+
django.VERSION < (2, 0),
1132+
'Django 1.11 does not provide an easy DB query introspection API'
1133+
)
11281134
class DuplicateMailTest(TestCase):
11291135
def setUp(self):
11301136
self.listid = 'patchwork.ozlabs.org'
11311137
create_project(listid=self.listid)
11321138
create_state()
11331139

11341140
def _test_duplicate_mail(self, mail):
1141+
errors = []
1142+
1143+
def log_query_errors(execute, sql, params, many, context):
1144+
try:
1145+
result = execute(sql, params, many, context)
1146+
except Exception as e:
1147+
errors.append(e)
1148+
raise
1149+
return result
1150+
11351151
_parse_mail(mail)
1152+
11361153
with self.assertRaises(DuplicateMailError):
1137-
# If we see any database errors from the duplicate insert
1138-
# (typically an IntegrityError), the insert will abort the current
1139-
# transaction. This atomic() ensures that we can recover, and
1140-
# perform subsequent queries.
1141-
with atomic():
1142-
_parse_mail(mail)
1154+
with connection.execute_wrapper(log_query_errors):
1155+
# If we see any database errors from the duplicate insert
1156+
# (typically an IntegrityError), the insert will abort the
1157+
# current transaction. This atomic() ensures that we can
1158+
# recover, and perform subsequent queries.
1159+
with atomic():
1160+
_parse_mail(mail)
1161+
1162+
self.assertEqual(errors, [])
11431163

1164+
@unittest.expectedFailure
11441165
def test_duplicate_patch(self):
11451166
diff = read_patch('0001-add-line.patch')
11461167
m = create_email(diff, listid=self.listid, msgid='1@example.com')

0 commit comments

Comments
 (0)