2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-13 08:21:26 +00:00

[Feature] Company Addresses (#4732)

* Add initial model structure

* Initial Address model defined

* Add migration and unit tests

* Initial migration for Address model generated

* Unit tests for Address model added

* Move address field to new model

* Added migration to move address field to Address model

* Implement address feature to backend

* API endpoints for list and detail implemented

* Serializer class for Address implemented

* Final migration to delete old address field from company added

* Tests for API and migrations added

* Amend migration file names

* Fix migration names in test

* Add address property to company model

* Iinital view and JS code

* Fix indents

* Fix different things

* Pre-emptive change before merge

* Post-merge fixes

* dotdotdot...

* ...

* iDots

* .

* .

* .

* Add form functionality and model checks

* Forms require a confirmation slider to be checked to submit
  if address is selected as primary

* Backend resets primary address before saving if new address
  is designated as primary

* Fix pre-save logic to enforce primary uniqueness

* Fix typos

* Sort out migrations

* Forgot one

* Add admin entry and small fixes

* Fix migration file name and dependency

* Update InvenTree/company/models.py

Co-authored-by: Matthias Mair <code@mjmair.com>

* Update InvenTree/company/models.py

Co-authored-by: Matthias Mair <code@mjmair.com>

* Correct final issues

* .

---------

Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
Lavissa
2023-06-17 13:55:25 +02:00
committed by GitHub
parent 61d2f452b2
commit bf707766b6
28 changed files with 1185 additions and 28 deletions

View File

@ -6,7 +6,7 @@ from rest_framework import status
from InvenTree.unit_test import InvenTreeAPITestCase
from .models import Company, Contact, ManufacturerPart, SupplierPart
from .models import Address, Company, Contact, ManufacturerPart, SupplierPart
class CompanyTest(InvenTreeAPITestCase):
@ -284,6 +284,138 @@ class ContactTest(InvenTreeAPITestCase):
self.get(url, expected_code=404)
class AddressTest(InvenTreeAPITestCase):
"""Test cases for Address API endpoints"""
roles = []
@classmethod
def setUpTestData(cls):
"""Perform initialization for this test class"""
super().setUpTestData()
cls.num_companies = 3
cls.num_addr = 3
# Create some companies
companies = [
Company(
name=f"Company {idx}",
description="Some company"
) for idx in range(cls.num_companies)
]
Company.objects.bulk_create(companies)
addresses = []
# Create some contacts
for cmp in Company.objects.all():
addresses += [
Address(
company=cmp,
title=f"Address no. {idx}",
) for idx in range(cls.num_addr)
]
cls.url = reverse('api-address-list')
Address.objects.bulk_create(addresses)
def test_list(self):
"""Test listing all addresses without filtering"""
response = self.get(self.url, expected_code=200)
self.assertEqual(len(response.data), self.num_companies * self.num_addr)
def test_filter_list(self):
"""Test listing addresses filtered on company"""
company = Company.objects.first()
response = self.get(self.url, {'company': company.pk}, expected_code=200)
self.assertEqual(len(response.data), self.num_addr)
def test_create(self):
"""Test creating a new address"""
company = Company.objects.first()
self.post(self.url,
{
'company': company.pk,
'title': 'HQ'
},
expected_code=403)
self.assignRole('purchase_order.add')
self.post(self.url,
{
'company': company.pk,
'title': 'HQ'
},
expected_code=201)
def test_get(self):
"""Test that objects are properly returned from a get"""
addr = Address.objects.first()
url = reverse('api-address-detail', kwargs={'pk': addr.pk})
response = self.get(url, expected_code=200)
self.assertEqual(response.data['pk'], addr.pk)
for key in ['title', 'line1', 'line2', 'postal_code', 'postal_city', 'province', 'country']:
self.assertIn(key, response.data)
def test_edit(self):
"""Test editing an object"""
addr = Address.objects.first()
url = reverse('api-address-detail', kwargs={'pk': addr.pk})
self.patch(
url,
{
'title': 'Hello'
},
expected_code=403
)
self.assignRole('purchase_order.change')
self.patch(
url,
{
'title': 'World'
},
expected_code=200
)
data = self.get(url, expected_code=200).data
self.assertEqual(data['title'], 'World')
def test_delete(self):
"""Test deleting an object"""
addr = Address.objects.first()
url = reverse('api-address-detail', kwargs={'pk': addr.pk})
self.delete(url, expected_code=403)
self.assignRole('purchase_order.delete')
self.delete(url, expected_code=204)
self.get(url, expected_code=404)
class ManufacturerTest(InvenTreeAPITestCase):
"""Series of tests for the Manufacturer DRF API."""