2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 13:05:42 +00:00

Merge branch 'inventree:master' into matmair/issue2524

This commit is contained in:
Matthias Mair
2022-03-16 15:15:27 +01:00
committed by GitHub
26 changed files with 336 additions and 76 deletions

View File

@ -12,7 +12,7 @@ class SettingsAdmin(ImportExportModelAdmin):
list_display = ('key', 'value')
def get_readonly_fields(self, request, obj=None):
def get_readonly_fields(self, request, obj=None): # pragma: no cover
"""
Prevent the 'key' field being edited once the setting is created
"""
@ -27,7 +27,7 @@ class UserSettingsAdmin(ImportExportModelAdmin):
list_display = ('key', 'value', 'user', )
def get_readonly_fields(self, request, obj=None):
def get_readonly_fields(self, request, obj=None): # pragma: no cover
"""
Prevent the 'key' field being edited once the setting is created
"""

View File

@ -570,7 +570,7 @@ class BaseInvenTreeSetting(models.Model):
try:
value = int(self.value)
except (ValueError, TypeError):
value = self.default_value()
value = self.default_value
return value

View File

@ -18,12 +18,12 @@ def currency_code_default():
try:
code = InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY')
except ProgrammingError:
except ProgrammingError: # pragma: no cover
# database is not initialized yet
code = ''
if code not in CURRENCIES:
code = 'USD'
code = 'USD' # pragma: no cover
return code

View File

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from django.test import TestCase
from common.models import NotificationEntry
from InvenTree.tasks import offload_task
class TaskTest(TestCase):
"""
Tests for common tasks
"""
def test_delete(self):
# check empty run
self.assertEqual(NotificationEntry.objects.all().count(), 0)
offload_task('common.tasks.delete_old_notifications',)

View File

@ -6,7 +6,9 @@ from datetime import timedelta
from django.test import TestCase, Client
from django.contrib.auth import get_user_model
from django.urls import reverse
from InvenTree.api_tester import InvenTreeAPITestCase
from .models import InvenTreeSetting, WebhookEndpoint, WebhookMessage, NotificationEntry
from .api import WebhookView
@ -46,6 +48,67 @@ class SettingsTest(TestCase):
# Check object lookup (case insensitive)
self.assertEqual(InvenTreeSetting.get_setting_object('iNvEnTrEE_inSTanCE').pk, 1)
def test_settings_functions(self):
"""
Test settings functions and properties
"""
# define settings to check
instance_ref = 'INVENTREE_INSTANCE'
instance_obj = InvenTreeSetting.get_setting_object(instance_ref)
stale_ref = 'STOCK_STALE_DAYS'
stale_days = InvenTreeSetting.get_setting_object(stale_ref)
report_size_obj = InvenTreeSetting.get_setting_object('REPORT_DEFAULT_PAGE_SIZE')
report_test_obj = InvenTreeSetting.get_setting_object('REPORT_ENABLE_TEST_REPORT')
# check settings base fields
self.assertEqual(instance_obj.name, 'InvenTree Instance Name')
self.assertEqual(instance_obj.get_setting_name(instance_ref), 'InvenTree Instance Name')
self.assertEqual(instance_obj.description, 'String descriptor for the server instance')
self.assertEqual(instance_obj.get_setting_description(instance_ref), 'String descriptor for the server instance')
# check units
self.assertEqual(instance_obj.units, '')
self.assertEqual(instance_obj.get_setting_units(instance_ref), '')
self.assertEqual(instance_obj.get_setting_units(stale_ref), 'days')
# check as_choice
self.assertEqual(instance_obj.as_choice(), 'My very first InvenTree Instance')
self.assertEqual(report_size_obj.as_choice(), 'A4')
# check is_choice
self.assertEqual(instance_obj.is_choice(), False)
self.assertEqual(report_size_obj.is_choice(), True)
# check setting_type
self.assertEqual(instance_obj.setting_type(), 'string')
self.assertEqual(report_test_obj.setting_type(), 'boolean')
self.assertEqual(stale_days.setting_type(), 'integer')
# check as_int
self.assertEqual(stale_days.as_int(), 0)
self.assertEqual(instance_obj.as_int(), 'InvenTree server') # not an int -> return default
# check as_bool
self.assertEqual(report_test_obj.as_bool(), True)
# check to_native_value
self.assertEqual(stale_days.to_native_value(), 0)
def test_allValues(self):
"""
Make sure that the allValues functions returns correctly
"""
# define testing settings
# check a few keys
result = InvenTreeSetting.allValues()
self.assertIn('INVENTREE_INSTANCE', result)
self.assertIn('PART_COPY_TESTS', result)
self.assertIn('STOCK_OWNERSHIP_CONTROL', result)
self.assertIn('SIGNUP_GROUP', result)
def test_required_values(self):
"""
- Ensure that every global setting has a name.
@ -93,6 +156,14 @@ class SettingsTest(TestCase):
raise ValueError(f'Non-boolean default value specified for {key}') # pragma: no cover
class SettingsApiTest(InvenTreeAPITestCase):
def test_settings_api(self):
# test setting with choice
url = reverse('api-user-setting-list')
self.get(url, expected_code=200)
class WebhookMessageTests(TestCase):
def setUp(self):
self.endpoint_def = WebhookEndpoint.objects.create()
@ -223,3 +294,26 @@ class NotificationTest(TestCase):
self.assertFalse(NotificationEntry.check_recent('test.notification2', 1, delta))
self.assertTrue(NotificationEntry.check_recent('test.notification', 1, delta))
class LoadingTest(TestCase):
"""
Tests for the common config
"""
def test_restart_flag(self):
"""
Test that the restart flag is reset on start
"""
import common.models
from plugin import registry
# set flag true
common.models.InvenTreeSetting.set_setting('SERVER_RESTART_REQUIRED', False, None)
# reload the app
registry.reload_plugins()
# now it should be false again
self.assertFalse(common.models.InvenTreeSetting.get_setting('SERVER_RESTART_REQUIRED'))