From 34bfdea4b5458168365dfe256c661b3cb825c0d5 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 29 Aug 2019 23:48:18 +1000 Subject: [PATCH 1/4] Test another invalid serialization --- InvenTree/stock/tests.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py index bc92a4c056..437464a77a 100644 --- a/InvenTree/stock/tests.py +++ b/InvenTree/stock/tests.py @@ -267,6 +267,9 @@ class StockTest(TestCase): with self.assertRaises(ValidationError): item.serializeStock(5, [1, 2, 3, 4, 5], self.user) + with self.assertRaises(ValidationError): + item.serializeStock(5, [1, 2, 3], self.user) + # Pick a StockItem which can actually be serialized item = StockItem.objects.get(pk=100) @@ -284,7 +287,7 @@ class StockTest(TestCase): with self.assertRaises(ValidationError): item.serializeStock(3, "hello", self.user) - def test_seiralize_stock_valid(self): + def test_serialize_stock_valid(self): """ Perform valid stock serializations """ # There are 10 of these in stock From 6f8c3454f38307ff035904c607197c103f5b4d1c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 30 Aug 2019 00:06:46 +1000 Subject: [PATCH 2/4] Tests for stock serialization form --- InvenTree/stock/test_views.py | 35 +++++++++++++++++++++++++++++++++++ InvenTree/stock/views.py | 5 +++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/InvenTree/stock/test_views.py b/InvenTree/stock/test_views.py index 5a7278d7d8..809f7d5956 100644 --- a/InvenTree/stock/test_views.py +++ b/InvenTree/stock/test_views.py @@ -4,6 +4,8 @@ from django.test import TestCase from django.urls import reverse from django.contrib.auth import get_user_model +import json + class StockViewTestCase(TestCase): @@ -128,3 +130,36 @@ class StockItemTest(StockViewTestCase): # Copy from an invalid item, invalid location response = self.client.get(reverse('stock-item-create'), {'location': 999, 'copy': 9999}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 200) + + def test_serialize_item(self): + # Test the serialization view + + url = reverse('stock-item-serialize', args=(100,)) + + # GET the form + response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + self.assertEqual(response.status_code, 200) + + data_valid = { + 'quantity': 5, + 'serial_numbers': '1-5', + 'destination': 4, + 'notes': 'Serializing stock test' + } + + data_invalid = { + 'quantity': 4, + 'serial_numbers': 'dd-23-adf', + 'destination': 'blorg' + } + + # POST + response = self.client.post(url, data_valid, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + self.assertEqual(response.status_code, 200) + data = json.loads(response.content) + self.assertTrue(data['form_valid']) + + response = self.client.post(url, data_invalid, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + self.assertEqual(response.status_code, 200) + data = json.loads(response.content) + self.assertFalse(data['form_valid']) diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 4ecceac3fb..22a0e48dca 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -491,10 +491,10 @@ class StockItemSerialize(AjaxUpdateView): item = self.get_object() - quantity = request.POST.get('quantity', None) + quantity = request.POST.get('quantity', 0) serials = request.POST.get('serial_numbers', '') dest_id = request.POST.get('destination', None) - notes = request.POST.get('note', None) + notes = request.POST.get('note', '') user = request.user valid = True @@ -509,6 +509,7 @@ class StockItemSerialize(AjaxUpdateView): except ValidationError as e: form.errors['serial_numbers'] = e.messages valid = False + numbers = [] if valid: try: From 891f4f9f178d5da2f4177c53d378885e57e56adf Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 30 Aug 2019 00:14:54 +1000 Subject: [PATCH 3/4] Another test --- InvenTree/stock/test_views.py | 8 ++++++++ InvenTree/stock/views.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/InvenTree/stock/test_views.py b/InvenTree/stock/test_views.py index 809f7d5956..71c7594478 100644 --- a/InvenTree/stock/test_views.py +++ b/InvenTree/stock/test_views.py @@ -159,7 +159,15 @@ class StockItemTest(StockViewTestCase): data = json.loads(response.content) self.assertTrue(data['form_valid']) + # Try again to serialize with the same numbers + response = self.client.post(url, data_valid, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + self.assertEqual(response.status_code, 200) + data = json.loads(response.content) + self.assertFalse(data['form_valid']) + + # POST with invalid data response = self.client.post(url, data_invalid, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertFalse(data['form_valid']) + diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 22a0e48dca..61d9517812 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -518,7 +518,7 @@ class StockItemSerialize(AjaxUpdateView): messages = e.message_dict for k in messages.keys(): - if k in ['quantity', 'destionation', 'serial_numbers']: + if k in ['quantity', 'destination', 'serial_numbers']: form.errors[k] = messages[k] else: form.non_field_errors = messages[k] From 094e8cdc2363f576a08f50a33228df0140e9b11c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 30 Aug 2019 00:15:16 +1000 Subject: [PATCH 4/4] PEP --- InvenTree/stock/test_views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/stock/test_views.py b/InvenTree/stock/test_views.py index 71c7594478..51532f36bb 100644 --- a/InvenTree/stock/test_views.py +++ b/InvenTree/stock/test_views.py @@ -170,4 +170,3 @@ class StockItemTest(StockViewTestCase): self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertFalse(data['form_valid']) -