mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Add unit tests for boolean user settings (via the API)
This commit is contained in:
		| @@ -18,8 +18,6 @@ def assign_bom_items(apps, schema_editor): | |||||||
|     BomItem = apps.get_model('part', 'bomitem') |     BomItem = apps.get_model('part', 'bomitem') | ||||||
|     Part = apps.get_model('part', 'part') |     Part = apps.get_model('part', 'part') | ||||||
|  |  | ||||||
|     logger.info("Assigning BomItems to existing BuildItem objects") |  | ||||||
|  |  | ||||||
|     count_valid = 0 |     count_valid = 0 | ||||||
|     count_total = 0 |     count_total = 0 | ||||||
|  |  | ||||||
| @@ -29,6 +27,10 @@ def assign_bom_items(apps, schema_editor): | |||||||
|         # Note: Before this migration, variant stock assignment was not allowed, |         # Note: Before this migration, variant stock assignment was not allowed, | ||||||
|         #       so BomItem lookup should be pretty easy |         #       so BomItem lookup should be pretty easy | ||||||
|  |  | ||||||
|  |         if count_total == 0: | ||||||
|  |             # First time around | ||||||
|  |             logger.info("Assigning BomItems to existing BuildItem objects") | ||||||
|  |  | ||||||
|         count_total += 1 |         count_total += 1 | ||||||
|  |  | ||||||
|         try: |         try: | ||||||
|   | |||||||
| @@ -417,6 +417,9 @@ class BaseInvenTreeSetting(models.Model): | |||||||
|  |  | ||||||
|         super().clean() |         super().clean() | ||||||
|  |  | ||||||
|  |         # Encode as native values | ||||||
|  |         self.value = self.native_value | ||||||
|  |  | ||||||
|         validator = self.__class__.get_setting_validator(self.key, **kwargs) |         validator = self.__class__.get_setting_validator(self.key, **kwargs) | ||||||
|  |  | ||||||
|         if validator is not None: |         if validator is not None: | ||||||
|   | |||||||
| @@ -9,7 +9,9 @@ from django.contrib.auth import get_user_model | |||||||
| from django.urls import reverse | from django.urls import reverse | ||||||
|  |  | ||||||
| from InvenTree.api_tester import InvenTreeAPITestCase | from InvenTree.api_tester import InvenTreeAPITestCase | ||||||
| from .models import InvenTreeSetting, WebhookEndpoint, WebhookMessage, NotificationEntry | from InvenTree.helpers import str2bool | ||||||
|  |  | ||||||
|  | from .models import InvenTreeSetting, InvenTreeUserSetting, WebhookEndpoint, WebhookMessage, NotificationEntry | ||||||
| from .api import WebhookView | from .api import WebhookView | ||||||
|  |  | ||||||
| CONTENT_TYPE_JSON = 'application/json' | CONTENT_TYPE_JSON = 'application/json' | ||||||
| @@ -158,10 +160,97 @@ class SettingsTest(TestCase): | |||||||
|  |  | ||||||
| class SettingsApiTest(InvenTreeAPITestCase): | class SettingsApiTest(InvenTreeAPITestCase): | ||||||
|  |  | ||||||
|     def test_settings_api(self): |     def test_global_settings_api_list(self): | ||||||
|         # test setting with choice |         """ | ||||||
|  |         Test list URL for global settings | ||||||
|  |         """ | ||||||
|  |         url = reverse('api-global-setting-list') | ||||||
|  |  | ||||||
|  |         response = self.get(url, expected_code=200) | ||||||
|  |  | ||||||
|  |     def test_user_settings_api_list(self): | ||||||
|  |         """ | ||||||
|  |         Test list URL for user settings | ||||||
|  |         """ | ||||||
|         url = reverse('api-user-setting-list') |         url = reverse('api-user-setting-list') | ||||||
|         self.get(url, expected_code=200) |         # test setting with choice | ||||||
|  |         response = self.get(url, expected_code=200) | ||||||
|  |  | ||||||
|  |     def test_user_setting_boolean(self): | ||||||
|  |         """ | ||||||
|  |         Test a boolean user setting value | ||||||
|  |         """ | ||||||
|  |  | ||||||
|  |         # Ensure we have a boolean setting available | ||||||
|  |         setting = InvenTreeUserSetting.get_setting_object( | ||||||
|  |             'SEARCH_PREVIEW_SHOW_PARTS', | ||||||
|  |             user=self.user | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         # Check default values | ||||||
|  |         self.assertEqual(setting.native_value, True) | ||||||
|  |  | ||||||
|  |         # Fetch via API | ||||||
|  |         url = reverse('api-user-setting-detail', kwargs={'pk': setting.pk}) | ||||||
|  |  | ||||||
|  |         response = self.get(url, expected_code=200) | ||||||
|  |  | ||||||
|  |         self.assertEqual(response.data['pk'], setting.pk) | ||||||
|  |         self.assertEqual(response.data['key'], 'SEARCH_PREVIEW_SHOW_PARTS') | ||||||
|  |         self.assertEqual(response.data['description'], 'Display parts in search preview window') | ||||||
|  |         self.assertEqual(response.data['type'], 'boolean') | ||||||
|  |         self.assertEqual(len(response.data['choices']), 0) | ||||||
|  |         self.assertTrue(str2bool(response.data['value'])) | ||||||
|  |  | ||||||
|  |         # Assign some truthy values | ||||||
|  |         for v in ['true', True, 1, 'y', 'TRUE']: | ||||||
|  |             self.patch( | ||||||
|  |                 url, | ||||||
|  |                 { | ||||||
|  |                     'value': str(v), | ||||||
|  |                 }, | ||||||
|  |                 expected_code=200, | ||||||
|  |             ) | ||||||
|  |  | ||||||
|  |             response = self.get(url, expected_code=200) | ||||||
|  |  | ||||||
|  |             self.assertTrue(str2bool(response.data['value'])) | ||||||
|  |  | ||||||
|  |         # Assign some falsey values | ||||||
|  |         for v in ['false', False, '0', 'n', 'FalSe']: | ||||||
|  |             self.patch( | ||||||
|  |                 url, | ||||||
|  |                 { | ||||||
|  |                     'value': str(v), | ||||||
|  |                 }, | ||||||
|  |                 expected_code=200, | ||||||
|  |             ) | ||||||
|  |  | ||||||
|  |             response = self.get(url, expected_code=200) | ||||||
|  |  | ||||||
|  |             self.assertFalse(str2bool(response.data['value'])) | ||||||
|  |  | ||||||
|  |         # Assign some invalid values | ||||||
|  |         for v in ['x', '', 'invalid', None, '-1', 'abcde']: | ||||||
|  |             response = self.patch( | ||||||
|  |                 url, | ||||||
|  |                 { | ||||||
|  |                     'value': str(v), | ||||||
|  |                 }, | ||||||
|  |                 expected_code=200 | ||||||
|  |             ) | ||||||
|  |  | ||||||
|  |             # Invalid values evaluate to False | ||||||
|  |             self.assertFalse(str2bool(response.data['value'])) | ||||||
|  |  | ||||||
|  |     def test_user_setting_string(self): | ||||||
|  |         ... | ||||||
|  |  | ||||||
|  |     def test_user_setting_choice(self): | ||||||
|  |         ... | ||||||
|  |  | ||||||
|  |     def test_user_setting_integer(self): | ||||||
|  |         ... | ||||||
|  |  | ||||||
|  |  | ||||||
| class WebhookMessageTests(TestCase): | class WebhookMessageTests(TestCase): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user