From e401bb8e3c5e27de81a33d61de0e95e2d9bc9971 Mon Sep 17 00:00:00 2001 From: eeintech Date: Wed, 4 Nov 2020 12:06:07 -0500 Subject: [PATCH] Improved tests, fixed admin, improved naming --- InvenTree/part/admin.py | 9 +------ InvenTree/part/fixtures/params.yaml | 2 +- InvenTree/part/forms.py | 2 +- InvenTree/part/models.py | 5 +++- InvenTree/part/test_param.py | 39 ++++++++++++++++++++++++++--- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/InvenTree/part/admin.py b/InvenTree/part/admin.py index 02e6974aef..1d1c981b74 100644 --- a/InvenTree/part/admin.py +++ b/InvenTree/part/admin.py @@ -277,14 +277,7 @@ class ParameterAdmin(ImportExportModelAdmin): class PartCategoryParameterAdmin(admin.ModelAdmin): - def get_form(self, request, obj=None, **kwargs): - """ Display only parent categories as choices for category field """ - - form = super().get_form(request, obj, **kwargs) - - form.base_fields['category'].choices = PartCategory.get_parent_categories() - - return form + pass class PartSellPriceBreakAdmin(admin.ModelAdmin): diff --git a/InvenTree/part/fixtures/params.yaml b/InvenTree/part/fixtures/params.yaml index 2dfcada698..e65c7335cc 100644 --- a/InvenTree/part/fixtures/params.yaml +++ b/InvenTree/part/fixtures/params.yaml @@ -42,7 +42,7 @@ default_value: '2.8' - model: part.PartCategoryParameterTemplate - pk: 3 + pk: 2 fields: category: 7 parameter_template: 3 diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index cb4691e05b..20507bd392 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -207,7 +207,7 @@ class EditPartForm(HelperForm): selected_category_templates = forms.BooleanField(required=False, initial=False, - label=_('Include selected category parameter templates'), + label=_('Include category parameter templates'), widget=forms.HiddenInput()) parent_category_templates = forms.BooleanField(required=False, diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 633915466f..6ebc6fe804 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -180,7 +180,9 @@ class PartCategory(InvenTreeTree): def get_parameter_templates(self): """ Return parameter templates associated to category """ - return PartCategoryParameterTemplate.objects.filter(category=self.id) + prefetch = PartCategoryParameterTemplate.objects.prefetch_related('category', 'parameter_template') + + return prefetch.filter(category=self.id) @receiver(pre_delete, sender=PartCategory, dispatch_uid='partcategory_delete_log') @@ -1701,6 +1703,7 @@ class PartCategoryParameterTemplate(models.Model): def __str__(self): """ String representation of a PartCategoryParameterTemplate (admin interface) """ + if self.default_value: return f'{self.category.name} | {self.parameter_template.name} | {self.default_value}' else: diff --git a/InvenTree/part/test_param.py b/InvenTree/part/test_param.py index 66b2bf303c..24eee44d89 100644 --- a/InvenTree/part/test_param.py +++ b/InvenTree/part/test_param.py @@ -3,10 +3,10 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from django.test import TestCase +from django.test import TestCase, TransactionTestCase import django.core.exceptions as django_exceptions -from .models import PartCategory +from .models import Part, PartCategory from .models import PartParameter, PartParameterTemplate from .models import PartCategoryParameterTemplate @@ -46,15 +46,46 @@ class TestParams(TestCase): t3.full_clean() t3.save() + +class TestCategoryTemplates(TransactionTestCase): + + fixtures = [ + 'location', + 'category', + 'part', + 'params' + ] + + def test_validate(self): + # Category templates n = PartCategoryParameterTemplate.objects.all().count() self.assertEqual(n, 2) - category = PartCategory.objects.get(pk=7) + category = PartCategory.objects.get(pk=8) + t1 = PartParameterTemplate.objects.get(pk=2) c1 = PartCategoryParameterTemplate(category=category, parameter_template=t1, default_value='xyz') c1.save() - self.assertEqual(n + 1, PartCategoryParameterTemplate.objects.filter(category=7).count()) + n = PartCategoryParameterTemplate.objects.all().count() + self.assertEqual(n, 3) + + # Get test part + part = Part.objects.get(pk=1) + + # Get part parameters count + n_param = part.get_parameters().count() + + add_category_templates = { + 'main': True, + 'parent': True, + } + # Save it with category parameters + part.save(**{'add_category_templates': add_category_templates}) + + # Check new part parameters count + # Only 2 parameters should be added as one already existed with same template + self.assertEqual(n_param + 2, part.get_parameters().count())