From: Jeremy Kerr Date: Thu, 16 Apr 2020 01:29:25 +0000 (+0800) Subject: tests: ensure we don't see database errors during duplicate insert X-Git-Tag: v2.2.2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9463c2b3038fd278ef1a7de4a370d40862379483;p=thirdparty%2Fpatchwork.git 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 Signed-off-by: Stephen Finucane [stephenfin: Add 'expectedFailure' marker to keep all tests green] (cherry picked from commit a60e75e2c6897fd262ec95a35e0e94b9027c11d4) --- diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py index 7fe3608f..11dcb21f 100644 --- a/patchwork/tests/test_parser.py +++ b/patchwork/tests/test_parser.py @@ -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')