From 3961e48fe82d03f45995cd75fc5634d69039d7d0 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 29 Aug 2024 17:06:14 +1000 Subject: [PATCH] Test template fix (#8031) * Fix restriction for PartTestTemplate - limit_choices_to should be "testable", not "trackable" - ref: https://github.com/inventree/InvenTree/pull/7888 * Add migration file * Fix validation check * Fix API filter * Fix for test fixture * Fix another test * Fixture data * Fix comment * More fixes * More fixes * Moar fix plz thx --- src/backend/InvenTree/build/test_build.py | 3 +++ src/backend/InvenTree/part/api.py | 2 +- src/backend/InvenTree/part/fixtures/part.yaml | 15 +++++++++++---- .../0130_alter_parttesttemplate_part.py | 19 +++++++++++++++++++ src/backend/InvenTree/part/models.py | 6 +++--- src/backend/InvenTree/part/test_api.py | 2 +- src/backend/InvenTree/stock/test_api.py | 5 ++--- 7 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 src/backend/InvenTree/part/migrations/0130_alter_parttesttemplate_part.py diff --git a/src/backend/InvenTree/build/test_build.py b/src/backend/InvenTree/build/test_build.py index 7e190e3e21..2df213b370 100644 --- a/src/backend/InvenTree/build/test_build.py +++ b/src/backend/InvenTree/build/test_build.py @@ -55,6 +55,7 @@ class BuildTestBase(TestCase): description="Why does it matter what my description is?", assembly=True, trackable=True, + testable=True, ) # create one build with one required test template @@ -63,6 +64,7 @@ class BuildTestBase(TestCase): description="Why does it matter what my description is?", assembly=True, trackable=True, + testable=True, ) cls.test_template_required = PartTestTemplate.objects.create( @@ -98,6 +100,7 @@ class BuildTestBase(TestCase): description="Why does it matter what my description is?", assembly=True, trackable=True, + testable=True, ) cls.test_template_non_required = PartTestTemplate.objects.create( diff --git a/src/backend/InvenTree/part/api.py b/src/backend/InvenTree/part/api.py index 887f11312f..ac4a59fcad 100644 --- a/src/backend/InvenTree/part/api.py +++ b/src/backend/InvenTree/part/api.py @@ -415,7 +415,7 @@ class PartTestTemplateFilter(rest_filters.FilterSet): fields = ['enabled', 'key', 'required', 'requires_attachment', 'requires_value'] part = rest_filters.ModelChoiceFilter( - queryset=Part.objects.filter(trackable=True), + queryset=Part.objects.filter(testable=True), label='Part', field_name='part', method='filter_part', diff --git a/src/backend/InvenTree/part/fixtures/part.yaml b/src/backend/InvenTree/part/fixtures/part.yaml index 5acba3e4c5..232faeb015 100644 --- a/src/backend/InvenTree/part/fixtures/part.yaml +++ b/src/backend/InvenTree/part/fixtures/part.yaml @@ -10,6 +10,7 @@ creation_date: '2018-01-01' tree_id: 0 purchaseable: True + testable: False level: 0 lft: 0 rght: 0 @@ -78,6 +79,7 @@ salable: true assembly: true trackable: true + testable: true tree_id: 0 level: 0 lft: 0 @@ -136,7 +138,8 @@ name: 'Chair Template' description: 'A chair, which is actually just a template part' is_template: True - trackable: true + trackable: false + testable: true creation_date: '2027-10-10' salable: true category: 7 @@ -151,7 +154,8 @@ name: 'Blue Chair' description: 'A variant chair part which is blue' variant_of: 10000 - trackable: true + trackable: false + testable: true creation_date: '2028-11-11' category: 7 tree_id: 1 @@ -166,7 +170,8 @@ description: 'A variant chair part which is red' variant_of: 10000 IPN: "R.CH" - trackable: true + trackable: false + testable: true salable: true creation_date: '2029-12-12' category: 7 @@ -183,7 +188,8 @@ variant_of: 10000 is_template: true category: 7 - trackable: true + trackable: false + testable: true creation_date: '2030-01-01' tree_id: 1 level: 0 @@ -200,6 +206,7 @@ category: 7 creation_date: '2031-02-02' trackable: true + testable: true tree_id: 1 level: 0 lft: 0 diff --git a/src/backend/InvenTree/part/migrations/0130_alter_parttesttemplate_part.py b/src/backend/InvenTree/part/migrations/0130_alter_parttesttemplate_part.py new file mode 100644 index 0000000000..449baff6fe --- /dev/null +++ b/src/backend/InvenTree/part/migrations/0130_alter_parttesttemplate_part.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.15 on 2024-08-29 03:24 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('part', '0129_auto_20240815_0214'), + ] + + operations = [ + migrations.AlterField( + model_name='parttesttemplate', + name='part', + field=models.ForeignKey(limit_choices_to={'testable': True}, on_delete=django.db.models.deletion.CASCADE, related_name='test_templates', to='part.part', verbose_name='Part'), + ), + ] diff --git a/src/backend/InvenTree/part/models.py b/src/backend/InvenTree/part/models.py index 866f00a7e8..246d71080c 100644 --- a/src/backend/InvenTree/part/models.py +++ b/src/backend/InvenTree/part/models.py @@ -3582,9 +3582,9 @@ class PartTestTemplate(InvenTree.models.InvenTreeMetadataModel): def validate_unique(self, exclude=None): """Test that this test template is 'unique' within this part tree.""" - if not self.part.trackable: + if not self.part.testable: raise ValidationError({ - 'part': _('Test templates can only be created for trackable parts') + 'part': _('Test templates can only be created for testable parts') }) # Check that this test is unique within the part tree @@ -3605,7 +3605,7 @@ class PartTestTemplate(InvenTree.models.InvenTreeMetadataModel): Part, on_delete=models.CASCADE, related_name='test_templates', - limit_choices_to={'trackable': True}, + limit_choices_to={'testable': True}, verbose_name=_('Part'), ) diff --git a/src/backend/InvenTree/part/test_api.py b/src/backend/InvenTree/part/test_api.py index 3c7ef8a247..5c74ba7674 100644 --- a/src/backend/InvenTree/part/test_api.py +++ b/src/backend/InvenTree/part/test_api.py @@ -2996,7 +2996,7 @@ class PartTestTemplateTest(PartAPITestBase): expected_code=400, ) - # Try to post a new test against a non-trackable part (should fail) + # Try to post a new test against a non-testable part (should fail) response = self.post( url, data={'part': 1, 'test_name': 'A simple test'}, expected_code=400 ) diff --git a/src/backend/InvenTree/stock/test_api.py b/src/backend/InvenTree/stock/test_api.py index 1b9407c159..4e06d0ac42 100644 --- a/src/backend/InvenTree/stock/test_api.py +++ b/src/backend/InvenTree/stock/test_api.py @@ -1784,9 +1784,9 @@ class StockTestResultTest(StockAPITestCase): stock_item = StockItem.objects.get(pk=1) - # Ensure the part is marked as "trackable" + # Ensure the part is marked as "testable" p = stock_item.part - p.trackable = True + p.testable = True p.save() # Create some objects (via the API) @@ -1799,7 +1799,6 @@ class StockTestResultTest(StockAPITestCase): 'result': True, 'value': 'Test result value', }, - # expected_code=201, ) tests.append(response.data['pk'])