Fix unit test (#6084)

* Add debug print statements

* Add more debug prints

* Upate unit test

- Use global variables to ensure correct error is raised
- collect() does not always collect in the same "order"
This commit is contained in:
Oliver 2023-12-15 17:23:33 +11:00 committed by GitHub
parent 64671dce20
commit 2c45d88d15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View File

@ -832,6 +832,7 @@ def inheritors(cls):
"""Return all classes that are subclasses from the supplied cls.""" """Return all classes that are subclasses from the supplied cls."""
subcls = set() subcls = set()
work = [cls] work = [cls]
while work: while work:
parent = work.pop() parent = work.pop()
for child in parent.__subclasses__(): for child in parent.__subclasses__():

View File

@ -4,6 +4,13 @@ from InvenTree.unit_test import InvenTreeTestCase
from .transition import StateTransitionMixin, TransitionMethod, storage from .transition import StateTransitionMixin, TransitionMethod, storage
# Global variables to determine which transition classes raises an exception
global raise_storage
global raise_function
raise_storage = False
raise_function = False
class MyPrivateError(NotImplementedError): class MyPrivateError(NotImplementedError):
"""Error for testing purposes.""" """Error for testing purposes."""
@ -16,6 +23,7 @@ def dflt(*args, **kwargs):
def _clean_storage(refs): def _clean_storage(refs):
"""Clean the storage.""" """Clean the storage."""
for ref in refs: for ref in refs:
del ref del ref
storage.collect() storage.collect()
@ -38,9 +46,20 @@ class TransitionTests(InvenTreeTestCase):
def test_storage(self): def test_storage(self):
"""Ensure that the storage collection mechanism works.""" """Ensure that the storage collection mechanism works."""
global raise_storage
global raise_function
raise_storage = True
raise_function = False
class RaisingImplementation(TransitionMethod): class RaisingImplementation(TransitionMethod):
def transition(self, *args, **kwargs): def transition(self, *args, **kwargs):
raise MyPrivateError('RaisingImplementation') """Custom transition method."""
global raise_storage
if raise_storage:
raise MyPrivateError('RaisingImplementation')
# Ensure registering works # Ensure registering works
storage.collect() storage.collect()
@ -58,6 +77,12 @@ class TransitionTests(InvenTreeTestCase):
def test_function(self): def test_function(self):
"""Ensure that a TransitionMethod's function is called.""" """Ensure that a TransitionMethod's function is called."""
global raise_storage
global raise_function
raise_storage = False
raise_function = True
# Setup # Setup
class ValidImplementationNoEffect(TransitionMethod): class ValidImplementationNoEffect(TransitionMethod):
def transition(self, *args, **kwargs): def transition(self, *args, **kwargs):

View File

@ -71,6 +71,7 @@ class StateTransitionMixin:
instance: Object instance instance: Object instance
default_action: Default action to be taken if none of the transitions returns a boolean true value default_action: Default action to be taken if none of the transitions returns a boolean true value
""" """
# Check if there is a custom override function for this transition # Check if there is a custom override function for this transition
for override in storage.list: for override in storage.list:
rslt = override.transition(current_state, target_state, instance, default_action, **kwargs) rslt = override.transition(current_state, target_state, instance, default_action, **kwargs)