From fef5b7548e07acbb7b00ded0d457c8b8ac5f0d18 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 14 Jan 2021 22:06:53 +1100 Subject: [PATCH] Increase unit testing for order API --- InvenTree/company/fixtures/company.yaml | 11 +- InvenTree/order/fixtures/order.yaml | 34 ++++++ InvenTree/order/fixtures/sales_order.yaml | 39 ++++++ InvenTree/order/models.py | 2 +- InvenTree/order/test_api.py | 137 ++++++++++++++++++++-- 5 files changed, 212 insertions(+), 11 deletions(-) create mode 100644 InvenTree/order/fixtures/sales_order.yaml diff --git a/InvenTree/company/fixtures/company.yaml b/InvenTree/company/fixtures/company.yaml index 69edee693a..8301eb0f5e 100644 --- a/InvenTree/company/fixtures/company.yaml +++ b/InvenTree/company/fixtures/company.yaml @@ -5,20 +5,29 @@ fields: name: ACME description: A Cool Military Enterprise + - model: company.company pk: 2 fields: name: Appel Computers description: Think more differenter + - model: company.company pk: 3 fields: name: Zerg Corp description: We eat the competition + - model: company.company pk: 4 fields: name: A customer description: A company that we sell things to! is_customer: True - \ No newline at end of file + +- model: company.company + pk: 5 + fields: + name: Another customer! + description: Yet another company + is_customer: True diff --git a/InvenTree/order/fixtures/order.yaml b/InvenTree/order/fixtures/order.yaml index 4ceaf3d482..8943b36061 100644 --- a/InvenTree/order/fixtures/order.yaml +++ b/InvenTree/order/fixtures/order.yaml @@ -7,6 +7,7 @@ reference: '0001' description: "Ordering some screws" supplier: 1 + status: 10 # Pending # Ordering some screws from Zerg Corp - model: order.purchaseorder @@ -15,6 +16,39 @@ reference: '0002' description: "Ordering some more screws" supplier: 3 + status: 10 # Pending + +- model: order.purchaseorder + pk: 3 + fields: + reference: '0003' + description: 'Another PO' + supplier: 3 + status: 20 # Placed + +- model: order.purchaseorder + pk: 4 + fields: + reference: '0004' + description: 'Another PO' + supplier: 3 + status: 20 # Placed + +- model: order.purchaseorder + pk: 5 + fields: + reference: '0005' + description: 'Another PO' + supplier: 3 + status: 30 # Complete + +- model: order.purchaseorder + pk: 6 + fields: + reference: '0006' + description: 'Another PO' + supplier: 3 + status: 40 # Cancelled # Add some line items against PO 0001 diff --git a/InvenTree/order/fixtures/sales_order.yaml b/InvenTree/order/fixtures/sales_order.yaml new file mode 100644 index 0000000000..e80119fa3e --- /dev/null +++ b/InvenTree/order/fixtures/sales_order.yaml @@ -0,0 +1,39 @@ +- model: order.salesorder + pk: 1 + fields: + reference: 'ABC123' + description: "One sales order, please" + customer: 4 + status: 10 # Pending + +- model: order.salesorder + pk: 2 + fields: + reference: 'ABC124' + description: "One sales order, please" + customer: 4 + status: 10 # Pending + +- model: order.salesorder + pk: 3 + fields: + reference: 'ABC125' + description: "One sales order, please" + customer: 4 + status: 10 # Pending + +- model: order.salesorder + pk: 4 + fields: + reference: 'ABC126' + description: "One sales order, please" + customer: 5 + status: 20 # Shipped + +- model: order.salesorder + pk: 5 + fields: + reference: 'ABC127' + description: "One sales order, please" + customer: 5 + status: 60 # Returned diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index 65729ab993..955859f11a 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -191,7 +191,7 @@ class PurchaseOrder(Order): issue_date = models.DateField( blank=True, null=True, - verbose_name=_('Issue Date'), + verbose_name=_('Issue Date'), help_text=_('Date order was issued') ) diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index 46c6f19277..58599f1eb3 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -2,12 +2,16 @@ Tests for the Order API """ +from datetime import datetime, timedelta + from rest_framework.test import APITestCase from rest_framework import status from django.urls import reverse from django.contrib.auth import get_user_model +from .models import PurchaseOrder, SalesOrder + class OrderTest(APITestCase): @@ -18,6 +22,8 @@ class OrderTest(APITestCase): 'location', 'supplier_part', 'stock', + 'order', + 'sales_order', ] def setUp(self): @@ -26,21 +32,80 @@ class OrderTest(APITestCase): get_user_model().objects.create_user('testuser', 'test@testing.com', 'password') self.client.login(username='testuser', password='password') - def doGet(self, url, options=''): + def doGet(self, url, data={}): - return self.client.get(url + "?" + options, format='json') + return self.client.get(url, data=data, format='json') + + def doPost(self, url, data={}): + return self.client.post(url, data=data, format='json') + + def filter(self, filters, count): + """ + Test API filters + """ + + response = self.doGet( + self.LIST_URL, + filters + ) + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.data), count) + + return response + + +class PurchaseOrderTest(OrderTest): + """ + Tests for the PurchaseOrder API + """ + + LIST_URL = reverse('api-po-list') def test_po_list(self): - - url = reverse('api-po-list') - # List all order items + # List *ALL* PO items + self.filter({}, 6) + + # Filter by supplier + self.filter({'supplier': 1}, 1) + self.filter({'supplier': 3}, 5) + + # Filter by "outstanding" + self.filter({'outstanding': True}, 4) + self.filter({'outstanding': False}, 2) + + # Filter by "status" + self.filter({'status': 10}, 2) + self.filter({'status': 40}, 1) + + def test_overdue(self): + """ + Test "overdue" status + """ + + self.filter({'overdue': True}, 0) + self.filter({'overdue': False}, 6) + + order = PurchaseOrder.objects.get(pk=1) + order.target_date = datetime.now().date() - timedelta(days=10) + order.save() + + self.filter({'overdue': True}, 1) + self.filter({'overdue': False}, 5) + + def test_po_detail(self): + + url = '/api/order/po/1/' + response = self.doGet(url) - self.assertEqual(response.status_code, status.HTTP_200_OK) + + self.assertEqual(response.status_code, 200) - # Filter by stuff - response = self.doGet(url, 'status=10&part=1&supplier_part=1') - self.assertEqual(response.status_code, status.HTTP_200_OK) + data = response.data + + self.assertEqual(data['pk'], 1) + self.assertEqual(data['description'], 'Ordering some screws') def test_po_attachments(self): @@ -50,6 +115,60 @@ class OrderTest(APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) + +class SalesOrderTest(OrderTest): + """ + Tests for the SalesOrder API + """ + + LIST_URL = reverse('api-so-list') + + def test_so_list(self): + + # All orders + self.filter({}, 5) + + # Filter by customer + self.filter({'customer': 4}, 3) + self.filter({'customer': 5}, 2) + + # Filter by outstanding + self.filter({'outstanding': True}, 3) + self.filter({'outstanding': False}, 2) + + # Filter by status + self.filter({'status': 10}, 3) # PENDING + self.filter({'status': 20}, 1) # SHIPPED + self.filter({'status': 99}, 0) # Invalid + + def test_overdue(self): + """ + Test "overdue" status + """ + + self.filter({'overdue': True}, 0) + self.filter({'overdue': False}, 5) + + for pk in [1, 2]: + order = SalesOrder.objects.get(pk=pk) + order.target_date = datetime.now().date() - timedelta(days=10) + order.save() + + self.filter({'overdue': True}, 2) + self.filter({'overdue': False}, 3) + + def test_so_detail(self): + + url = '/api/order/so/1/' + + response = self.doGet(url) + + self.assertEqual(response.status_code, 200) + + data = response.data + + self.assertEqual(data['pk'], 1) + def test_so_attachments(self): url = reverse('api-so-attachment-list')