Say I've added a few event listeners to a model and I wanted to get a list of all of these added events for the model to verify their existence during testing with assertions. Is there a way to do so?
I'm aware of SQLAlchemy's inspect
, which I currently use to assert the presence of columns and relationships. But, is there a way to obtain the list of custom event listeners through inspect
as well? If not, is there any other way of doing so? I'd like to obtain only the events that have been added to the models explicitly, not those that are present by default (if possible).
Example of how I expect to retrieve event listeners:
def test_schema(self):
# sanity checks
# this will raise any flags in the event schema is modified, so we know to update the appropriate tests
assert tuple(inspect(MyModel).columns.keys()) == (
"id", "module", "slug", "display_name"
)
assert tuple(inspect(MyModel).relationships.keys()) == ("accounts", "reports", "jobs")
assert tuple(inspect(MyModel).events) == (
"{event_function_name}_{trigger_action}",
"{notify_manager_of_billing_changes}_{after_update}"
)
def notify_manager_of_billing_changes(mapper, connection, model_instance):
print(model_instance.billing_address)
from sqlalchemy import event
event.listen(MyModel, "after_update", notify_manager_of_billing_changes, retval=False)