Menu

My N+1 test plugin failed 551 tests on someone else's project. Nearly all of them were noise.
📰

My N+1 test plugin failed 551 tests on someone else's project. Nearly all of them were noise.

DEV Community: python·maciej zielinski·about 10 hours ago
#ok9Oco9j
#devtopython#devto#dev#relationship#test
Reading 0:00
15s threshold

I maintain pytest-queryguard, a pytest plugin that fails a test when the code it runs sends one query per row. The problem it goes after is old: a test database holds a handful of rows, so a loop that lazy-loads a relationship costs a few extra queries and nobody notices. In production the same loop runs over hundreds of rows.

This test passes:

def test_list_orders ( session ) : assert len ( list_orders ( session ) ) == 50

Enter fullscreen mode

Exit fullscreen mode

and list_orders looks fine in review:

def list_orders ( session : Session ) -> list [ dict [ str , object ] ] : rows = [ ] for order in session . scalars ( select ( Order ) ) . all ( ) : rows . append ( { "id" : order . id , "customer" : order . customer .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.