2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-17 12:35:46 +00:00
Files
InvenTree/InvenTree/common/test_notifications.py
Matthias Mair 0c97a50e47 Docstring checks in QC checks (#3089)
* Add pre-commit to the stack

* exclude static

* Add locales to excludes

* fix style errors

* rename pipeline steps

* also wait on precommit

* make template matching simpler

* Use the same code for python setup everywhere

* use step and cache for python setup

* move regular settings up into general envs

* just use full update

* Use invoke instead of static references

* make setup actions more similar

* use python3

* refactor names to be similar

* fix runner version

* fix references

* remove incidential change

* use matrix for os

* Github can't do this right now

* ignore docstyle errors

* Add seperate docstring test

* update flake call

* do not fail on docstring

* refactor setup into workflow

* update reference

* switch to action

* resturcture

* add bash statements

* remove os from cache

* update input checks

* make code cleaner

* fix boolean

* no relative paths

* install wheel by python

* switch to install

* revert back to simple wheel

* refactor import export tests

* move setup keys back to not disturbe tests

* remove docstyle till that is fixed

* update references

* continue on error

* add docstring test

* use relativ action references

* Change step / job docstrings

* update to merge

* reformat comments 1

* fix docstrings 2

* fix docstrings 3

* fix docstrings 4

* fix docstrings 5

* fix docstrings 6

* fix docstrings 7

* fix docstrings 8

* fix docstirns 9

* fix docstrings 10

* docstring adjustments

* update the remaining docstrings

* small docstring changes

* fix function name

* update support files for docstrings

* Add missing args to docstrings

* Remove outdated function

* Add docstrings for the 'build' app

* Make API code cleaner

* add more docstrings for plugin app

* Remove dead code for plugin settings
No idea what that was even intended for

* ignore __init__ files for docstrings

* More docstrings

* Update docstrings for the 'part' directory

* Fixes for related_part functionality

* Fix removed stuff from merge 99676ee

* make more consistent

* Show statistics for docstrings

* add more docstrings

* move specific register statements to make them clearer to understant

* More docstrings for common

* and more docstrings

* and more

* simpler call

* docstrings for notifications

* docstrings for common/tests

* Add docs for common/models

* Revert "move specific register statements to make them clearer to understant"

This reverts commit ca96654622.

* use typing here

* Revert "Make API code cleaner"

This reverts commit 24fb68bd3e.

* docstring updates for the 'users' app

* Add generic Meta info to simple Meta classes

* remove unneeded unique_together statements

* More simple metas

* Remove unnecessary format specifier

* Remove extra json format specifiers

* Add docstrings for the 'plugin' app

* Docstrings for the 'label' app

* Add missing docstrings for the 'report' app

* Fix build test regression

* Fix top-level files

* docstrings for InvenTree/InvenTree

* reduce unneeded code

* add docstrings

* and more docstrings

* more docstrings

* more docstrings for stock

* more docstrings

* docstrings for order/views

* Docstrings for various files in the 'order' app

* Docstrings for order/test_api.py

* Docstrings for order/serializers.py

* Docstrings for order/admin.py

* More docstrings for the order app

* Add docstrings for the 'company' app

* Add unit tests for rebuilding the reference fields

* Prune out some more dead code

* remove more dead code

Co-authored-by: Oliver Walters <oliver.henry.walters@gmail.com>
2022-06-02 01:37:39 +10:00

168 lines
5.7 KiB
Python

"""Tests for basic notification methods and functions in InvenTree."""
import plugin.templatetags.plugin_extras as plugin_tags
from common.notifications import (BulkNotificationMethod, NotificationMethod,
SingleNotificationMethod, storage)
from part.test_part import BaseNotificationIntegrationTest
from plugin.models import NotificationUserSetting
class BaseNotificationTests(BaseNotificationIntegrationTest):
"""Tests for basic NotificationMethod."""
def test_NotificationMethod(self):
"""Ensure the implementation requirements are tested."""
class FalseNotificationMethod(NotificationMethod):
METHOD_NAME = 'FalseNotification'
class AnotherFalseNotificationMethod(NotificationMethod):
METHOD_NAME = 'AnotherFalseNotification'
def send(self):
"""A comment so we do not need a pass."""
class NoNameNotificationMethod(NotificationMethod):
def send(self):
"""A comment so we do not need a pass."""
class WrongContextNotificationMethod(NotificationMethod):
METHOD_NAME = 'WrongContextNotification'
CONTEXT_EXTRA = [
'aa',
('aa', 'bb', ),
('templates', 'ccc', ),
(123, )
]
def send(self):
"""A comment so we do not need a pass."""
# no send / send bulk
with self.assertRaises(NotImplementedError):
FalseNotificationMethod('', '', '', '', )
# no METHOD_NAME
with self.assertRaises(NotImplementedError):
NoNameNotificationMethod('', '', '', '', )
# a not existant context check
with self.assertRaises(NotImplementedError):
WrongContextNotificationMethod('', '', '', '', )
# no get_targets
with self.assertRaises(NotImplementedError):
AnotherFalseNotificationMethod('', '', '', {'name': 1, 'message': 2, }, )
def test_failing_passing(self):
"""Ensure that an error in one deliverymethod is not blocking all mehthods."""
# cover failing delivery
self._notification_run()
def test_errors_passing(self):
"""Ensure that errors do not kill the whole delivery."""
class ErrorImplementation(SingleNotificationMethod):
METHOD_NAME = 'ErrorImplementation'
def get_targets(self):
return [1, ]
def send(self, target):
raise KeyError('This could be any error')
self._notification_run(ErrorImplementation)
class BulkNotificationMethodTests(BaseNotificationIntegrationTest):
"""Tests for BulkNotificationMethod classes specifically.
General tests for NotificationMethods are in BaseNotificationTests.
"""
def test_BulkNotificationMethod(self):
"""Ensure the implementation requirements are tested.
MixinNotImplementedError needs to raise if the send_bulk() method is not set.
"""
class WrongImplementation(BulkNotificationMethod):
METHOD_NAME = 'WrongImplementationBulk'
def get_targets(self):
return [1, ]
with self.assertRaises(NotImplementedError):
self._notification_run(WrongImplementation)
class SingleNotificationMethodTests(BaseNotificationIntegrationTest):
"""Tests for SingleNotificationMethod classes specifically.
General tests for NotificationMethods are in BaseNotificationTests.
"""
def test_SingleNotificationMethod(self):
"""Ensure the implementation requirements are tested.
MixinNotImplementedError needs to raise if the send() method is not set.
"""
class WrongImplementation(SingleNotificationMethod):
METHOD_NAME = 'WrongImplementationSingle'
def get_targets(self):
return [1, ]
with self.assertRaises(NotImplementedError):
self._notification_run(WrongImplementation)
# A integration test for notifications is provided in test_part.PartNotificationTest
class NotificationUserSettingTests(BaseNotificationIntegrationTest):
"""Tests for NotificationUserSetting."""
def setUp(self):
"""Setup for all tests."""
super().setUp()
self.client.login(username=self.user.username, password='password')
def test_setting_attributes(self):
"""Check notification method plugin methods: usersettings and tags."""
class SampleImplementation(BulkNotificationMethod):
METHOD_NAME = 'test'
GLOBAL_SETTING = 'ENABLE_NOTIFICATION_TEST'
USER_SETTING = {
'name': 'Enable test notifications',
'description': 'Allow sending of test for event notifications',
'default': True,
'validator': bool,
'units': 'alpha',
}
def get_targets(self):
return [1, ]
def send_bulk(self):
return True
# run thorugh notification
self._notification_run(SampleImplementation)
# make sure the array fits
array = storage.get_usersettings(self.user)
setting = NotificationUserSetting.objects.all().first()
# assertions for settings
self.assertEqual(setting.name, 'Enable test notifications')
self.assertEqual(setting.default_value, True)
self.assertEqual(setting.description, 'Allow sending of test for event notifications')
self.assertEqual(setting.units, 'alpha')
# test tag and array
self.assertEqual(plugin_tags.notification_settings_list({'user': self.user}), array)
self.assertEqual(array[0]['key'], 'NOTIFICATION_METHOD_TEST')
self.assertEqual(array[0]['method'], 'test')