2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-30 20:46:47 +00:00

Factor out helper (#8744)

* facour out helper

* fix import path
This commit is contained in:
Matthias Mair 2024-12-23 21:20:27 +01:00 committed by GitHub
parent 71eb2814d4
commit 728e0894aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 11 deletions

View File

@ -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), {})

View File

@ -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)