From 728e0894aa7c8a62541ecd5e801bf03a1de8452d Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Mon, 23 Dec 2024 21:20:27 +0100 Subject: [PATCH] Factor out helper (#8744) * facour out helper * fix import path --- src/backend/InvenTree/InvenTree/tests.py | 18 +++++++----------- src/backend/InvenTree/InvenTree/unit_test.py | 7 +++++++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/tests.py b/src/backend/InvenTree/InvenTree/tests.py index 5ed177be40..91be4f865a 100644 --- a/src/backend/InvenTree/InvenTree/tests.py +++ b/src/backend/InvenTree/InvenTree/tests.py @@ -32,7 +32,7 @@ from common.currency import currency_codes from common.models import CustomUnit, InvenTreeSetting from InvenTree.helpers_mixin import ClassProviderMixin, ClassValidationMixin from InvenTree.sanitizer import sanitize_svg -from InvenTree.unit_test import InvenTreeTestCase +from InvenTree.unit_test import InvenTreeTestCase, in_env_context from part.models import Part, PartCategory from stock.models import StockItem, StockLocation @@ -1121,10 +1121,6 @@ class TestSettings(InvenTreeTestCase): superuser = True - def in_env_context(self, envs): - """Patch the env to include the given dict.""" - return mock.patch.dict(os.environ, envs) - def run_reload(self, envs=None): """Helper function to reload InvenTree.""" # Set default - see B006 @@ -1133,7 +1129,7 @@ class TestSettings(InvenTreeTestCase): from plugin import registry - with self.in_env_context(envs): + with in_env_context(envs): settings.USER_ADDED = False registry.reload_plugins() @@ -1198,7 +1194,7 @@ class TestSettings(InvenTreeTestCase): ) # with env set - with self.in_env_context({ + with in_env_context({ 'INVENTREE_CONFIG_FILE': '_testfolder/my_special_conf.yaml' }): self.assertIn( @@ -1217,7 +1213,7 @@ class TestSettings(InvenTreeTestCase): ) # with env set - with self.in_env_context({ + with in_env_context({ 'INVENTREE_PLUGIN_FILE': '_testfolder/my_special_plugins.txt' }): self.assertIn( @@ -1231,7 +1227,7 @@ class TestSettings(InvenTreeTestCase): self.assertEqual(config.get_setting(TEST_ENV_NAME, None, '123!'), '123!') # with env set - with self.in_env_context({TEST_ENV_NAME: '321'}): + with in_env_context({TEST_ENV_NAME: '321'}): self.assertEqual(config.get_setting(TEST_ENV_NAME, None), '321') # test typecasting to dict - None should be mapped to empty dict @@ -1240,13 +1236,13 @@ class TestSettings(InvenTreeTestCase): ) # test typecasting to dict - valid JSON string should be mapped to corresponding dict - with self.in_env_context({TEST_ENV_NAME: '{"a": 1}'}): + with in_env_context({TEST_ENV_NAME: '{"a": 1}'}): self.assertEqual( config.get_setting(TEST_ENV_NAME, None, typecast=dict), {'a': 1} ) # test typecasting to dict - invalid JSON string should be mapped to empty dict - with self.in_env_context({TEST_ENV_NAME: "{'a': 1}"}): + with in_env_context({TEST_ENV_NAME: "{'a': 1}"}): self.assertEqual(config.get_setting(TEST_ENV_NAME, None, typecast=dict), {}) diff --git a/src/backend/InvenTree/InvenTree/unit_test.py b/src/backend/InvenTree/InvenTree/unit_test.py index 6bc758b71d..6511c384fa 100644 --- a/src/backend/InvenTree/InvenTree/unit_test.py +++ b/src/backend/InvenTree/InvenTree/unit_test.py @@ -3,10 +3,12 @@ import csv import io import json +import os import re import time from contextlib import contextmanager from pathlib import Path +from unittest import mock from django.contrib.auth import get_user_model from django.contrib.auth.models import Group, Permission @@ -601,3 +603,8 @@ class AdminTestCase(InvenTreeAPITestCase): self.assertEqual(response.status_code, 200) return obj + + +def in_env_context(envs): + """Patch the env to include the given dict.""" + return mock.patch.dict(os.environ, envs)