replace assertation with more targeted logging check (#5045)

This commit is contained in:
Matthias Mair 2023-06-14 23:34:00 +02:00 committed by GitHub
parent be6ab14c9b
commit 2322a98068
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 4 deletions

View File

@ -1,12 +1,14 @@
"""Sample plugin which responds to events."""
import warnings
import logging
from django.conf import settings
from plugin import InvenTreePlugin
from plugin.mixins import EventMixin
logger = logging.getLogger('inventree')
class EventPluginSample(EventMixin, InvenTreePlugin):
"""A sample plugin which provides supports for triggered events."""
@ -23,4 +25,4 @@ class EventPluginSample(EventMixin, InvenTreePlugin):
# Issue warning that we can test for
if settings.PLUGIN_TESTING:
warnings.warn(f'Event `{event}` triggered', stacklevel=2)
logger.debug(f'Event `{event}` triggered in sample plugin')

View File

@ -8,6 +8,8 @@ from plugin.base.event.events import trigger_event
from plugin.helpers import MixinNotImplementedError
from plugin.mixins import EventMixin
from .event_sample import logger
class EventPluginSampleTests(TestCase):
"""Tests for EventPluginSample."""
@ -22,9 +24,9 @@ class EventPluginSampleTests(TestCase):
# Enable event testing
settings.PLUGIN_TESTING_EVENTS = True
# Check that an event is issued
with self.assertWarns(Warning) as cm:
with self.assertLogs(logger=logger, level="DEBUG") as cm:
trigger_event('test.event')
self.assertEqual(cm.warning.args[0], 'Event `test.event` triggered')
self.assertIn('DEBUG:inventree:Event `test.event` triggered in sample plugin', cm[1])
# Disable again
settings.PLUGIN_TESTING_EVENTS = False