From ee1c1c696b422f482a1611344f8c0a9db16f8808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Sat, 19 Nov 2016 19:14:37 -0800 Subject: [PATCH] Made prefetch_related SQL inspection tests less brittle. After refs #16614, integers might appear outside the WHERE clause. --- tests/prefetch_related/tests.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py index f88cb1c0e6..7ddf4a4c96 100644 --- a/tests/prefetch_related/tests.py +++ b/tests/prefetch_related/tests.py @@ -44,6 +44,13 @@ class PrefetchRelatedTests(TestCase): cls.reader1.books_read.add(cls.book1, cls.book4) cls.reader2.books_read.add(cls.book2, cls.book4) + def assertWhereContains(self, sql, needle): + where_idx = sql.index('WHERE') + self.assertEqual( + sql.count(str(needle), where_idx), 1, + msg="WHERE clause doesn't contain %s, actual SQL: %s" % (needle, sql[where_idx:]) + ) + def test_m2m_forward(self): with self.assertNumQueries(2): lists = [list(b.authors.all()) for b in Book.objects.prefetch_related('authors')] @@ -238,21 +245,21 @@ class PrefetchRelatedTests(TestCase): list(Book.objects.prefetch_related('authors__addresses')) sql = queries[-1]['sql'] - self.assertEqual(sql.count(self.author1.name), 1) + self.assertWhereContains(sql, self.author1.name) def test_m2m_then_m2m_object_ids(self): with CaptureQueriesContext(connection) as queries: list(Book.objects.prefetch_related('authors__favorite_authors')) sql = queries[-1]['sql'] - self.assertEqual(sql.count(self.author1.name), 1) + self.assertWhereContains(sql, self.author1.name) def test_m2m_then_reverse_one_to_one_object_ids(self): with CaptureQueriesContext(connection) as queries: list(Book.objects.prefetch_related('authors__authorwithage')) sql = queries[-1]['sql'] - self.assertEqual(sql.count(str(self.author1.id)), 1, sql) + self.assertWhereContains(sql, self.author1.id) class CustomPrefetchTests(TestCase):