diff --git a/src/backend/InvenTree/InvenTree/tests.py b/src/backend/InvenTree/InvenTree/tests.py index a8a702b23f..6f32995592 100644 --- a/src/backend/InvenTree/InvenTree/tests.py +++ b/src/backend/InvenTree/InvenTree/tests.py @@ -1148,12 +1148,8 @@ class TestSettings(InvenTreeTestCase): superuser = True - def in_env_context(self, envs=None): + def in_env_context(self, envs): """Patch the env to include the given dict.""" - # Set default - see B006 - if envs is None: - envs = {} - return mock.patch.dict(os.environ, envs) def run_reload(self, envs=None): @@ -1588,15 +1584,15 @@ class ClassValidationMixinTest(TestCase): def test(self): """Test function.""" - pass + ... def test1(self): """Test function.""" - pass + ... def test2(self): """Test function.""" - pass + ... required_attributes = ['NAME'] required_overrides = [test, [test1, test2]] @@ -1616,11 +1612,11 @@ class ClassValidationMixinTest(TestCase): def test(self): """Test function.""" - pass + ... def test2(self): """Test function.""" - pass + ... TestClass.validate() @@ -1643,7 +1639,7 @@ class ClassValidationMixinTest(TestCase): def test2(self): """Test function.""" - pass + ... with self.assertRaisesRegex( NotImplementedError, diff --git a/src/backend/InvenTree/InvenTree/unit_test.py b/src/backend/InvenTree/InvenTree/unit_test.py index 38713e0fb0..4ad2d68b7b 100644 --- a/src/backend/InvenTree/InvenTree/unit_test.py +++ b/src/backend/InvenTree/InvenTree/unit_test.py @@ -157,12 +157,14 @@ class UserMixin: if type(assign_all) is not bool: # Raise exception if common mistake is made! - raise TypeError('assignRole: assign_all must be a boolean value') + raise TypeError( + 'assignRole: assign_all must be a boolean value' + ) # pragma: no cover if not role and not assign_all: raise ValueError( 'assignRole: either role must be provided, or assign_all must be set' - ) + ) # pragma: no cover if not assign_all and role: rule, perm = role.split('.') @@ -241,14 +243,18 @@ class InvenTreeAPITestCase(ExchangeRateMixin, UserMixin, APITestCase): yield # your test will be run here if verbose: - msg = '\r\n%s' % json.dumps(context.captured_queries, indent=4) + msg = '\r\n%s' % json.dumps( + context.captured_queries, indent=4 + ) # pragma: no cover else: msg = None n = len(context.captured_queries) if debug: - print(f'Expected less than {value} queries, got {n} queries') + print( + f'Expected less than {value} queries, got {n} queries' + ) # pragma: no cover self.assertLess(n, value, msg=msg) @@ -258,7 +264,7 @@ class InvenTreeAPITestCase(ExchangeRateMixin, UserMixin, APITestCase): if expected_code is None: return - if expected_code != response.status_code: + if expected_code != response.status_code: # pragma: no cover print( f"Unexpected {method} response at '{url}': status_code = {response.status_code}" ) @@ -280,11 +286,7 @@ class InvenTreeAPITestCase(ExchangeRateMixin, UserMixin, APITestCase): response = self.client.options(url) self.assertEqual(response.status_code, 200) - actions = response.data.get('actions', None) - - if not actions: - actions = {} - + actions = response.data.get('actions', {}) return actions def get(self, url, data=None, expected_code=200, format='json', **kwargs): diff --git a/src/backend/InvenTree/common/test_tasks.py b/src/backend/InvenTree/common/test_tasks.py index e8e86e294d..c477864805 100644 --- a/src/backend/InvenTree/common/test_tasks.py +++ b/src/backend/InvenTree/common/test_tasks.py @@ -46,7 +46,7 @@ class NewsFeedTests(TestCase): """Tests that news feed is updated when accessing a valid URL.""" try: common_tasks.update_news_feed() - except Exception as ex: + except Exception as ex: # pragma: no cover self.fail(f'News feed raised exceptions: {ex}') self.assertNotEqual(NewsFeedEntry.objects.all().count(), 0) diff --git a/src/backend/InvenTree/common/tests.py b/src/backend/InvenTree/common/tests.py index 744237ba5b..a45a7ad1f6 100644 --- a/src/backend/InvenTree/common/tests.py +++ b/src/backend/InvenTree/common/tests.py @@ -1048,18 +1048,18 @@ class ColorThemeTest(TestCase): """Test that default choices are returned.""" result = ColorTheme.get_color_themes_choices() - # skip + # skip due to directories not being set up if not result: - return + return # pragma: no cover self.assertIn(('default', 'Default'), result) def test_valid_choice(self): """Check that is_valid_choice works correctly.""" result = ColorTheme.get_color_themes_choices() - # skip + # skip due to directories not being set up if not result: - return + return # pragma: no cover # check wrong reference self.assertFalse(ColorTheme.is_valid_choice('abcdd')) @@ -1099,10 +1099,12 @@ class CurrencyAPITests(InvenTreeAPITestCase): # Exit early return - # Delay and try again - time.sleep(10) + # Delay and try again - might have problems with exchange rate endpoint + time.sleep(10) # pragma: no cover - raise TimeoutError('Could not refresh currency exchange data after 5 attempts') + raise TimeoutError( + 'Could not refresh currency exchange data after 5 attempts' + ) # pragma: no cover class NotesImageTest(InvenTreeAPITestCase): diff --git a/src/backend/InvenTree/generic/states/test_transition.py b/src/backend/InvenTree/generic/states/test_transition.py index 39bde4e1d1..985985e7c1 100644 --- a/src/backend/InvenTree/generic/states/test_transition.py +++ b/src/backend/InvenTree/generic/states/test_transition.py @@ -6,10 +6,8 @@ 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): @@ -44,10 +42,8 @@ class TransitionTests(InvenTreeTestCase): def test_storage(self): """Ensure that the storage collection mechanism works.""" global raise_storage - global raise_function raise_storage = True - raise_function = False class RaisingImplementation(TransitionMethod): def transition(self, *args, **kwargs): @@ -73,10 +69,8 @@ class TransitionTests(InvenTreeTestCase): def test_function(self): """Ensure that a TransitionMethod's function is called.""" global raise_storage - global raise_function raise_storage = False - raise_function = True # Setup class ValidImplementationNoEffect(TransitionMethod): @@ -85,12 +79,7 @@ class TransitionTests(InvenTreeTestCase): class ValidImplementation(TransitionMethod): def transition(self, *args, **kwargs): - global raise_function - - if raise_function: - return 1234 - else: - return False + return 1234 storage.collect() self.assertIn(ValidImplementationNoEffect, storage.list) diff --git a/src/backend/InvenTree/generic/states/tests.py b/src/backend/InvenTree/generic/states/tests.py index 55e116edfc..c30da83ae3 100644 --- a/src/backend/InvenTree/generic/states/tests.py +++ b/src/backend/InvenTree/generic/states/tests.py @@ -23,7 +23,7 @@ class GeneralStatus(StatusCode): def GHI(self): # This should be ignored """A invalid function.""" - pass + ... class GeneralStateTest(InvenTreeTestCase): diff --git a/src/backend/InvenTree/label/test_api.py b/src/backend/InvenTree/label/test_api.py index 2f64aac34f..f2e4d728ec 100644 --- a/src/backend/InvenTree/label/test_api.py +++ b/src/backend/InvenTree/label/test_api.py @@ -64,11 +64,6 @@ class LabelTest(InvenTreeAPITestCase): response = self.get(url, {'enabled': False}) self.assertEqual(len(response.data), 0) - # Disable each report - for label in labels: - label.enabled = False - label.save() - # Filter by "enabled" status response = self.get(url, {'enabled': True}) self.assertEqual(len(response.data), 0) diff --git a/src/backend/InvenTree/machine/test_api.py b/src/backend/InvenTree/machine/test_api.py index 1e603bd2c2..f67b48d005 100644 --- a/src/backend/InvenTree/machine/test_api.py +++ b/src/backend/InvenTree/machine/test_api.py @@ -143,7 +143,7 @@ class MachineAPITest(TestMachineRegistryMixin, InvenTreeAPITestCase): for error in errors_msgs: if re.match(pattern, error): break - else: + else: # pragma: no cover errors_str = '\n'.join([f'- {e}' for e in errors_msgs]) self.fail( f"""Error message matching pattern '{pattern}' not found in machine registry errors:\n{errors_str}""" diff --git a/src/backend/InvenTree/stock/test_api.py b/src/backend/InvenTree/stock/test_api.py index a72cf06525..88fe7bb0c0 100644 --- a/src/backend/InvenTree/stock/test_api.py +++ b/src/backend/InvenTree/stock/test_api.py @@ -554,9 +554,6 @@ class StockItemListTest(StockAPITestCase): ) self.assertTrue(len(response.data) < StockItem.objects.count()) - for result in response.data: - self.assertIsNone(result['location']) - # Filter with "cascade=True" response = self.get( self.list_url, {'location': 'null', 'cascade': True}, expected_code=200