enable/disable event listeners

574 views
Skip to first unread message

Антонио Антуан

unread,
Jul 4, 2017, 7:54:51 AM7/4/17
to sqlalchemy
Hi all.

In case of unit-testing I need to disable all event listeners on model/session/etc. When particular test finished I need to enable all listeners.
Is there any ways to achieve this?

Антонио Антуан

unread,
Jul 7, 2017, 3:07:12 PM7/7/17
to sqlalchemy
Looks like it is not possible, isn't it?

Jonathan Vanasco

unread,
Jul 7, 2017, 4:25:47 PM7/7/17
to sqlalchemy
The docs give information on how to register and remove event listeners:


It may be easier for you to keep the event listeners "on", and have them look for a toggle value to enable/disable.

Mike Bayer

unread,
Jul 7, 2017, 5:00:02 PM7/7/17
to sqlal...@googlegroups.com
oh, sorry, forgot this.

you need to register a cleanup for each event you add, such as:

def setUp(self):
event.listen(SomeThing, "some_event", my_handler)
self.addCleanup(event.remove, SomeThing, "some_event", my_handler)

or whatever mechanism your test suite provides.

There of course is a way to clear out event handlers globally but
SQLAlchemy reserves the right to use its own handlers internally and
these can't be cleared out without breaking the library.


On Fri, Jul 7, 2017 at 3:07 PM, Антонио Антуан <a.ch...@gmail.com> wrote:
> Looks like it is not possible, isn't it?
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Антонио Антуан

unread,
Jul 8, 2017, 4:13:17 AM7/8/17
to sqlalchemy
 

On Fri, Jul 7, 2017 at 3:07 PM, Антонио Антуан <a.ch...@gmail.com> wrote:
> Looks like it is not possible, isn't it?
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.


суббота, 8 июля 2017 г., 0:00:02 UTC+3 пользователь Mike Bayer написал:
oh, sorry, forgot this.

you need to register a cleanup for each event you add, such as:

def setUp(self):
    event.listen(SomeThing, "some_event", my_handler)
    self.addCleanup(event.remove, SomeThing, "some_event", my_handler)

or whatever mechanism your test suite provides.

There of course is a way to clear out event handlers globally but
SQLAlchemy reserves the right to use its own handlers internally and
these can't be cleared out without breaking the library.

I don't know (and in case of tests, don't want to know) what events are bound to my model, session and query. 
So, I want to call something like that:


from my_project.model import Session, UsedModel


@pytest.fixture(scope='session', autouse=True)
def disable_sqla_events():
   
# setup
   
Session.disable_all_bound_events()
   
UsedModel.disable_all_bound_events()
   
yield
   
# teardown
   
Session.enable_events()
   
UsedModel.enable_events()

Looks like such mechanism does not available, so I should implement it. 
I'd tried to understand, where and how events-objects stored and bouned with particular sessions and models, but I've failed :)

Антонио Антуан

unread,
Jul 8, 2017, 6:56:09 AM7/8/17
to sqlalchemy
suggested solution (stackoverslow):
import ctypes
from sqlalchemy import event

def clear_event_listeners(model_or_session):
    keys
= [k for k in event.registry._key_to_collection if k[0] == id(model_or_session)]
   
for key in keys:
        target
= model_or_session
        identifier
= key[1]
        fn
= ctypes.cast(key[2], ctypes.py_object).value  # get function by id
       
event.remove(target, identifier, fn)


суббота, 8 июля 2017 г., 11:13:17 UTC+3 пользователь Антонио Антуан написал:
Reply all
Reply to author
Forward
0 new messages