From 47f6b709c91b5202c603fbeae174aa9a4b8ba8c2 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 17 Feb 2022 12:10:48 +1100 Subject: [PATCH] Improve unit testing --- InvenTree/part/serializers.py | 2 +- InvenTree/part/test_bom_import.py | 34 ++++++++++++++++++------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 6b0c89ad88..cc489cf5c9 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -780,7 +780,7 @@ class BomImportExtractSerializer(DataFileExtractSerializer): row['errors']['part'] = _('No matching part found') else: if not part.component: - row['errors']['part'] = _('Part is not designed as a component') + row['errors']['part'] = _('Part is not designated as a component') # Update the 'part' value in the row row['part'] = part.pk if part is not None else None diff --git a/InvenTree/part/test_bom_import.py b/InvenTree/part/test_bom_import.py index 06ff2a79d0..8cf66a183b 100644 --- a/InvenTree/part/test_bom_import.py +++ b/InvenTree/part/test_bom_import.py @@ -218,25 +218,31 @@ class BomUploadTest(InvenTreeAPITestCase): dataset.append([cmp.pk, idx]) - # Add a duplicate part too - dataset.append([components.first().pk, 'invalid']) + url = reverse('api-bom-import-extract') - response = self.post_bom( - 'test.csv', - bytes(dataset.csv, 'utf8'), - content_type='text/csv', - expected_code=201 + response = self.post( + url, + { + 'columns': dataset.headers, + 'rows': [row for row in dataset], + }, ) - errors = response.data['errors'] + rows = response.data['rows'] - self.assertIn('Quantity must be greater than zero', str(errors[0])) - self.assertIn('Part is not designated as a component', str(errors[5])) - self.assertIn('Duplicate part selected', str(errors[-1])) - self.assertIn('Invalid quantity', str(errors[-1])) + # Returned data must be the same as the original dataset + self.assertEqual(len(rows), len(dataset)) - for idx, row in enumerate(response.data['rows'][:-1]): - self.assertEqual(str(row['part']), str(components[idx].pk)) + for idx, row in enumerate(rows): + data = row['data'] + cmp = components[idx] + + # Should have guessed the correct part + data['part'] = cmp.pk + + # Check some specific error messages + self.assertEqual(rows[0]['data']['errors']['quantity'], 'Quantity must be greater than zero') + self.assertEqual(rows[5]['data']['errors']['part'], 'Part is not designated as a component') def test_part_guess(self): """