mirror of
https://github.com/inventree/InvenTree.git
synced 2025-10-23 17:37:38 +00:00
Order tests (#10649)
- Additional test coverage for order models - Validate address - Validate contact - Validate date ranges
This commit is contained in:
@@ -383,6 +383,13 @@ class Order(
|
|||||||
'start_date': _('Start date must be before target date'),
|
'start_date': _('Start date must be before target date'),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# Check that the referenced 'address' matches the correct 'company'
|
||||||
|
if self.company and self.address:
|
||||||
|
if self.address.company != self.company:
|
||||||
|
raise ValidationError({
|
||||||
|
'address': _('Address does not match selected company')
|
||||||
|
})
|
||||||
|
|
||||||
def clean_line_item(self, line):
|
def clean_line_item(self, line):
|
||||||
"""Clean a line item for this order.
|
"""Clean a line item for this order.
|
||||||
|
|
||||||
|
@@ -8,7 +8,7 @@ from django.core.exceptions import ValidationError
|
|||||||
|
|
||||||
import order.tasks
|
import order.tasks
|
||||||
from common.models import InvenTreeSetting, NotificationMessage
|
from common.models import InvenTreeSetting, NotificationMessage
|
||||||
from company.models import Company
|
from company.models import Address, Company
|
||||||
from InvenTree import status_codes as status
|
from InvenTree import status_codes as status
|
||||||
from InvenTree.unit_test import InvenTreeTestCase, addUserPermission
|
from InvenTree.unit_test import InvenTreeTestCase, addUserPermission
|
||||||
from order.models import (
|
from order.models import (
|
||||||
@@ -26,7 +26,7 @@ from users.models import Owner
|
|||||||
class SalesOrderTest(InvenTreeTestCase):
|
class SalesOrderTest(InvenTreeTestCase):
|
||||||
"""Run tests to ensure that the SalesOrder model is working correctly."""
|
"""Run tests to ensure that the SalesOrder model is working correctly."""
|
||||||
|
|
||||||
fixtures = ['users']
|
fixtures = ['company', 'users']
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
@@ -75,6 +75,38 @@ class SalesOrderTest(InvenTreeTestCase):
|
|||||||
quantity=1, order=cls.order, reference='Extra line'
|
quantity=1, order=cls.order, reference='Extra line'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_validate_address(self):
|
||||||
|
"""Test validation of the linked Address."""
|
||||||
|
order = SalesOrder.objects.first()
|
||||||
|
|
||||||
|
# Create an address for a different company
|
||||||
|
company = Company.objects.exclude(pk=order.customer.pk).first()
|
||||||
|
self.assertIsNotNone(company)
|
||||||
|
address = Address.objects.create(
|
||||||
|
company=company,
|
||||||
|
primary=False,
|
||||||
|
line1='123 Different St',
|
||||||
|
line2='Elsewhere',
|
||||||
|
postal_code='99999',
|
||||||
|
country='US',
|
||||||
|
)
|
||||||
|
|
||||||
|
order.address = address
|
||||||
|
|
||||||
|
with self.assertRaises(ValidationError) as err:
|
||||||
|
order.clean()
|
||||||
|
|
||||||
|
self.assertIn('Address does not match selected company', str(err.exception))
|
||||||
|
|
||||||
|
# Update the address to match the correct company
|
||||||
|
address.company = order.customer
|
||||||
|
address.save()
|
||||||
|
|
||||||
|
# Now validation should pass
|
||||||
|
order.address = address
|
||||||
|
order.clean()
|
||||||
|
order.save()
|
||||||
|
|
||||||
def test_so_reference(self):
|
def test_so_reference(self):
|
||||||
"""Unit tests for sales order generation."""
|
"""Unit tests for sales order generation."""
|
||||||
# Test that a good reference is created when we have no existing orders
|
# Test that a good reference is created when we have no existing orders
|
||||||
|
@@ -13,7 +13,8 @@ from djmoney.money import Money
|
|||||||
import common.models
|
import common.models
|
||||||
import order.tasks
|
import order.tasks
|
||||||
from common.settings import get_global_setting, set_global_setting
|
from common.settings import get_global_setting, set_global_setting
|
||||||
from company.models import Company, SupplierPart
|
from company.models import Company, Contact, SupplierPart
|
||||||
|
from InvenTree.helpers import current_date
|
||||||
from InvenTree.unit_test import (
|
from InvenTree.unit_test import (
|
||||||
ExchangeRateMixin,
|
ExchangeRateMixin,
|
||||||
PluginRegistryMixin,
|
PluginRegistryMixin,
|
||||||
@@ -55,6 +56,49 @@ class OrderTest(ExchangeRateMixin, PluginRegistryMixin, TestCase):
|
|||||||
line = PurchaseOrderLineItem.objects.get(pk=1)
|
line = PurchaseOrderLineItem.objects.get(pk=1)
|
||||||
self.assertEqual(str(line), '100 x ACME0001 - PO-0001 - ACME')
|
self.assertEqual(str(line), '100 x ACME0001 - PO-0001 - ACME')
|
||||||
|
|
||||||
|
def test_validate_dates(self):
|
||||||
|
"""Test for validation of date fields."""
|
||||||
|
order = PurchaseOrder.objects.first()
|
||||||
|
|
||||||
|
order.start_date = current_date()
|
||||||
|
order.target_date = current_date() - timedelta(days=5)
|
||||||
|
|
||||||
|
with self.assertRaises(django_exceptions.ValidationError) as err:
|
||||||
|
order.clean()
|
||||||
|
|
||||||
|
self.assertIn('Target date must be after start date', err.exception.messages)
|
||||||
|
self.assertIn('Start date must be before target date', err.exception.messages)
|
||||||
|
|
||||||
|
order.target_date = current_date() + timedelta(days=5)
|
||||||
|
|
||||||
|
# This should now pass
|
||||||
|
order.clean()
|
||||||
|
order.save()
|
||||||
|
|
||||||
|
def test_validate_contact(self):
|
||||||
|
"""Test for validation of linked Contact."""
|
||||||
|
order = PurchaseOrder.objects.first()
|
||||||
|
|
||||||
|
# Create a contact which does does not match the company
|
||||||
|
company = Company.objects.exclude(pk=order.supplier.pk).first()
|
||||||
|
self.assertIsNotNone(company)
|
||||||
|
contact = Contact.objects.create(company=company, name='Harold Henderson')
|
||||||
|
|
||||||
|
order.contact = contact
|
||||||
|
|
||||||
|
with self.assertRaises(django_exceptions.ValidationError) as err:
|
||||||
|
order.clean()
|
||||||
|
|
||||||
|
self.assertIn('Contact does not match selected company', err.exception.messages)
|
||||||
|
|
||||||
|
# Update the contact, point to the right company
|
||||||
|
contact.company = order.supplier
|
||||||
|
contact.save()
|
||||||
|
|
||||||
|
order.contact = contact
|
||||||
|
order.clean() # Should not raise
|
||||||
|
order.save()
|
||||||
|
|
||||||
def test_rebuild_reference(self):
|
def test_rebuild_reference(self):
|
||||||
"""Test that the reference_int field is correctly updated when the model is saved."""
|
"""Test that the reference_int field is correctly updated when the model is saved."""
|
||||||
order = PurchaseOrder.objects.get(pk=1)
|
order = PurchaseOrder.objects.get(pk=1)
|
||||||
|
Reference in New Issue
Block a user