mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 20:45:44 +00:00
More plugin testing (#3052)
* Add a check of a child panel too * do not cover error catching * test for implementation error * Add warning to test for * Add test for event_sample * ignore safety switches * Add a settings flag to enable event testing * test if not implemented is raises * raise plugin specific errors * use plugin specific error * fix assertation * add test for mixin * this point can't be reached * add tests for locate plugin * fix assertations * fix function call * refert switch * this is already caught by the internal API * also cover mixin redirect
This commit is contained in:
@ -2,6 +2,10 @@
|
||||
Sample plugin which responds to events
|
||||
"""
|
||||
|
||||
import warnings
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from plugin import InvenTreePlugin
|
||||
from plugin.mixins import EventMixin
|
||||
|
||||
@ -21,3 +25,7 @@ class EventPluginSample(EventMixin, InvenTreePlugin):
|
||||
print(f"Processing triggered event: '{event}'")
|
||||
print("args:", str(args))
|
||||
print("kwargs:", str(kwargs))
|
||||
|
||||
# Issue warning that we can test for
|
||||
if settings.PLUGIN_TESTING:
|
||||
warnings.warn(f'Event `{event}` triggered')
|
||||
|
40
InvenTree/plugin/samples/event/test_event_sample.py
Normal file
40
InvenTree/plugin/samples/event/test_event_sample.py
Normal file
@ -0,0 +1,40 @@
|
||||
"""Unit tests for event_sample sample plugins"""
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from plugin import InvenTreePlugin, registry
|
||||
from plugin.base.event.events import trigger_event
|
||||
from plugin.helpers import MixinNotImplementedError
|
||||
from plugin.mixins import EventMixin
|
||||
|
||||
|
||||
class EventPluginSampleTests(TestCase):
|
||||
"""Tests for EventPluginSample"""
|
||||
|
||||
def test_run_event(self):
|
||||
"""Check if the event is issued"""
|
||||
# Activate plugin
|
||||
config = registry.get_plugin('sampleevent').plugin_config()
|
||||
config.active = True
|
||||
config.save()
|
||||
|
||||
# Enable event testing
|
||||
settings.PLUGIN_TESTING_EVENTS = True
|
||||
# Check that an event is issued
|
||||
with self.assertWarns(Warning) as cm:
|
||||
trigger_event('test.event')
|
||||
self.assertEqual(cm.warning.args[0], 'Event `test.event` triggered')
|
||||
|
||||
# Disable again
|
||||
settings.PLUGIN_TESTING_EVENTS = False
|
||||
|
||||
def test_mixin(self):
|
||||
"""Test that MixinNotImplementedError is raised"""
|
||||
|
||||
with self.assertRaises(MixinNotImplementedError):
|
||||
class Wrong(EventMixin, InvenTreePlugin):
|
||||
pass
|
||||
|
||||
plugin = Wrong()
|
||||
plugin.process_event('abc')
|
Reference in New Issue
Block a user