I work in a very large Django code base, and our data is crazy, we have a lot of databases. Our tests look like this:
class TestThing(TestCase):
databases = {"db1", "db2", "db3", "db4", ...}
def test_thing(self):
with self.assertNumQueries(4, using="db1"), self.assertNumQueries(0, using="db2"), self.assertNumQueries(1, using="db3"), self.assertNumQueries(0, using="db4"):
thing()
While correct, and explicit… I fear it’s… too explicit, not to mention, we have over 4 thousand tests, and it’s an absolute mess.
Actual numbers
$ grep "def test_" **/tests/*.py | wc -l
4179
I propose, we create a new function at the SimpleTestCase
level called something like assertAllNumQueries
that uses the class’s databases
and would make the code a bit simpler to read:
class TestThing(TestCase):
databases = {"db1", "db2", "db3", "db4", ...}
def test_thing(self):
with self.assertAllNumQueries(5):
thing()
or keep what we have, and just expand using
to allow for __all__
value.
class TestThing(TestCase):
databases = {"db1", "db2", "db3", "db4", ...}
def test_thing(self):
# Note that __all__ in this case would be a sum() of all the queries that need
# to be equal to 5. Not 5 queries each database, but collectively 5 total.
with self.assertNumQueries(5, using="__all__"):
thing()
One clarification: I’m not concerned with which specific database (e.g., db1 versus db4) executes the query. My goal is to detect significant increases in the overall query count. For example, if a refactor causes the total number of queries to jump tenfold, that warrants further investigation. In contrast, a change from 20 to 21 queries is trivial and acceptable if the alteration is justified.
What do you guys think?