mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Tests for POLineItem creation form
This commit is contained in:
parent
7c6901f445
commit
d515e2d968
@ -6,7 +6,7 @@ from django.contrib.auth import get_user_model
|
||||
|
||||
from InvenTree.status_codes import OrderStatus
|
||||
|
||||
from .models import PurchaseOrder
|
||||
from .models import PurchaseOrder, PurchaseOrderLineItem
|
||||
|
||||
import json
|
||||
|
||||
@ -107,3 +107,57 @@ class POTests(OrderViewTestCase):
|
||||
# Test that the order was actually placed
|
||||
order = PurchaseOrder.objects.get(pk=1)
|
||||
self.assertEqual(order.status, OrderStatus.PLACED)
|
||||
|
||||
def test_line_item_create(self):
|
||||
""" Test the form for adding a new LineItem to a PurchaseOrder """
|
||||
|
||||
# Record the number of line items in the PurchaseOrder
|
||||
po = PurchaseOrder.objects.get(pk=1)
|
||||
n = po.lines.count()
|
||||
self.assertEqual(po.status, OrderStatus.PENDING)
|
||||
|
||||
url = reverse('po-line-item-create')
|
||||
|
||||
# GET the form (pass the correct info)
|
||||
response = self.client.get(url, {'order': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
||||
|
||||
post_data = {
|
||||
'part': 100,
|
||||
'quantity': 45,
|
||||
'reference': 'Test reference field',
|
||||
'notes': 'Test notes field'
|
||||
}
|
||||
|
||||
# POST with an invalid purchase order
|
||||
post_data['order'] = 99
|
||||
response = self.client.post(url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
||||
data = json.loads(response.content)
|
||||
self.assertFalse(data['form_valid'])
|
||||
self.assertIn('Invalid Purchase Order', str(data['html_form']))
|
||||
|
||||
# POST with a part that does not match the purchase order
|
||||
post_data['order'] = 1
|
||||
post_data['part'] = 3
|
||||
response = self.client.post(url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
||||
data = json.loads(response.content)
|
||||
self.assertFalse(data['form_valid'])
|
||||
self.assertIn('must match for Part and Order', str(data['html_form']))
|
||||
|
||||
# POST with an invalid part
|
||||
post_data['part'] = 12345
|
||||
response = self.client.post(url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
||||
data = json.loads(response.content)
|
||||
self.assertFalse(data['form_valid'])
|
||||
self.assertIn('Invalid SupplierPart selection', str(data['html_form']))
|
||||
|
||||
# POST the form with valid data
|
||||
post_data['part'] = 100
|
||||
response = self.client.post(url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
data = json.loads(response.content)
|
||||
self.assertTrue(data['form_valid'])
|
||||
|
||||
self.assertEqual(n+1, PurchaseOrder.objects.get(pk=1).lines.count())
|
||||
|
||||
line = PurchaseOrderLineItem.objects.get(order=1, part=100)
|
||||
self.assertEqual(line.quantity, 45)
|
@ -611,13 +611,30 @@ class POLineItemCreate(AjaxCreateView):
|
||||
|
||||
valid = form.is_valid()
|
||||
|
||||
# Extract the SupplierPart ID from the form
|
||||
part_id = form['part'].value()
|
||||
|
||||
# Extract the Order ID from the form
|
||||
order_id = form['order'].value()
|
||||
|
||||
try:
|
||||
SupplierPart.objects.get(id=part_id)
|
||||
order = PurchaseOrder.objects.get(id=order_id)
|
||||
except (ValueError, PurchaseOrder.DoesNotExist):
|
||||
order = None
|
||||
form.errors['order'] = [_('Invalid Purchase Order')]
|
||||
valid = False
|
||||
|
||||
try:
|
||||
sp = SupplierPart.objects.get(id=part_id)
|
||||
|
||||
if order is not None:
|
||||
if not sp.supplier == order.supplier:
|
||||
form.errors['part'] = [_('Supplier must match for Part and Order')]
|
||||
valid = False
|
||||
|
||||
except (SupplierPart.DoesNotExist, ValueError):
|
||||
valid = False
|
||||
form.errors['part'] = [_('This field is required')]
|
||||
form.errors['part'] = [_('Invalid SupplierPart selection')]
|
||||
|
||||
data = {
|
||||
'form_valid': valid,
|
||||
@ -639,6 +656,11 @@ class POLineItemCreate(AjaxCreateView):
|
||||
|
||||
form = super().get_form()
|
||||
|
||||
# Limit the available to orders to ones that are PENDING
|
||||
query = form.fields['order'].queryset
|
||||
query = query.filter(status=OrderStatus.PENDING)
|
||||
form.fields['order'].queryset = query
|
||||
|
||||
order_id = form['order'].value()
|
||||
|
||||
try:
|
||||
@ -660,7 +682,7 @@ class POLineItemCreate(AjaxCreateView):
|
||||
|
||||
form.fields['part'].queryset = query
|
||||
form.fields['order'].widget = HiddenInput()
|
||||
except PurchaseOrder.DoesNotExist:
|
||||
except (ValueError, PurchaseOrder.DoesNotExist):
|
||||
pass
|
||||
|
||||
return form
|
||||
|
Loading…
Reference in New Issue
Block a user