How to count sqlalchemy queries in unit tests

后端 未结 3 1294
萌比男神i
萌比男神i 2021-02-04 02:44

In Django I often assert the number of queries that should be made so that unit tests catch new N+1 query problems

from django import db
from django.conf import          


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 03:36

    Use SQLAlchemy Core Events to log/track queries executed (you can attach it from your unit tests so they don't impact your performance on the actual application:

    event.listen(engine, "before_cursor_execute", catch_queries)
    

    Now you write the function catch_queries, where the way depends on how you test. For example, you could define this function in your test statement:

    def test_something(self):
        stmts = []
        def catch_queries(conn, cursor, statement, ...):
            stmts.append(statement)
        # Now attach it as a listener and work with the collected events after running your test
    

    The above method is just an inspiration. For extended cases you'd probably like to have a global cache of events that you empty after each test. The reason is that prior to 0.9 (current dev) there is no API to remove event listeners. Thus make one global listener that accesses a global list.

提交回复
热议问题