]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
tests: ensure we don't see database errors during duplicate insert
authorJeremy Kerr <jk@ozlabs.org>
Thu, 16 Apr 2020 01:29:25 +0000 (09:29 +0800)
committerStephen Finucane <stephen@that.guru>
Sat, 18 Apr 2020 11:55:47 +0000 (12:55 +0100)
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 a60e75e2c6897fd262ec95a35e0e94b9027c11d4)

patchwork/tests/test_parser.py

index 7fe3608fa9dcad0cb6f86a8f9d826c376f491cf7..11dcb21ff9192244591bed9e699e9170717a29d2 100644 (file)
@@ -12,7 +12,9 @@ import os
 import sys
 import unittest
 
+import django
 from django.db.transaction import atomic
+from django.db import connection
 from django.test import TestCase
 from django.test import TransactionTestCase
 from django.utils import six
@@ -1125,6 +1127,10 @@ class WeirdMailTest(TransactionTestCase):
         self._test_patch('x-face.mbox')
 
 
+@unittest.skipIf(
+    django.VERSION < (2, 0),
+    'Django 1.11 does not provide an easy DB query introspection API'
+)
 class DuplicateMailTest(TestCase):
     def setUp(self):
         self.listid = 'patchwork.ozlabs.org'
@@ -1132,15 +1138,30 @@ class DuplicateMailTest(TestCase):
         create_state()
 
     def _test_duplicate_mail(self, mail):
+        errors = []
+
+        def log_query_errors(execute, sql, params, many, context):
+            try:
+                result = execute(sql, params, many, context)
+            except Exception as e:
+                errors.append(e)
+                raise
+            return result
+
         _parse_mail(mail)
+
         with self.assertRaises(DuplicateMailError):
-            # If we see any database errors from the duplicate insert
-            # (typically an IntegrityError), the insert will abort the current
-            # transaction. This atomic() ensures that we can recover, and
-            # perform subsequent queries.
-            with atomic():
-                _parse_mail(mail)
+            with connection.execute_wrapper(log_query_errors):
+                # If we see any database errors from the duplicate insert
+                # (typically an IntegrityError), the insert will abort the
+                # current transaction. This atomic() ensures that we can
+                # recover, and perform subsequent queries.
+                with atomic():
+                    _parse_mail(mail)
+
+        self.assertEqual(errors, [])
 
+    @unittest.expectedFailure
     def test_duplicate_patch(self):
         diff = read_patch('0001-add-line.patch')
         m = create_email(diff, listid=self.listid, msgid='1@example.com')