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 .…