From c3fc04e872a7c25dca69c543f9eb712eb77c0fb0 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Tue, 22 Jun 2021 09:33:20 +1000
Subject: [PATCH] Unit testing for part duplication

---
 InvenTree/part/test_part.py | 49 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/InvenTree/part/test_part.py b/InvenTree/part/test_part.py
index 2bc24c3a99..bcae74a0be 100644
--- a/InvenTree/part/test_part.py
+++ b/InvenTree/part/test_part.py
@@ -11,7 +11,7 @@ from django.core.exceptions import ValidationError
 
 import os
 
-from .models import Part, PartTestTemplate
+from .models import Part, PartCategory, PartTestTemplate
 from .models import rename_part_image, match_part_names
 from .templatetags import inventree_extras
 
@@ -78,6 +78,53 @@ class PartTest(TestCase):
         p = Part.objects.get(pk=100)
         self.assertEqual(str(p), "BOB | Bob | A2 - Can we build it?")
 
+    def test_duplicate(self):
+        """
+        Test that we cannot create a "duplicate" Part
+        """
+
+        n = Part.objects.count()
+
+        cat = PartCategory.objects.get(pk=1)
+
+        Part.objects.create(
+            category=cat,
+            name='part',
+            description='description',
+            IPN='IPN',
+            revision='A',
+        )
+
+        self.assertEqual(Part.objects.count(), n + 1)
+
+        part = Part(category=cat,
+                name='part',
+                description='description',
+                IPN='IPN',
+                revision='A',
+        )
+
+        with self.assertRaises(ValidationError):
+            part.validate_unique()
+
+        try:
+            part.save()
+            assert(False)
+        except:
+            pass
+
+        self.assertEqual(Part.objects.count(), n + 1)
+
+        Part.objects.create(
+            category=cat,
+            name='part',
+            description='description',
+            IPN='IPN',
+            revision='B',
+        )
+
+        self.assertEqual(Part.objects.count(), n + 2)
+
     def test_metadata(self):
         self.assertEqual(self.r1.name, 'R_2K2_0805')
         self.assertEqual(self.r1.get_absolute_url(), '/part/3/')