diff --git a/InvenTree/company/fixtures/company.yaml b/InvenTree/company/fixtures/company.yaml index 8301eb0f5e..c302b6efad 100644 --- a/InvenTree/company/fixtures/company.yaml +++ b/InvenTree/company/fixtures/company.yaml @@ -31,3 +31,17 @@ name: Another customer! description: Yet another company is_customer: True + +- model: company.company + pk: 6 + fields: + name: A manufacturer + description: A company that makes parts! + is_manufacturer: True + +- model: company.company + pk: 7 + fields: + name: Another manufacturer + description: They build things and sell it to us + is_manufacturer: True diff --git a/InvenTree/company/fixtures/manufacturer_part.yaml b/InvenTree/company/fixtures/manufacturer_part.yaml new file mode 100644 index 0000000000..7532955eac --- /dev/null +++ b/InvenTree/company/fixtures/manufacturer_part.yaml @@ -0,0 +1,22 @@ +# Manufacturer Parts + +- model: company.manufacturerpart + pk: 1 + fields: + part: 5 + manufacturer: 6 + MPN: 'MPN123' + +- model: company.manufacturerpart + pk: 2 + fields: + part: 3 + manufacturer: 7 + MPN: 'MPN456' + +- model: company.manufacturerpart + pk: 3 + fields: + part: 5 + manufacturer: 7 + MPN: 'MPN789' diff --git a/InvenTree/company/fixtures/supplier_part.yaml b/InvenTree/company/fixtures/supplier_part.yaml index 446339d58b..b4c4d7e58a 100644 --- a/InvenTree/company/fixtures/supplier_part.yaml +++ b/InvenTree/company/fixtures/supplier_part.yaml @@ -52,3 +52,20 @@ part: 2 supplier: 2 SKU: 'ZERGM312' + +# Supplier parts linked to Manufacturer parts +- model: company.supplierpart + pk: 10 + fields: + part: 3 + manufacturer_part: 2 + supplier: 2 + SKU: 'MPN456-APPEL' + +- model: company.supplierpart + pk: 11 + fields: + part: 3 + manufacturer_part: 2 + supplier: 3 + SKU: 'MPN456-ZERG' diff --git a/InvenTree/company/test_views.py b/InvenTree/company/test_views.py index 0163e65c29..1868175648 100644 --- a/InvenTree/company/test_views.py +++ b/InvenTree/company/test_views.py @@ -10,6 +10,7 @@ from django.urls import reverse from django.contrib.auth import get_user_model from django.contrib.auth.models import Group +from .models import ManufacturerPart from .models import SupplierPart @@ -20,6 +21,7 @@ class CompanyViewTestBase(TestCase): 'part', 'location', 'company', + 'manufacturer_part', 'supplier_part', ] @@ -200,3 +202,65 @@ class CompanyViewTest(CompanyViewTestBase): response = self.client.get(reverse('customer-create'), HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertContains(response, 'Create new Customer') + + +class ManufacturerPartViewTests(CompanyViewTestBase): + """ + Tests for the ManufacturerPart views. + """ + + def test_manufacturer_part_create(self): + """ + Test the ManufacturerPartCreate view. + """ + + url = reverse('manufacturer-part-create') + + # First check that we can GET the form + response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + self.assertEqual(response.status_code, 200) + + # How many supplier parts are already in the database? + n = ManufacturerPart.objects.all().count() + + data = { + 'part': 1, + 'manufacturer': 6, + } + + # MPN is required! (form should fail) + (response, errors) = self.post(url, data, valid=False) + + self.assertIsNotNone(errors.get('MPN', None)) + + data['MPN'] = 'TEST-ME-123' + + (response, errors) = self.post(url, data, valid=True) + + # Check that the ManufacturerPart was created! + self.assertEqual(n + 1, ManufacturerPart.objects.all().count()) + + def test_manufacturer_part_delete(self): + """ + Test the ManufacturerPartDelete view + """ + + url = reverse('manufacturer-part-delete') + + # Get form using 'part' argument + response = self.client.get(url, {'part': '5'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + self.assertEqual(response.status_code, 200) + + # POST to delete manufacturer part + n = ManufacturerPart.objects.count() + response = self.client.post( + url, + { + 'manufacturer-part-2': 'manufacturer-part-2', + 'confirm_delete': True + }, + HTTP_X_REQUESTED_WITH='XMLHttpRequest') + + self.assertEqual(response.status_code, 200) + + self.assertEqual(n - 1, ManufacturerPart.objects.count()) diff --git a/InvenTree/company/tests.py b/InvenTree/company/tests.py index f7bcd4e0b6..a92e4300a8 100644 --- a/InvenTree/company/tests.py +++ b/InvenTree/company/tests.py @@ -6,7 +6,7 @@ from django.core.exceptions import ValidationError import os -from .models import Company, Contact, SupplierPart +from .models import Company, Contact, ManufacturerPart, SupplierPart from .models import rename_company_image from part.models import Part @@ -22,6 +22,7 @@ class CompanySimpleTest(TestCase): 'part', 'location', 'bom', + 'manufacturer_part', 'supplier_part', 'price_breaks', ] @@ -74,10 +75,10 @@ class CompanySimpleTest(TestCase): self.assertEqual(acme.supplied_part_count, 4) self.assertTrue(appel.has_parts) - self.assertEqual(appel.supplied_part_count, 2) + self.assertEqual(appel.supplied_part_count, 3) self.assertTrue(zerg.has_parts) - self.assertEqual(zerg.supplied_part_count, 1) + self.assertEqual(zerg.supplied_part_count, 2) def test_price_breaks(self): @@ -166,3 +167,31 @@ class ContactSimpleTest(TestCase): # Remove the parent company Company.objects.get(pk=self.c.pk).delete() self.assertEqual(Contact.objects.count(), 0) + + +class ManufacturerPartSimpleTest(TestCase): + + fixtures = [ + 'category', + 'company', + 'location', + 'part', + 'manufacturer_part', + ] + + def setUp(self): + # Create a manufacturer part + self.part = Part.objects.get(pk=1) + manufacturer = Company.objects.get(pk=1) + MPN = 'MPN_TEST' + + self.mp = ManufacturerPart.objects.create(part=self.part, manufacturer=manufacturer, MPN=MPN) + + def test_exists(self): + self.assertEqual(ManufacturerPart.objects.count(), 4) + + def test_delete(self): + # Remove a part + Part.objects.get(pk=self.part.id).delete() + # Check that ManufacturerPart was deleted + self.assertEqual(ManufacturerPart.objects.count(), 3)