From 6e90ac367ee06a76bead74e0ece3076b0e633bae Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 19 Nov 2019 08:59:56 +1100 Subject: [PATCH] Massaging unit tests - Decimal fields are useful but VERY ANNOYING to use - Needed to fix some test cases --- InvenTree/InvenTree/helpers.py | 18 ++++++++++++++++++ InvenTree/part/models.py | 2 +- InvenTree/part/test_api.py | 4 ++-- InvenTree/stock/models.py | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 231423be8f..5ede677b9f 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -52,6 +52,24 @@ def str2bool(text, test=True): return str(text).lower() in ['0', 'n', 'no', 'none', 'f', 'false', 'off', ] +def decimal2string(d): + """ + Format a Decimal number as a string, + stripping out any trailing zeroes or decimal points. + Essentially make it look like a whole number if it is one. + + Args: + d: A python Decimal object + + Returns: + A string representation of the input number + """ + + s = str(d) + + return s.rstrip("0").rstrip(".") + + def WrapWithQuotes(text, quote='"'): """ Wrap the supplied text with quotes diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 768f509c47..6d2554dc0f 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1161,7 +1161,7 @@ class BomItem(models.Model): return "{n} x {child} to make {parent}".format( parent=self.part.full_name, child=self.sub_part.full_name, - n=self.quantity) + n=helpers.decimal2string(self.quantity)) def get_overage_quantity(self, quantity): """ Calculate overage quantity diff --git a/InvenTree/part/test_api.py b/InvenTree/part/test_api.py index 1b1ef3bc07..f93eb6604d 100644 --- a/InvenTree/part/test_api.py +++ b/InvenTree/part/test_api.py @@ -123,7 +123,7 @@ class PartAPITest(APITestCase): url = reverse('api-bom-item-detail', kwargs={'pk': 3}) response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data['quantity'], 25) + self.assertEqual(int(float(response.data['quantity'])), 25) # Increase the quantity data = response.data @@ -134,7 +134,7 @@ class PartAPITest(APITestCase): # Check that the quantity was increased and a note added self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data['quantity'], 57) + self.assertEqual(int(float(response.data['quantity'])), 57) self.assertEqual(response.data['note'], 'Added a note') def test_add_bom_item(self): diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index dd8e545355..a89489f902 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -691,7 +691,7 @@ class StockItem(models.Model): sn=self.serial) else: s = '{n} x {part}'.format( - n=self.quantity, + n=helpers.decimal2string(self.quantity), part=self.part.full_name) if self.location: