From 3e2e9aaf9ec49cdd6b28faf626767ded42669aeb Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 9 Jan 2022 20:10:00 +1100 Subject: [PATCH 1/3] Mark serializer fields as not required --- InvenTree/build/serializers.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index 55f89c1844..01ea9fd924 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -248,6 +248,8 @@ class BuildCompleteSerializer(serializers.Serializer): accept_unallocated = serializers.BooleanField( label=_('Accept Unallocated'), help_text=_('Accept that stock items have not been fully allocated to this build order'), + required=False, + default=False, ) def validate_accept_unallocated(self, value): @@ -262,6 +264,8 @@ class BuildCompleteSerializer(serializers.Serializer): accept_incomplete = serializers.BooleanField( label=_('Accept Incomplete'), help_text=_('Accept that the required number of build outputs have not been completed'), + required=False, + default=False, ) def validate_accept_incomplete(self, value): From da9fa1313c8fb91b17f3e75f87f8f0e765a697f2 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 9 Jan 2022 22:14:48 +1100 Subject: [PATCH 2/3] Increased unit testing --- InvenTree/build/models.py | 2 +- InvenTree/build/serializers.py | 9 ++++++++ InvenTree/build/test_api.py | 42 ++++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index f03cb30c74..cd8f4df16f 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -582,7 +582,7 @@ class Build(MPTTModel, ReferenceIndexingMixin): self.subtractUntrackedStock(user) # Ensure that there are no longer any BuildItem objects - # which point to thie Build Order + # which point to thisFcan Build Order self.allocated_stock.all().delete() @transaction.atomic diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index 01ea9fd924..04cbc162c9 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -277,6 +277,15 @@ class BuildCompleteSerializer(serializers.Serializer): return value + def validate(self, data): + + build = self.context['build'] + + if build.incomplete_count > 0: + raise ValidationError(_("Build order has incomplete outputs")) + + return data + def save(self): request = self.context['request'] diff --git a/InvenTree/build/test_api.py b/InvenTree/build/test_api.py index 45662a58d6..64d17430a0 100644 --- a/InvenTree/build/test_api.py +++ b/InvenTree/build/test_api.py @@ -38,7 +38,7 @@ class BuildAPITest(InvenTreeAPITestCase): super().setUp() -class BuildCompleteTest(BuildAPITest): +class BuildOutputCompleteTest(BuildAPITest): """ Unit testing for the build complete API endpoint """ @@ -140,6 +140,9 @@ class BuildCompleteTest(BuildAPITest): Test build order completion """ + # Initially, build should not be able to be completed + self.assertFalse(self.build.can_complete) + # We start without any outputs assigned against the build self.assertEqual(self.build.incomplete_outputs.count(), 0) @@ -153,7 +156,7 @@ class BuildCompleteTest(BuildAPITest): self.assertEqual(self.build.completed, 0) # We shall complete 4 of these outputs - outputs = self.build.incomplete_outputs[0:4] + outputs = self.build.incomplete_outputs.all() self.post( self.url, @@ -165,19 +168,44 @@ class BuildCompleteTest(BuildAPITest): expected_code=201 ) - # There now should be 6 incomplete build outputs remaining - self.assertEqual(self.build.incomplete_outputs.count(), 6) + self.assertEqual(self.build.incomplete_outputs.count(), 0) - # And there should be 4 completed outputs + # And there should be 10 completed outputs outputs = self.build.complete_outputs - self.assertEqual(outputs.count(), 4) + self.assertEqual(outputs.count(), 10) for output in outputs: self.assertFalse(output.is_building) self.assertEqual(output.build, self.build) self.build.refresh_from_db() - self.assertEqual(self.build.completed, 40) + self.assertEqual(self.build.completed, 100) + + # Try to complete the build (it should fail) + finish_url = reverse('api-build-finish', kwargs={'pk': self.build.pk}) + + response = self.post( + finish_url, + {}, + expected_code=400 + ) + + self.assertTrue('accept_unallocated' in response.data) + + # Accept unallocated stock + response = self.post( + finish_url, + { + 'accept_unallocated': True, + }, + expected_code=201, + ) + + # Accept unfinished + self.build.refresh_from_db() + + # Build should have been marked as complete + self.assertTrue(self.build.is_complete) class BuildAllocationTest(BuildAPITest): From 6aa83796eadb029e8635f6b18df0d17bda3cf24e Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 9 Jan 2022 22:15:59 +1100 Subject: [PATCH 3/3] PEP fixes --- InvenTree/build/serializers.py | 2 +- InvenTree/build/test_api.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index 04cbc162c9..fb34a40a16 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -282,7 +282,7 @@ class BuildCompleteSerializer(serializers.Serializer): build = self.context['build'] if build.incomplete_count > 0: - raise ValidationError(_("Build order has incomplete outputs")) + raise ValidationError(_("Build order has incomplete outputs")) return data diff --git a/InvenTree/build/test_api.py b/InvenTree/build/test_api.py index 64d17430a0..1e0c2b4792 100644 --- a/InvenTree/build/test_api.py +++ b/InvenTree/build/test_api.py @@ -201,7 +201,6 @@ class BuildOutputCompleteTest(BuildAPITest): expected_code=201, ) - # Accept unfinished self.build.refresh_from_db() # Build should have been marked as complete